-
Notifications
You must be signed in to change notification settings - Fork 47.9k
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
Use jsxc instead of commoner #2230
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,204 @@ | ||
#!/usr/bin/env node | ||
// -*- mode: js -*- | ||
"use strict"; | ||
|
||
var chokidar = require('chokidar'); | ||
var fs = require('fs'); | ||
var glob = require('glob'); | ||
var mkdirp = require('mkdirp').sync; | ||
var path = require('path'); | ||
var transform = require('../main').transform; | ||
var yargs = require('yargs'); | ||
var VERSION = require('../package.json').version; | ||
|
||
function handleChange(options, exitOnError, args, changedPath) { | ||
var startTime = Date.now(); | ||
var src = path.resolve(args.argv._[0]); | ||
var dest = path.resolve(args.argv._[1]); | ||
|
||
var absoluteChangedPath = path.resolve(changedPath); | ||
var newExt = 'js'; | ||
if (args.argv.extension[0] === '.') { | ||
newExt = '.js'; | ||
} | ||
var changedDest = absoluteChangedPath.replace(src, dest).replace(args.argv.extension, newExt); | ||
fs.readFile(absoluteChangedPath, {encoding: 'utf8'}, function(err, changedSrc) { | ||
if (err) { | ||
console.error(err); | ||
if (exitOnError) { | ||
process.exit(exitOnError); | ||
} | ||
return; | ||
} | ||
|
||
try { | ||
var transformedSrc = transform(changedSrc, options); | ||
} catch (e) { | ||
console.error('[' + new Date() + '] *** ERROR TRANSFORMING ' + JSON.stringify(absoluteChangedPath) + ':'); | ||
console.error(' ' + e.toString()); | ||
if (exitOnError) { | ||
process.exit(exitOnError); | ||
} | ||
return; | ||
} | ||
|
||
mkdirp(path.dirname(changedDest)); | ||
fs.writeFile(changedDest, transformedSrc, {encoding: 'utf8'}, function(err) { | ||
if (err) { | ||
console.error(err); | ||
if (exitOnError) { | ||
process.exit(exitOnError); | ||
} | ||
} | ||
|
||
if (args.argv.s) { | ||
return; | ||
} | ||
|
||
var duration = Date.now() - startTime; | ||
console.log( | ||
'[' + new Date() + '] ' + JSON.stringify(absoluteChangedPath) + ' -> ' + JSON.stringify(changedDest) + ' (' + duration.toFixed(0) + ' ms)' | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
function main() { | ||
var args = yargs | ||
.usage( | ||
'jsxc ' + VERSION + ': Convert files containing JSX syntax to regular JavaScript.\n' + | ||
'Usage:\n' + | ||
' $0 [src file] [dest file] (convert a single file)\n' + | ||
' $0 [src file] (convert a single file and print to stdout)\n' + | ||
' $0 - (convert stdin and print to stdout)\n' + | ||
' $0 [src dir] [dest dir] (convert all files in src dir and write to dest dir)\n' + | ||
' $0 --extension .jsx [src dir] [src dir] (convert all .jsx files in src dir to .js)' + | ||
' $0 --watch [src dir] [dest dir] (watch src dir and place converted files in dest dir)\n' + | ||
' $0 --extension .jsx --watch [src dir] [src dir] (watch all .jsx files in src dir and convert to .js)' | ||
) | ||
.alias('w', 'watch') | ||
.boolean('w') | ||
.describe('w', 'Watch a directory for changes') | ||
.alias('e', 'extension') | ||
.string('e') | ||
.default('e', '.js') | ||
.describe('e', 'Extension of files containing JSX syntax') | ||
.alias('s', 'silent') | ||
.boolean('s') | ||
.describe('s', 'Don\'t display non-error logging') | ||
.default('s', false) | ||
.boolean('a') | ||
.alias('a', 'harmony') | ||
.describe('a', 'Enable ES6 features') | ||
.alias('h', 'help') | ||
.describe('t', 'Strip type annotations') | ||
.alias('t', 'strip-types') | ||
.describe('i', 'Add inline source maps') | ||
.alias('i', 'source-map-inline') | ||
.boolean('h') | ||
.describe('h', 'Show this help message'); | ||
|
||
if (args.argv.h || args.argv._.length === 0 || args.argv._.length > 2) { | ||
console.error(args.help()); | ||
process.exit(1); | ||
} | ||
|
||
require('commoner').version( | ||
require('../package.json').version | ||
).resolve(function(id) { | ||
return this.readModuleP(id); | ||
}).option( | ||
'--harmony', | ||
'Turns on JS transformations such as ES6 Classes etc.' | ||
).option( | ||
'--strip-types', | ||
'Strips out type annotations.' | ||
).option( | ||
'--source-map-inline', | ||
'Embed inline sourcemap in transformed source' | ||
).process(function(id, source) { | ||
// This is where JSX, ES6, etc. desugaring happens. | ||
var options = { | ||
harmony: this.options.harmony, | ||
sourceMap: this.options.sourceMapInline, | ||
stripTypes: this.options.stripTypes | ||
harmony: args.argv.a, | ||
sourceMap: args.argv.i, | ||
stripTypes: args.argv.t | ||
}; | ||
return transform(source, options); | ||
}); | ||
|
||
if (args.argv._.length === 1) { | ||
if (args.argv._[0] === '-') { | ||
// read from stdin | ||
var buf = ''; | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on('data', function(chunk) { | ||
buf += chunk; | ||
}); | ||
process.stdin.on('end', function() { | ||
process.stdout.write(transform(buf, options)); | ||
process.exit(0); | ||
}); | ||
} else { | ||
process.stdout.write( | ||
transform( | ||
fs.readFileSync(args.argv._[0], {encoding: 'utf8'}), | ||
options | ||
) | ||
); | ||
process.exit(0); | ||
} | ||
} else if (args.argv.watch) { | ||
if (!args.argv._.length === 2) { | ||
console.error(args.help()); | ||
process.exit(1); | ||
} | ||
if (!fs.existsSync(args.argv._[0]) || | ||
!fs.lstatSync(args.argv._[0]).isDirectory()) { | ||
console.error('ERROR: ' + JSON.stringify(args.argv._[0]) + ' must be an existing directory'); | ||
process.exit(1); | ||
} | ||
|
||
var watcher = chokidar.watch(args.argv._[0], { | ||
ignored: function(path) { | ||
try { | ||
if (fs.lstatSync(path).isDirectory()) { | ||
return false; | ||
} | ||
} catch (e) { | ||
// ignore files you can't stat | ||
} | ||
return path.slice(path.length - args.argv.extension.length) !== args.argv.extension; | ||
}, | ||
persistent: args.argv.w | ||
}); | ||
|
||
watcher.on('add', handleChange.bind(null, options, false, args)); | ||
watcher.on('change', handleChange.bind(null, options, false, args)); | ||
watcher.on('error', function(err) { | ||
console.error(err); | ||
}); | ||
} else { | ||
if (!fs.existsSync(args.argv._[0])) { | ||
console.error('ERROR: ' + JSON.stringify(args.argv._[0]) + ' must exist'); | ||
process.exit(1); | ||
} | ||
|
||
if (!fs.existsSync(args.argv._[1])) { | ||
console.error('ERROR: ' + JSON.stringify(args.argv._[1]) + ' must exist'); | ||
process.exit(1); | ||
} | ||
|
||
var srcDir = fs.lstatSync(args.argv._[0]).isDirectory(); | ||
var destDir = fs.lstatSync(args.argv._[1]).isDirectory(); | ||
if (srcDir !== destDir) { | ||
console.error(args.help()); | ||
process.exit(1); | ||
} | ||
|
||
if (!srcDir) { | ||
var transformedSrc = transform( | ||
fs.readFileSync(args.argv._[0], {encoding: 'utf8'}), | ||
{harmony: args.argv.a} | ||
); | ||
fs.writeFileSync(args.argv._[1], transformedSrc, {encoding: 'utf8'}); | ||
process.exit(0); | ||
} else { | ||
var newExt = 'js'; | ||
if (args.argv.extension[0] === '.') { | ||
newExt = '.js'; | ||
} | ||
glob(path.join(args.argv._[0], '**', '*' + args.argv.extension), {}, function(err, files) { | ||
if (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
files.forEach(handleChange.bind(null, options, 2, args)); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't optimist take care of outputting options/usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to provide usage yourself. It does describe all of the options. I wanted to put some common use cases in here to make the tool more usable.