diff --git a/README.md b/README.md index f2c4ed1..c18e83d 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ Convert jsx ExtendScript files into jsxbin ```javascript const jsxbin = require( 'jsxbin' ) +const preserveStructure = false; // Set TRUE to preserve directory structure in output -jsxbin( 'path/to/script.js', 'output/script.jsxbin' ) +jsxbin( 'path/to/script.js', 'output/script.jsxbin', preserveStructure ) .then( outputfiles => { console.log( 'Finished!' ) }) @@ -18,7 +19,7 @@ jsxbin( 'path/to/script.js', 'output/script.jsxbin' ) ## Methods -### jsxbin( inputPaths, [outputPath] ) +### jsxbin( inputPaths, [outputPath], [preserveStructure] ) `inputPaths` can be: @@ -35,6 +36,11 @@ jsxbin( 'path/to/script.js', 'output/script.jsxbin' ) - Should only be used when passing an array to `inputPaths`. Input and output arrays must be the same length. - If not given, the files will be created in the same directory as the input file(s) +`preserveStructure`, optional `true|false` (default: `false`) + +- If preserveStructure is true, the original structure of the input files will be preserved in the output directory +- This improvement was first added in v2.3.0 and is therefore default to `false` (to don't break any existing CI/CD scripts) + `jsxbin` returns a promise with an array of file paths to the converted files ### Examples @@ -59,6 +65,12 @@ jsxbin( 'src/*jsx' ) gulp.task( 'jsxbin', () => { return jsxbin( 'src/index.js', 'output/script.jsxbin' ) }) + +// Recursive directory as a gulp task +gulp.task( 'jsxbin', () => { + // Convert every .jsx file in the src directory and place them with same directory structure into output/ + return jsxbin( 'src/**/*.jsx', 'output/', true ) +}) ``` ## From the Command Line @@ -78,6 +90,7 @@ Options -i, --input file(s) The file or files to convert -o, --output file|folder The file or folder where the converted file will be placed -v, --verbose Show more info while running + -p, --preserveStructure Preserve the directory structure in output --debug Show even more info while running -h, --help Show help ``` diff --git a/command.js b/command.js index a37a62a..ad885d9 100755 --- a/command.js +++ b/command.js @@ -8,6 +8,7 @@ const log = require( './src/logger' ) const optionDefinitions = [ { name: 'input', alias: 'i', type: String, multiple: true }, { name: 'output', alias: 'o', type: String }, + { name: 'preserve', alias: 'p', type: Boolean }, { name: 'verbose', alias: 'v', type: Boolean }, { name: 'debug', type: Boolean }, { name: 'help', alias: 'h', type: Boolean } @@ -30,7 +31,7 @@ if ( options.help ) { if ( !options.input || !options.output ) { showUsage() } - jsxbin( options.input, options.output ) + jsxbin( options.input, options.output, options.preserve || false ) .catch( log.error ) } @@ -56,6 +57,11 @@ function showUsage() { typeLabel: '{underline file}|{underline folder}', description: 'The file or folder where the converted file will be placed' }, + { + name: 'preserve', + alias: 'p', + description: 'Preserve the directory structure in output (if input is a directory)' + }, { name: 'verbose', alias: 'v', diff --git a/index.js b/index.js index 2cc0403..a02eeaf 100755 --- a/index.js +++ b/index.js @@ -26,13 +26,17 @@ module.exports.getOutputPaths = getOutputPaths * @return {Promise} A Promise that returns an array of file paths to the * converted files */ -function jsxbin( inputPaths, outputPath ) { +function jsxbin( inputPaths, outputPath, preserveStructure = false ) { if ( !Array.isArray( inputPaths ) && typeof inputPaths === 'object' ) { const options = inputPaths inputPaths = options.input outputPath = options.output } + // This is the basePath of the inputPath to make sure the nested directory + // structure can be preserved in outputPath + const baseInputPath = path.dirname(path.join(process.cwd(), inputPaths[0])); + // Debug some values log.debug( `Current dir: ${process.cwd()}` ) log.debug( 'arguments', { inputPaths, outputPath }) @@ -44,14 +48,18 @@ function jsxbin( inputPaths, outputPath ) { // correct value, an array of absolute paths, that can be used in the // ESTK script. return getInputPaths( inputPaths ) - .then( inputPaths => { + .then(async inputPaths => { input = inputPaths // We also have to convert outputPath into an array of absolute paths - output = getOutputPaths( input, outputPath ) + output = getOutputPaths( input, outputPath, (preserveStructure ? baseInputPath : null) ) if ( outputPath === undefined ) { outputPath = output[0] } + + if ( preserveStructure ) { + await createDir( output.map( outPath => path.dirname(outPath) ) ); + } }) // We have to create the output folder if it does not exist @@ -109,7 +117,7 @@ function getInputPaths( inputPaths ) { return Promise.all( globPromises ).then( () => allPaths ) } -function getOutputPaths( inputPaths, outputPath ) { +function getOutputPaths( inputPaths, outputPath, baseInputPath ) { const output = [] if ( Array.isArray( outputPath ) ) { @@ -151,7 +159,13 @@ function getOutputPaths( inputPaths, outputPath ) { // Replace the extension of the filename with jsxbin and put it // in the output directory const fileName = replaceExtension( filePath, 'jsxbin' ) - output.push( path.join( outputPath, fileName ) ) + + if (baseInputPath) { + const subdirPath = path.dirname(filePath.replace(baseInputPath, '')) + output.push(path.join(outputPath, subdirPath, fileName)) + } else { + output.push(path.join(outputPath, fileName)) + } }) } return output diff --git a/package.json b/package.json index 4b641d6..2ec6e72 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jsxbin", - "version": "2.2.0", + "version": "2.3.0", "description": "Convert jsx ExtendScript files into jsxbin files using ExtendScript Toolkit", "keywords": [ "after effects",