Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(output): preserve directory structure in output (fix issue #13) #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 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 All @@ -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
Expand All @@ -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
```
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