Skip to content

Commit faeba61

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

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ Convert jsx ExtendScript files into jsxbin
66

77
```javascript
88
const jsxbin = require( 'jsxbin' )
9+
const preserveStructure = false; // Set TRUE to preserve directory structure in output
910

10-
jsxbin( 'path/to/script.js', 'output/script.jsxbin' )
11+
jsxbin( 'path/to/script.js', 'output/script.jsxbin', preserveStructure )
1112
.then( outputfiles => {
1213
console.log( 'Finished!' )
1314
})
@@ -18,7 +19,7 @@ jsxbin( 'path/to/script.js', 'output/script.jsxbin' )
1819

1920
## Methods
2021

21-
### jsxbin( inputPaths, [outputPath] )
22+
### jsxbin( inputPaths, [outputPath], [preserveStructure] )
2223

2324
`inputPaths` can be:
2425

@@ -35,6 +36,11 @@ jsxbin( 'path/to/script.js', 'output/script.jsxbin' )
3536
- Should only be used when passing an array to `inputPaths`. Input and output arrays must be the same length.
3637
- If not given, the files will be created in the same directory as the input file(s)
3738

39+
`preserveStructure`, optional `true|false` (default: `false`)
40+
41+
- If preserveStructure is true, the original structure of the input files will be preserved in the output directory
42+
- This improvement was first added in v2.3.0 and is therefore default to `false` (to don't break any existing CI/CD scripts)
43+
3844
`jsxbin` returns a promise with an array of file paths to the converted files
3945

4046
### Examples

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsxbin",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"description": "Convert jsx ExtendScript files into jsxbin files using ExtendScript Toolkit",
55
"keywords": [
66
"after effects",

0 commit comments

Comments
 (0)