Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial thing seems to work, doesn't hang anymore

  • Loading branch information...
commit 5fffa47220333688aa8bdf597008cde174d5abaf 0 parents
James Halliday authored

Showing 4 changed files with 200 additions and 0 deletions. Show diff stats Hide diff stats

  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
Source Rendered
... ... @@ -0,0 +1,59 @@
  1 +pkginit
  2 +=======
  3 +
  4 +Create a package.json file like `npm init` but with more control over package
  5 +defaults.
  6 +
  7 +example
  8 +=======
  9 +
  10 +Create a default.json using
  11 +[prompter syntax](https://github.com/substack/node-prompter#readme)
  12 +to serve as the basis for whenever you type `pkginit`:
  13 +
  14 +``` js
  15 +{
  16 + "name": dirname,
  17 + "description": prompt('Description'),
  18 + "version": "0.0.0",
  19 + "repository": {
  20 + "url": "git://github.com/substack/" + dirname + ".git"
  21 + },
  22 + "main": prompt('Entry point', 'index.js'),
  23 + "scripts": {
  24 + "test": "tap test/*.js"
  25 + },
  26 + "dependencies": {},
  27 + "devDependencies": {
  28 + "tap": "~0.2.5"
  29 + }
  30 +}
  31 +```
  32 +
  33 +Then add this file as the default:
  34 +
  35 +```
  36 +$ pkginit add default default.json
  37 +```
  38 +
  39 +Now when you type `pkginit`, you will only be prompted for the entries you've
  40 +scripted:
  41 +
  42 +```
  43 +$ mkdir beep-boop; cd beep-boop
  44 +$ pkginit
  45 +description: make beep and boop sounds
  46 +entry point: (index.js)
  47 +
  48 +wrote file /home/substack/projects/beep-boop/package.json
  49 +```
  50 +
  51 +variables
  52 +=========
  53 +
  54 +In packages, these variables are availble:
  55 +
  56 +* basename - shorthand for `path.basename(process.cwd())`
  57 +* process - node's `process`
  58 +* require - node's `require`
  59 +* env - shorthand for `process.env`
51 bin/cmd.js
... ... @@ -0,0 +1,51 @@
  1 +var pkginit = require('../');
  2 +var argv = require('optimist').argv;
  3 +var fs = require('fs');
  4 +var cmd = argv._[0];
  5 +
  6 +pkginit({ context : argv }, function (err, pkg) {
  7 + if (err) return console.error(err);
  8 +
  9 + if (cmd === 'add' && argv._.length == 3) {
  10 + var name = argv._[1];
  11 + var file = argv._[2];
  12 + fs.readFile(file, function (err, src) {
  13 + if (err) return console.error(err);
  14 + pkg.add(name, src, function (err) {
  15 + if (err) return console.error(err)
  16 + else console.log('added file for ' + JSON.stringify(name))
  17 + });
  18 + });
  19 + }
  20 + else if (cmd === 'rm') {
  21 + var name = argv._[1];
  22 + pkg.rm(name, function (err) {
  23 + if (err) console.error(err)
  24 + });
  25 + }
  26 + else if (cmd === 'list' || cmd === 'ls' || cmd === 'l') {
  27 + pkg.list(function (err, files) {
  28 + if (err) console.error(err);
  29 + if (files && files.length) {
  30 + console.log(files.join('\r\n'));
  31 + }
  32 + });
  33 + }
  34 + else {
  35 + var name = (cmd === 'build' ? argv._[1] : argv._[0]) || 'default';
  36 +
  37 + var outfile = argv.outfile || argv.o || 'package.json';
  38 + pkg.build(name, function (err, output) {
  39 + if (err) console.error(err)
  40 + else if (outfile === '-') {
  41 + console.log(output);
  42 + }
  43 + else fs.writeFile(outfile, output, function (err) {
  44 + if (err) return console.error(err)
  45 + if (argv.verbose || argv.v) {
  46 + console.error('\r\nwrote file: ' + outfile);
  47 + }
  48 + });
  49 + });
  50 + }
  51 +});
16 example/pkg.json
... ... @@ -0,0 +1,16 @@
  1 +{
  2 + "name": basename.replace(/^node-/, ''),
  3 + "description": prompt(),
  4 + "version": "0.0.0",
  5 + "repository": {
  6 + "url": "git://github.com/substack/" + basename + ".git"
  7 + },
  8 + "main": prompt('entry point', 'index.js'),
  9 + "scripts": {
  10 + "test": "tap test/*.js"
  11 + },
  12 + "dependencies": {},
  13 + "devDependencies": {
  14 + "tap": "~0.2.5"
  15 + }
  16 +}
74 index.js
... ... @@ -0,0 +1,74 @@
  1 +var mkdirp = require('mkdirp');
  2 +var prompter = require('prompter');
  3 +var merge = require('merge');
  4 +
  5 +var fs = require('fs');
  6 +var path = require('path');
  7 +var home = process.env.HOME || process.env.USERPROFILE;
  8 +
  9 +exports = module.exports = function (opts, cb) {
  10 + if (!opts) opts = {};
  11 + opts.configDir = opts.configDir || path.join(home, '.config', 'pkginit');
  12 + opts.context = merge({
  13 + basename : path.basename(process.cwd()),
  14 + process : process,
  15 + env : process.env,
  16 + require : function (name) {
  17 + if (/^[.\/]/.test(name)) {
  18 + return require(path.resolve(opts.configDir, name))
  19 + }
  20 + else return require(name)
  21 + }
  22 + }, opts.context || {});
  23 +
  24 + mkdirp(opts.configDir, function (err) {
  25 + if (err) cb(err)
  26 + else cb(null, new PkgInit(opts))
  27 + });
  28 +};
  29 +
  30 +function PkgInit (opts) {
  31 + this.configDir = opts.configDir;
  32 + this.opts = opts;
  33 + this.context = opts.context;
  34 +}
  35 +
  36 +PkgInit.prototype.build = function (name, cb) {
  37 + var self = this;
  38 + var opts = self.opts;
  39 + var stderr = opts.stderr || process.stderr;
  40 + var stdout = opts.stdout || process.stdout;
  41 + var stdin = opts.stdin || process.stdin;
  42 + var file = path.join(self.configDir, name + '.json');
  43 +
  44 + fs.readFile(file, function (err, src) {
  45 + if (err) return cb(err);
  46 + var s = prompter(src, self.context, function (err, output) {
  47 + cb(err, output);
  48 + stdin.pause();
  49 + });
  50 + s.pipe(stderr, { end : false });
  51 + stdin.pipe(s);
  52 + if (typeof stdin.resume === 'function') stdin.resume();
  53 + });
  54 +};
  55 +
  56 +PkgInit.prototype.add = function (name, src, cb) {
  57 + var file = path.join(this.configDir, name + '.json');
  58 + fs.writeFile(file, src, cb);
  59 +};
  60 +
  61 +PkgInit.prototype.rm = function (name, cb) {
  62 + var file = path.join(this.configDir, name + '.json');
  63 + fs.unlink(file, cb);
  64 +};
  65 +
  66 +PkgInit.prototype.list = function (cb) {
  67 + fs.readdir(this.configDir, function (err, files) {
  68 + if (err) cb(err)
  69 + else cb(null, files
  70 + .filter(function (x) { return /\.json$/.test(x) })
  71 + .map(function (x) { return /\.json$/.replace('') })
  72 + )
  73 + });
  74 +};

0 comments on commit 5fffa47

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