Skip to content

Commit

Permalink
fix(output): preserve directory structure in output (fix issue runega…
Browse files Browse the repository at this point in the history
…n#13)

Change-Id: Ic6da54ffac9ce8e19e63a6a08d63858079f434d1
  • Loading branch information
smhagit committed May 5, 2021
1 parent e292a52 commit faeba61
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!' )
})
Expand All @@ -18,7 +19,7 @@ jsxbin( 'path/to/script.js', 'output/script.jsxbin' )

## Methods

### jsxbin( inputPaths, [outputPath] )
### jsxbin( inputPaths, [outputPath], [preserveStructure] )

`inputPaths` can be:

Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 )
}

Expand All @@ -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',
Expand Down
24 changes: 19 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit faeba61

Please sign in to comment.