Skip to content

Commit 74bf042

Browse files
committed
fix(output): preserve directory structure in output (fix issue runegan#13)
Change-Id: Ic6da54ffac9ce8e19e63a6a08d63858079f434d1
1 parent e292a52 commit 74bf042

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

command.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const log = require( './src/logger' )
88
const optionDefinitions = [
99
{ name: 'input', alias: 'i', type: String, multiple: true },
1010
{ name: 'output', alias: 'o', type: String },
11+
{ name: 'preserve', alias: 'p', type: Boolean },
1112
{ name: 'verbose', alias: 'v', type: Boolean },
1213
{ name: 'debug', type: Boolean },
1314
{ name: 'help', alias: 'h', type: Boolean }
@@ -30,7 +31,7 @@ if ( options.help ) {
3031
if ( !options.input || !options.output ) {
3132
showUsage()
3233
}
33-
jsxbin( options.input, options.output )
34+
jsxbin(options.input, options.output, options.preserve || false )
3435
.catch( log.error )
3536
}
3637

@@ -56,6 +57,11 @@ function showUsage() {
5657
typeLabel: '{underline file}|{underline folder}',
5758
description: 'The file or folder where the converted file will be placed'
5859
},
60+
{
61+
name: 'preserve',
62+
alias: 'p',
63+
description: 'Preserve the directory structure in output (if input is a directory)'
64+
},
5965
{
6066
name: 'verbose',
6167
alias: 'v',

index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ module.exports.getOutputPaths = getOutputPaths
2626
* @return {Promise} A Promise that returns an array of file paths to the
2727
* converted files
2828
*/
29-
function jsxbin( inputPaths, outputPath ) {
29+
function jsxbin(inputPaths, outputPath, preserveStructure = false ) {
3030
if ( !Array.isArray( inputPaths ) && typeof inputPaths === 'object' ) {
3131
const options = inputPaths
3232
inputPaths = options.input
3333
outputPath = options.output
3434
}
3535

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+
3640
// Debug some values
3741
log.debug( `Current dir: ${process.cwd()}` )
3842
log.debug( 'arguments', { inputPaths, outputPath })
@@ -44,14 +48,18 @@ function jsxbin( inputPaths, outputPath ) {
4448
// correct value, an array of absolute paths, that can be used in the
4549
// ESTK script.
4650
return getInputPaths( inputPaths )
47-
.then( inputPaths => {
51+
.then(async inputPaths => {
4852
input = inputPaths
4953

5054
// 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) )
5256
if ( outputPath === undefined ) {
5357
outputPath = output[0]
5458
}
59+
60+
if (preserveStructure) {
61+
await createDir(output.map(outPath => path.dirname(outPath)));
62+
}
5563
})
5664

5765
// We have to create the output folder if it does not exist
@@ -109,7 +117,7 @@ function getInputPaths( inputPaths ) {
109117
return Promise.all( globPromises ).then( () => allPaths )
110118
}
111119

112-
function getOutputPaths( inputPaths, outputPath ) {
120+
function getOutputPaths(inputPaths, outputPath, baseInputPath ) {
113121
const output = []
114122

115123
if ( Array.isArray( outputPath ) ) {
@@ -151,7 +159,13 @@ function getOutputPaths( inputPaths, outputPath ) {
151159
// Replace the extension of the filename with jsxbin and put it
152160
// in the output directory
153161
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+
}
155169
})
156170
}
157171
return output

0 commit comments

Comments
 (0)