diff --git a/packages/babel-cli/bin/babel-plugin b/packages/babel-cli/bin/babel-plugin/index.js similarity index 53% rename from packages/babel-cli/bin/babel-plugin rename to packages/babel-cli/bin/babel-plugin/index.js index a8e9ea5e2b..ff304fd93c 100755 --- a/packages/babel-cli/bin/babel-plugin +++ b/packages/babel-cli/bin/babel-plugin/index.js @@ -33,11 +33,24 @@ function spawnMultiple(cmds) { next(); } +function template(name, data) { + var source = fs.readFileSync(__dirname + "/templates/" + name, "utf8"); + source = source.replace(/[A-Z_]+/g, function (key) { + return data[key] === undefined ? key : data[key]; + }); + return source; +} + function write(filename, content) { console.log(filename); fs.writeFileSync(filename, content); } +var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + var BABEL_PLUGIN_PREFIX = "babel-plugin-"; var cmds = { @@ -48,36 +61,49 @@ var cmds = { name = name.slice(BABEL_PLUGIN_PREFIX.length); } - write("package.json", JSON.stringify({ - name: BABEL_PLUGIN_PREFIX + name, - version: "1.0.0", - description: "", - license: "MIT", - main: "lib/index.js", + rl.question("Description (optional): ", function (description) { + rl.question("GitHub Repository (eg. sebmck/babel-plugin-foobar) (optional): ", function (repo) { + rl.close(); - devDependencies: { - babel: "^5.6.0" - }, + var templateData = { + DESCRIPTION: description, + FULL_NAME: BABEL_PLUGIN_PREFIX + name, + NAME: name + }; - peerDependencies: { - babel: "^5.6.0" - }, + write("package.json", JSON.stringify({ + name: templateData.FULL_NAME, + version: "1.0.0", + description: templateData.DESCRIPTION, + repository: repo || undefined, + license: "MIT", + main: "lib/index.js", - scripts: { - build: "babel-plugin build", - push: "babel-plugin publish", - test: "babel-plugin test" - }, + devDependencies: { + babel: "^5.6.0" + }, - keywords: ["babel-plugin"] - }, null, " ")); + scripts: { + build: "babel-plugin build", + push: "babel-plugin publish", + test: "babel-plugin test" + }, - write(".npmignore", "node_modules\n*.log\nsrc"); + keywords: ["babel-plugin"] + }, null, " ") + "\n"); - write(".gitignore", "node_modules\n*.log\nlib"); + write(".npmignore", "node_modules\n*.log\nsrc\n"); - fs.mkdirSync("src"); - write("src/index.js", ""); + write(".gitignore", "node_modules\n*.log\nlib\n"); + + write("README.md", template("README.md", templateData)); + + if (!fs.existsSync("src")) { + fs.mkdirSync("src"); + write("src/index.js", template("index.js", templateData)); + } + }); + }); }, build: function () { @@ -85,15 +111,12 @@ var cmds = { }, publish: function () { - var rl = readline.createInterface({ - input: process.stdin, - output: process.stdout - }); - var pkg = require(process.cwd() + "/package.json"); console.log("Current verison:", pkg.version); rl.question("New version (enter nothing for patch): ", function (newVersion) { + rl.close(); + newVersion = newVersion || "patch"; spawnMultiple([ diff --git a/packages/babel-cli/bin/babel-plugin/templates/README.md b/packages/babel-cli/bin/babel-plugin/templates/README.md new file mode 100644 index 0000000000..b8f71ad228 --- /dev/null +++ b/packages/babel-cli/bin/babel-plugin/templates/README.md @@ -0,0 +1,35 @@ +# FULL_NAME + +DESCRIPTION + +## Installation + +```sh +$ npm install FULL_NAME +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +```json +{ + "plugins": ["NAME"] +} +``` + +### Via CLI + +```sh +$ babel --plugins NAME script.js +``` + +### Via Node API + +```javascript +require("babel-core").transform("code", { + plugins: ["NAME"] +}); +``` diff --git a/packages/babel-cli/bin/babel-plugin/templates/index.js b/packages/babel-cli/bin/babel-plugin/templates/index.js new file mode 100644 index 0000000000..3dc6bc100a --- /dev/null +++ b/packages/babel-cli/bin/babel-plugin/templates/index.js @@ -0,0 +1,7 @@ +export default function ({ Plugin, types: t }) { + return new Plugin("NAME", { + visitor: { + // your visitor methods go here + } + }); +} diff --git a/packages/babel-cli/package.json b/packages/babel-cli/package.json index af82623f3a..17fbd41211 100644 --- a/packages/babel-cli/package.json +++ b/packages/babel-cli/package.json @@ -24,6 +24,6 @@ "babel": "./bin/babel/index.js", "babel-node": "./bin/babel-node", "babel-external-helpers": "./bin/babel-external-helpers", - "babel-plugin": "./bin/babel-plugin" + "babel-plugin": "./bin/babel-plugin/index.js" } }