babel-codemod
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -13,6 +13,5 @@ "use strict"; | ||
const path_1 = require("path"); | ||
const resolve_1 = require("resolve"); | ||
const iterateSources_1 = require("./iterateSources"); | ||
const Options_1 = require("./Options"); | ||
const TransformRunner_1 = require("./TransformRunner"); | ||
const DEFAULT_EXTENSIONS = new Set(['.js', '.jsx']); | ||
function printHelp(out) { | ||
@@ -27,3 +26,3 @@ let $0 = path_1.basename(process.argv[1]); | ||
-r, --require PATH Require PATH before transform (allows multiple). | ||
--extensions EXTS Comma-separated extensions to process (default: "${Array.from(DEFAULT_EXTENSIONS).join(',')}"). | ||
--extensions EXTS Comma-separated extensions to process (default: "${Array.from(Options_1.DEFAULT_EXTENSIONS).join(',')}"). | ||
-s, --stdio Read source from stdin and print to stdout. | ||
@@ -62,3 +61,3 @@ -h, --help Show this help message. | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let options = Options.parse(args); | ||
let options = Options_1.default.parse(args); | ||
if (options instanceof Error) { | ||
@@ -116,122 +115,3 @@ process.stderr.write(`ERROR: ${options.message}\n`); | ||
exports.default = run; | ||
class Options { | ||
constructor(sourcePaths, pluginFilePaths, pluginOptions, extensions, requires, ignore, stdio, help) { | ||
this.sourcePaths = sourcePaths; | ||
this.pluginFilePaths = pluginFilePaths; | ||
this.pluginOptions = pluginOptions; | ||
this.extensions = extensions; | ||
this.requires = requires; | ||
this.ignore = ignore; | ||
this.stdio = stdio; | ||
this.help = help; | ||
} | ||
getPlugins() { | ||
if (!this.plugins) { | ||
this.plugins = this.loadPlugins(); | ||
} | ||
return this.plugins; | ||
} | ||
loadRequires() { | ||
for (let modulePath of this.requires) { | ||
require(modulePath); | ||
} | ||
} | ||
loadPlugins() { | ||
return this.pluginFilePaths.map(pluginFilePath => { | ||
let name = path_1.basename(pluginFilePath, path_1.extname(pluginFilePath)); | ||
let options = this.pluginOptions.get(name); | ||
let exports = require(pluginFilePath); | ||
let plugin; | ||
if (exports.default) { | ||
plugin = exports.default; | ||
} | ||
else { | ||
plugin = exports; | ||
} | ||
if (options) { | ||
return [plugin, options]; | ||
} | ||
else { | ||
return plugin; | ||
} | ||
}); | ||
} | ||
static parse(args) { | ||
let sourcePaths = []; | ||
let pluginFilePaths = []; | ||
let pluginOptions = new Map(); | ||
let extensions = DEFAULT_EXTENSIONS; | ||
let ignore = (path, basename, root) => basename[0] === '.'; | ||
let requires = []; | ||
let stdio = false; | ||
let help = false; | ||
for (let i = 0; i < args.length; i++) { | ||
let arg = args[i]; | ||
switch (arg) { | ||
case '-p': | ||
case '--plugin': | ||
i++; | ||
pluginFilePaths.push(getRequirableModulePath(args[i])); | ||
break; | ||
case '-o': | ||
case '--plugin-options': | ||
i++; | ||
let nameAndOptions = args[i].split('='); | ||
let name = nameAndOptions[0]; | ||
let optionsRaw = nameAndOptions[1]; | ||
if (optionsRaw && optionsRaw[0] === '@') { | ||
optionsRaw = fs_1.readFileSync(args[i].slice(1), { encoding: 'utf8' }); | ||
pluginOptions.set(name, JSON.parse(optionsRaw)); | ||
} | ||
try { | ||
pluginOptions.set(name, JSON.parse(optionsRaw)); | ||
} | ||
catch (err) { | ||
throw new Error(`unable to parse JSON config for ${name}: ${optionsRaw}`); | ||
} | ||
break; | ||
case '-r': | ||
case '--require': | ||
i++; | ||
requires.push(getRequirableModulePath(args[i])); | ||
break; | ||
case '--extensions': | ||
i++; | ||
extensions = new Set(args[i] | ||
.split(',') | ||
.map(ext => ext[0] === '.' ? ext : `.${ext}`)); | ||
break; | ||
case '-s': | ||
case '--stdio': | ||
stdio = true; | ||
break; | ||
case '-h': | ||
case '--help': | ||
help = true; | ||
break; | ||
default: | ||
if (arg[0] === '-') { | ||
throw new Error(`unexpected option: ${arg}`); | ||
} | ||
else { | ||
sourcePaths.push(arg); | ||
} | ||
break; | ||
} | ||
} | ||
return new Options(sourcePaths, pluginFilePaths, pluginOptions, extensions, requires, ignore, stdio, help); | ||
} | ||
} | ||
/** | ||
* Gets a path that can be passed to `require` for a given module path. | ||
*/ | ||
function getRequirableModulePath(modulePath) { | ||
if (fs_1.existsSync(modulePath) || fs_1.existsSync(modulePath + '.js')) { | ||
return path_1.resolve(modulePath); | ||
} | ||
else { | ||
return resolve_1.sync(modulePath, { basedir: process.cwd() }); | ||
} | ||
} | ||
/** | ||
* Reads stdin and resolves to the read string. | ||
@@ -238,0 +118,0 @@ */ |
{ | ||
"name": "babel-codemod", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "babel-codemod rewrites JavaScript using babel plugins.", | ||
@@ -27,12 +27,14 @@ "main": "dist/index.js", | ||
"@types/babylon": "^6.7.15", | ||
"@types/glob": "^5.0.30", | ||
"@types/mocha": "^2.2.40", | ||
"@types/node": "^7.0.5", | ||
"mocha": "^3.2.0", | ||
"semantic-release": "^6.3.2", | ||
"ts-node": "^2.1.0", | ||
"tslint": "^4.5.1", | ||
"typescript": "^2.2.1", | ||
"semantic-release": "^6.3.2" | ||
"typescript": "^2.2.1" | ||
}, | ||
"dependencies": { | ||
"babel-core": "^6.23.1", | ||
"glob": "^7.1.1", | ||
"mz": "^2.6.0", | ||
@@ -39,0 +41,0 @@ "recast": "^0.12.1", |
@@ -1,2 +0,2 @@ | ||
# babel-codemod | ||
# babel-codemod [![Build Status](https://travis-ci.org/square/babel-codemod.svg?branch=master)](https://travis-ci.org/square/babel-codemod) [![dependencies Status](https://david-dm.org/square/babel-codemod/status.svg)](https://david-dm.org/square/babel-codemod) | ||
@@ -3,0 +3,0 @@ babel-codemod rewrites JavaScript using babel plugins. |
Sorry, the diff of this file is not supported yet
43623
12
736
5
11
10
+ Addedglob@^7.1.1
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedwrappy@1.0.2(transitive)