@@ -26,13 +26,17 @@ module.exports.getOutputPaths = getOutputPaths
26
26
* @return {Promise } A Promise that returns an array of file paths to the
27
27
* converted files
28
28
*/
29
- function jsxbin ( inputPaths , outputPath ) {
29
+ function jsxbin ( inputPaths , outputPath , preserveStructure = false ) {
30
30
if ( ! Array . isArray ( inputPaths ) && typeof inputPaths === 'object' ) {
31
31
const options = inputPaths
32
32
inputPaths = options . input
33
33
outputPath = options . output
34
34
}
35
35
36
+ // This is the basePath of the inputPath to make sure the nested directory
37
+ // structure can be preserved in outputPath
38
+ const baseInputPath = path . dirname ( path . join ( process . cwd ( ) , inputPaths [ 0 ] ) ) ;
39
+
36
40
// Debug some values
37
41
log . debug ( `Current dir: ${ process . cwd ( ) } ` )
38
42
log . debug ( 'arguments' , { inputPaths, outputPath } )
@@ -44,14 +48,18 @@ function jsxbin( inputPaths, outputPath ) {
44
48
// correct value, an array of absolute paths, that can be used in the
45
49
// ESTK script.
46
50
return getInputPaths ( inputPaths )
47
- . then ( inputPaths => {
51
+ . then ( async inputPaths => {
48
52
input = inputPaths
49
53
50
54
// We also have to convert outputPath into an array of absolute paths
51
- output = getOutputPaths ( input , outputPath )
55
+ output = getOutputPaths ( input , outputPath , ( preserveStructure ? baseInputPath : null ) )
52
56
if ( outputPath === undefined ) {
53
57
outputPath = output [ 0 ]
54
58
}
59
+
60
+ if ( preserveStructure ) {
61
+ await createDir ( output . map ( outPath => path . dirname ( outPath ) ) ) ;
62
+ }
55
63
} )
56
64
57
65
// We have to create the output folder if it does not exist
@@ -109,7 +117,7 @@ function getInputPaths( inputPaths ) {
109
117
return Promise . all ( globPromises ) . then ( ( ) => allPaths )
110
118
}
111
119
112
- function getOutputPaths ( inputPaths , outputPath ) {
120
+ function getOutputPaths ( inputPaths , outputPath , baseInputPath ) {
113
121
const output = [ ]
114
122
115
123
if ( Array . isArray ( outputPath ) ) {
@@ -151,7 +159,13 @@ function getOutputPaths( inputPaths, outputPath ) {
151
159
// Replace the extension of the filename with jsxbin and put it
152
160
// in the output directory
153
161
const fileName = replaceExtension ( filePath , 'jsxbin' )
154
- output . push ( path . join ( outputPath , fileName ) )
162
+
163
+ if ( baseInputPath ) {
164
+ const subdirPath = path . dirname ( filePath . replace ( baseInputPath , '' ) )
165
+ output . push ( path . join ( outputPath , subdirPath , fileName ) )
166
+ } else {
167
+ output . push ( path . join ( outputPath , fileName ) )
168
+ }
155
169
} )
156
170
}
157
171
return output
0 commit comments