Skip to content
Browse files

initial thing seems to work, doesn't hang anymore

  • Loading branch information...
0 parents commit 5fffa47220333688aa8bdf597008cde174d5abaf @substack committed Jun 12, 2012
Showing with 200 additions and 0 deletions.
  1. +59 −0 README.markdown
  2. +51 −0 bin/cmd.js
  3. +16 −0 example/pkg.json
  4. +74 −0 index.js
59 README.markdown
@@ -0,0 +1,59 @@
+pkginit
+=======
+
+Create a package.json file like `npm init` but with more control over package
+defaults.
+
+example
+=======
+
+Create a default.json using
+[prompter syntax](https://github.com/substack/node-prompter#readme)
+to serve as the basis for whenever you type `pkginit`:
+
+``` js
+{
+ "name": dirname,
+ "description": prompt('Description'),
+ "version": "0.0.0",
+ "repository": {
+ "url": "git://github.com/substack/" + dirname + ".git"
+ },
+ "main": prompt('Entry point', 'index.js'),
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "tap": "~0.2.5"
+ }
+}
+```
+
+Then add this file as the default:
+
+```
+$ pkginit add default default.json
+```
+
+Now when you type `pkginit`, you will only be prompted for the entries you've
+scripted:
+
+```
+$ mkdir beep-boop; cd beep-boop
+$ pkginit
+description: make beep and boop sounds
+entry point: (index.js)
+
+wrote file /home/substack/projects/beep-boop/package.json
+```
+
+variables
+=========
+
+In packages, these variables are availble:
+
+* basename - shorthand for `path.basename(process.cwd())`
+* process - node's `process`
+* require - node's `require`
+* env - shorthand for `process.env`
51 bin/cmd.js
@@ -0,0 +1,51 @@
+var pkginit = require('../');
+var argv = require('optimist').argv;
+var fs = require('fs');
+var cmd = argv._[0];
+
+pkginit({ context : argv }, function (err, pkg) {
+ if (err) return console.error(err);
+
+ if (cmd === 'add' && argv._.length == 3) {
+ var name = argv._[1];
+ var file = argv._[2];
+ fs.readFile(file, function (err, src) {
+ if (err) return console.error(err);
+ pkg.add(name, src, function (err) {
+ if (err) return console.error(err)
+ else console.log('added file for ' + JSON.stringify(name))
+ });
+ });
+ }
+ else if (cmd === 'rm') {
+ var name = argv._[1];
+ pkg.rm(name, function (err) {
+ if (err) console.error(err)
+ });
+ }
+ else if (cmd === 'list' || cmd === 'ls' || cmd === 'l') {
+ pkg.list(function (err, files) {
+ if (err) console.error(err);
+ if (files && files.length) {
+ console.log(files.join('\r\n'));
+ }
+ });
+ }
+ else {
+ var name = (cmd === 'build' ? argv._[1] : argv._[0]) || 'default';
+
+ var outfile = argv.outfile || argv.o || 'package.json';
+ pkg.build(name, function (err, output) {
+ if (err) console.error(err)
+ else if (outfile === '-') {
+ console.log(output);
+ }
+ else fs.writeFile(outfile, output, function (err) {
+ if (err) return console.error(err)
+ if (argv.verbose || argv.v) {
+ console.error('\r\nwrote file: ' + outfile);
+ }
+ });
+ });
+ }
+});
16 example/pkg.json
@@ -0,0 +1,16 @@
+{
+ "name": basename.replace(/^node-/, ''),
+ "description": prompt(),
+ "version": "0.0.0",
+ "repository": {
+ "url": "git://github.com/substack/" + basename + ".git"
+ },
+ "main": prompt('entry point', 'index.js'),
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "tap": "~0.2.5"
+ }
+}
74 index.js
@@ -0,0 +1,74 @@
+var mkdirp = require('mkdirp');
+var prompter = require('prompter');
+var merge = require('merge');
+
+var fs = require('fs');
+var path = require('path');
+var home = process.env.HOME || process.env.USERPROFILE;
+
+exports = module.exports = function (opts, cb) {
+ if (!opts) opts = {};
+ opts.configDir = opts.configDir || path.join(home, '.config', 'pkginit');
+ opts.context = merge({
+ basename : path.basename(process.cwd()),
+ process : process,
+ env : process.env,
+ require : function (name) {
+ if (/^[.\/]/.test(name)) {
+ return require(path.resolve(opts.configDir, name))
+ }
+ else return require(name)
+ }
+ }, opts.context || {});
+
+ mkdirp(opts.configDir, function (err) {
+ if (err) cb(err)
+ else cb(null, new PkgInit(opts))
+ });
+};
+
+function PkgInit (opts) {
+ this.configDir = opts.configDir;
+ this.opts = opts;
+ this.context = opts.context;
+}
+
+PkgInit.prototype.build = function (name, cb) {
+ var self = this;
+ var opts = self.opts;
+ var stderr = opts.stderr || process.stderr;
+ var stdout = opts.stdout || process.stdout;
+ var stdin = opts.stdin || process.stdin;
+ var file = path.join(self.configDir, name + '.json');
+
+ fs.readFile(file, function (err, src) {
+ if (err) return cb(err);
+ var s = prompter(src, self.context, function (err, output) {
+ cb(err, output);
+ stdin.pause();
+ });
+ s.pipe(stderr, { end : false });
+ stdin.pipe(s);
+ if (typeof stdin.resume === 'function') stdin.resume();
+ });
+};
+
+PkgInit.prototype.add = function (name, src, cb) {
+ var file = path.join(this.configDir, name + '.json');
+ fs.writeFile(file, src, cb);
+};
+
+PkgInit.prototype.rm = function (name, cb) {
+ var file = path.join(this.configDir, name + '.json');
+ fs.unlink(file, cb);
+};
+
+PkgInit.prototype.list = function (cb) {
+ fs.readdir(this.configDir, function (err, files) {
+ if (err) cb(err)
+ else cb(null, files
+ .filter(function (x) { return /\.json$/.test(x) })
+ .map(function (x) { return /\.json$/.replace('') })
+ )
+ });
+};

0 comments on commit 5fffa47

Please sign in to comment.
Something went wrong with that request. Please try again.