Permalink
Browse files

Install init-package-json module

  • Loading branch information...
1 parent 5a02feb commit 7f4f4041b3e7785fdf39bbb54503512d00dcf9ab @isaacs isaacs committed Jun 17, 2012
Showing with 1,336 additions and 2 deletions.
  1. +43 −0 node_modules/init-package-json/README.md
  2. +175 −0 node_modules/init-package-json/default-input.js
  3. +14 −0 node_modules/init-package-json/example.js
  4. +104 −0 node_modules/init-package-json/init-package-json.js
  5. +1 −0 node_modules/init-package-json/node_modules/promzard/.npmignore
  6. +130 −0 node_modules/init-package-json/node_modules/promzard/README.md
  7. +11 −0 node_modules/init-package-json/node_modules/promzard/example/index.js
  8. +8 −0 node_modules/init-package-json/node_modules/promzard/example/npm-init/README.md
  9. +191 −0 node_modules/init-package-json/node_modules/promzard/example/npm-init/init-input.js
  10. +37 −0 node_modules/init-package-json/node_modules/promzard/example/npm-init/init.js
  11. +10 −0 node_modules/init-package-json/node_modules/promzard/example/npm-init/package.json
  12. +61 −0 node_modules/init-package-json/node_modules/promzard/example/substack-input.js
  13. +26 −0 node_modules/init-package-json/node_modules/promzard/package.json
  14. +216 −0 node_modules/init-package-json/node_modules/promzard/promzard.js
  15. +83 −0 node_modules/init-package-json/node_modules/promzard/test/basic.js
  16. +5 −0 node_modules/init-package-json/node_modules/promzard/test/exports.input
  17. +30 −0 node_modules/init-package-json/node_modules/promzard/test/exports.js
  18. +18 −0 node_modules/init-package-json/node_modules/promzard/test/fn.input
  19. +39 −0 node_modules/init-package-json/node_modules/promzard/test/fn.js
  20. +8 −0 node_modules/init-package-json/node_modules/promzard/test/simple.input
  21. +30 −0 node_modules/init-package-json/node_modules/promzard/test/simple.js
  22. +42 −0 node_modules/init-package-json/package.json
  23. +15 −0 node_modules/init-package-json/test/basic.input
  24. +35 −0 node_modules/init-package-json/test/basic.js
  25. +4 −2 package.json
View
43 node_modules/init-package-json/README.md
@@ -0,0 +1,43 @@
+# init-package-json
+
+A node module to get your node module started.
+
+## Usage
+
+```javascript
+var init = require('init-package-json')
+var path = require('path')
+
+// a path to a promzard module. In the event that this file is
+// not found, one will be provided for you.
+var initFile = path.resolve(process.env.HOME, '.npm-init')
+
+// the dir where we're doin stuff.
+var dir = process.cwd()
+
+// extra stuff that gets put into the PromZard module's context.
+// In npm, this is the resolved config object. Exposed as 'config'
+// Optional.
+var configData = { some: 'extra stuff' }
+
+// Any existing stuff from the package.json file is also exposed in the
+// PromZard module as the `package` object. There will also be free
+// vars for:
+// * `filename` path to the package.json file
+// * `basename` the tip of the package dir
+// * `dirname` the parent of the package dir
+
+init(dir, initFile, configData, function (er, data) {
+ // the data's already been written to {dir}/package.json
+ // now you can do stuff with it
+})
+```
+
+Or from the command line:
+
+```
+$ npm-init
+```
+
+See [PromZard](https://github.com/isaacs/promzard) for details about
+what can go in the config file.
View
175 node_modules/init-package-json/default-input.js
@@ -0,0 +1,175 @@
+var fs = require('fs')
+var path = require('path')
+var glob = require('glob')
+
+// more popular packages should go here, maybe?
+function isTestPkg (p) {
+ return !!p.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/)
+}
+
+function niceName (n) {
+ return n.replace(/^node-|[.-]js$/g, '')
+}
+
+function readDeps (test) { return function (cb) {
+ fs.readdir('node_modules', function (er, dir) {
+ if (er) return cb()
+ var deps = {}
+ var n = dir.length
+ dir.forEach(function (d) {
+ if (d.match(/^\./)) return next()
+ if (test !== isTestPkg(d))
+ return next()
+
+ var dp = path.join(dirname, 'node_modules', d, 'package.json')
+ fs.readFile(dp, 'utf8', function (er, p) {
+ if (er) return next()
+ try { p = JSON.parse(p) }
+ catch (e) { return next() }
+ if (!p.version) return next()
+ deps[d] = '~' + p.version
+ return next()
+ })
+ })
+ function next () {
+ if (--n === 0) return cb(null, deps)
+ }
+ })
+}}
+
+
+exports.name = prompt('name', package.name || basename)
+exports.version = prompt('version', package.version || '0.0.0')
+
+if (!package.main) {
+ exports.main = function (cb) {
+ fs.readdir(dirname, function (er, f) {
+ if (er) f = []
+
+ f = f.filter(function (f) {
+ return f.match(/\.js$/)
+ })
+
+ if (f.indexOf('index.js') !== -1)
+ f = 'index.js'
+ else if (f.indexOf('main.js') !== -1)
+ f = 'main.js'
+ else if (f.indexOf(basename + '.js') !== -1)
+ f = basename + '.js'
+ else
+ f = f[0]
+
+ return cb(null, prompt('entry point', f || 'index.js'))
+ })
+ }
+}
+
+if (!package.bin) {
+ exports.bin = function (cb) {
+ fs.readdir(path.resolve(dirname, 'bin'), function (er, d) {
+ // no bins
+ if (er) return cb()
+ // just take the first js file we find there, or nada
+ return cb(null, d.filter(function (f) {
+ return f.match(/\.js$/)
+ })[0])
+ })
+ }
+}
+
+exports.directories = function (cb) {
+ fs.readdir(dirname, function (er, dirs) {
+ if (er) return cb(er)
+ var res = {}
+ dirs.forEach(function (d) {
+ switch (d) {
+ case 'example': case 'examples': return res.example = d
+ case 'test': case 'tests': return res.test = d
+ case 'doc': case 'docs': return res.doc = d
+ case 'man': return res.man = d
+ }
+ })
+ if (Object.keys(res).length === 0) res = undefined
+ return cb(null, res)
+ })
+}
+
+if (!package.dependencies) {
+ exports.dependencies = readDeps(false)
+}
+
+if (!package.devDependencies) {
+ exports.devDependencies = readDeps(true)
+}
+
+// MUST have a test script!
+var s = package.scripts || {}
+var notest = 'echo "Error: no test specified" && exit 1'
+if (!package.scripts) {
+ exports.scripts = function (cb) {
+ fs.readdir(path.join(dirname, 'node_modules'), function (er, d) {
+ setupScripts(d || [], cb)
+ })
+ }
+}
+function setupScripts (d, cb) {
+ // check to see what framework is in use, if any
+ function tx (test) {
+ return test || notest
+ }
+
+ if (!s.test || s.test === notest) {
+ if (d.indexOf('tap') !== -1)
+ s.test = prompt('test command', 'tap test/*.js', tx)
+ else if (d.indexOf('expresso') !== -1)
+ s.test = prompt('test command', 'expresso test', tx)
+ else if (d.indexOf('mocha') !== -1)
+ s.test = prompt('test command', 'mocha', tx)
+ else
+ s.test = prompt('test command', tx)
+ }
+
+ return cb(null, s)
+}
+
+if (!package.repository) {
+ exports.repository = function (cb) {
+ fs.readFile('.git/config', 'utf8', function (er, gconf) {
+ if (er || !gconf) return cb(null, prompt('git repository'))
+
+ gconf = gconf.split(/\r?\n/)
+ var i = gconf.indexOf('[remote "origin"]')
+ if (i !== -1) {
+ var u = gconf[i + 1]
+ if (!u.match(/^\s*url =/)) u = gconf[i + 2]
+ if (!u.match(/^\s*url =/)) u = null
+ else u = u.replace(/^\s*url = /, '')
+ }
+ if (u && u.match(/^git@github.com:/))
+ u = u.replace(/^git@github.com:/, 'git://github.com/')
+
+ return cb(null, prompt('git repository', u))
+ })
+ }
+}
+
+if (!package.keywords) {
+ exports.keywords = prompt('keywords', function (s) {
+ if (!s) return undefined
+ if (Array.isArray(s)) s = s.join(' ')
+ if (typeof s !== 'string') return s
+ return s.split(/[\s,]+/)
+ })
+}
+
+if (!package.author) {
+ exports.author = config['init.author.name']
+ ? {
+ "name" : config['init.author.name'],
+ "email" : config['init.author.email'],
+ "url" : config['init.author.url']
+ }
+ : prompt('author')
+}
+
+exports.license = prompt('license', 'BSD')
View
14 node_modules/init-package-json/example.js
@@ -0,0 +1,14 @@
+var init = require('./init-package-json.js')
+var path = require('path')
+var initFile = path.resolve(process.env.HOME, '.npm-init')
+var dir = process.cwd()
+
+var npm = require('npm')
+npm.load(function (er, npm) {
+ if (er) throw er
+ init(dir, initFile, npm.config.get(), function (er, data) {
+ if (er) throw er
+ console.log('written successfully')
+ })
+})
+
View
104 node_modules/init-package-json/init-package-json.js
@@ -0,0 +1,104 @@
+
+module.exports = init
+
+var PZ = require('promzard').PromZard
+var path = require('path')
+var def = require.resolve('./default-input.js')
+
+var fs = require('fs')
+var semver = require('semver')
+var read = require('read')
+
+// to validate the data object at the end as a worthwhile package
+// and assign default values for things.
+// readJson.extras(file, data, cb)
+var readJson = require('read-package-json')
+
+function init (dir, input, config, cb) {
+ if (typeof config === 'function')
+ cb = config, config = {}
+ var package = path.resolve(dir, 'package.json')
+ input = path.resolve(input)
+ var pkg
+ var ctx = {}
+
+ var es = readJson.extraSet
+ readJson.extraSet = es.filter(function (fn) {
+ return fn.name !== 'authors' && fn.name !== 'mans'
+ })
+ readJson(package, function (er, d) {
+ readJson.extraSet = es
+
+ if (er) pkg = {}
+ else pkg = d
+
+ ctx.filename = package
+ ctx.dirname = path.dirname(package)
+ ctx.basename = path.basename(ctx.dirname)
+ if (!pkg.version || !semver.valid(pkg.version))
+ delete pkg.version
+
+ ctx.package = pkg
+ ctx.config = config || {}
+
+ // make sure that the input is valid.
+ // if not, use the default
+ var pz = new PZ(input, ctx)
+ pz.backupFile = def
+ pz.on('error', cb)
+ pz.on('data', function (data) {
+ Object.keys(data).forEach(function (k) {
+ if (data[k] !== undefined && data[k] !== null) pkg[k] = data[k]
+ })
+
+ // only do a few of these.
+ // no need for mans or contributors if they're in the files
+ var es = readJson.extraSet
+ readJson.extraSet = es.filter(function (fn) {
+ return fn.name !== 'authors' && fn.name !== 'mans'
+ })
+ readJson.extras(package, pkg, function (er, pkg) {
+ readJson.extraSet = es
+ if (er) return cb(er, pkg)
+ pkg = unParsePeople(pkg)
+ // no need for the readme now.
+ delete pkg.readme
+ // really don't want to have this lying around in the file
+ delete pkg._id
+ var d = JSON.stringify(pkg, null, 2) + '\n'
+ console.log('About to write to %s:\n\n%s\n', package, d)
+ read({prompt:'Is this ok? ', default: 'yes'}, function (er, ok) {
+ if (!ok || ok.toLowerCase().charAt(0) !== 'y') {
+ console.log('Aborted.')
+ } else {
+ fs.writeFile(package, d, 'utf8', function (er) {
+ return cb(er, pkg)
+ })
+ }
+ })
+ })
+ })
+ })
+
+}
+
+// turn the objects into somewhat more humane strings.
+function unParsePeople (data) {
+ if (data.author) data.author = unParsePerson(data.author)
+ ;["maintainers", "contributors"].forEach(function (set) {
+ if (!Array.isArray(data[set])) return;
+ data[set] = data[set].map(unParsePerson)
+ })
+ return data
+}
+
+function unParsePerson (person) {
+ if (typeof person === "string") return person
+ var name = person.name || ""
+ var u = person.url || person.web
+ var url = u ? (" ("+u+")") : ""
+ var e = person.email || person.mail
+ var email = e ? (" <"+e+">") : ""
+ return name+email+url
+}
+
View
1 node_modules/init-package-json/node_modules/promzard/.npmignore
@@ -0,0 +1 @@
+example/npm-init/package.json
View
130 node_modules/init-package-json/node_modules/promzard/README.md
@@ -0,0 +1,130 @@
+# promzard
+
+A reimplementation of @SubStack's
+[prompter](https://github.com/substack/node-prompter), which does not
+use AST traversal.
+
+From another point of view, it's a reimplementation of
+[@Marak](https://github.com/marak)'s
+[wizard](https://github.com/Marak/wizard) which doesn't use schemas.
+
+The goal is a nice drop-in enhancement for `npm init`.
+
+## Usage
+
+```javascript
+var promzard = require('promzard')
+promzard(inputFile, optionalContextAdditions, function (er, data) {
+ // .. you know what you doing ..
+})
+```
+
+In the `inputFile` you can have something like this:
+
+```javascript
+var fs = require('fs')
+module.exports = {
+ "greeting": prompt("Who shall you greet?", "world", function (who) {
+ return "Hello, " + who
+ }),
+ "filename": __filename,
+ "directory": function (cb) {
+ fs.readdir(__dirname, cb)
+ }
+}
+```
+
+When run, promzard will display the prompts and resolve the async
+functions in order, and then either give you an error, or the resolved
+data, ready to be dropped into a JSON file or some other place.
+
+
+### promzard(inputFile, ctx, callback)
+
+The inputFile is just a node module. You can require() things, set
+module.exports, etc. Whatever that module exports is the result, and it
+is walked over to call any functions as described below.
+
+The only caveat is that you must give PromZard the full absolute path
+to the module (you can get this via Node's `require.resolve`.) Also,
+the `prompt` function is injected into the context object, so watch out.
+
+Whatever you put in that `ctx` will of course also be available in the
+module. You can get quite fancy with this, passing in existing configs
+and so on.
+
+### Class: promzard.PromZard(file, ctx)
+
+Just like the `promzard` function, but the EventEmitter that makes it
+all happen. Emits either a `data` event with the data, or a `error`
+event if it blows up.
+
+If `error` is emitted, then `data` never will be.
+
+### prompt(...)
+
+In the promzard input module, you can call the `prompt` function.
+This prompts the user to input some data. The arguments are interpreted
+based on type:
+
+1. `string` The first string encountered is the prompt. The second is
+ the default value.
+2. `function` A transformer function which receives the data and returns
+ something else. More than meets the eye.
+3. `object` The `prompt` member is the prompt, the `default` member is
+ the default value, and the `transform` is the transformer.
+
+Whatever the final value is, that's what will be put on the resulting
+object.
+
+### Functions
+
+If there are any functions on the promzard input module's exports, then
+promzard will call each of them with a callback. This way, your module
+can do asynchronous actions if necessary to validate or ascertain
+whatever needs verification.
+
+The functions are called in the context of the ctx object, and are given
+a single argument, which is a callback that should be called with either
+an error, or the result to assign to that spot.
+
+In the async function, you can also call prompt() and return the result
+of the prompt in the callback.
+
+For example, this works fine in a promzard module:
+
+```
+exports.asyncPrompt = function (cb) {
+ fs.stat(someFile, function (er, st) {
+ // if there's an error, no prompt, just error
+ // otherwise prompt and use the actual file size as the default
+ cb(er, prompt('file size', st.size))
+ })
+}
+```
+
+You can also return other async functions in the async function
+callback. Though that's a bit silly, it could be a handy way to reuse
+functionality in some cases.
+
+### Sync vs Async
+
+The `prompt()` function is not synchronous, though it appears that way.
+It just returns a token that is swapped out when the data object is
+walked over asynchronously later, and returns a token.
+
+For that reason, prompt() calls whose results don't end up on the data
+object are never shown to the user. For example, this will only prompt
+once:
+
+```
+exports.promptThreeTimes = prompt('prompt me once', 'shame on you')
+exports.promptThreeTimes = prompt('prompt me twice', 'um....')
+exports.promptThreeTimes = prompt('you cant prompt me again')
+```
+
+### Isn't this exactly the sort of 'looks sync' that you said was bad about other libraries?
+
+Yeah, sorta. I wouldn't use promzard for anything more complicated than
+a wizard that spits out prompts to set up a config file or something.
+Maybe there are other use cases I haven't considered.
View
11 node_modules/init-package-json/node_modules/promzard/example/index.js
@@ -0,0 +1,11 @@
+var pz = require('../promzard')
+
+var path = require('path')
+var file = path.resolve(__dirname, 'substack-input.js')
+var ctx = { basename: path.basename(path.dirname(file)) }
+
+pz(file, ctx, function (er, res) {
+ if (er)
+ throw er
+ console.error(JSON.stringify(res, null, 2))
+})
View
8 node_modules/init-package-json/node_modules/promzard/example/npm-init/README.md
@@ -0,0 +1,8 @@
+# npm-init
+
+An initter you init wit, innit?
+
+## More stuff here
+
+Blerp derp herp lerg borgle pop munch efemerate baz foo a gandt synergy
+jorka chatt slurm.
View
191 node_modules/init-package-json/node_modules/promzard/example/npm-init/init-input.js
@@ -0,0 +1,191 @@
+var fs = require('fs')
+var path = require('path');
+
+module.exports = {
+ "name" : prompt('name',
+ typeof name === 'undefined'
+ ? basename.replace(/^node-|[.-]js$/g, ''): name),
+ "version" : prompt('version', typeof version !== "undefined"
+ ? version : '0.0.0'),
+ "description" : (function () {
+ if (typeof description !== 'undefined' && description) {
+ return description
+ }
+ var value;
+ try {
+ var src = fs.readFileSync('README.md', 'utf8');
+ value = src.split('\n').filter(function (line) {
+ return /\s+/.test(line)
+ && line.trim() !== basename.replace(/^node-/, '')
+ && !line.trim().match(/^#/)
+ ;
+ })[0]
+ .trim()
+ .replace(/^./, function (c) { return c.toLowerCase() })
+ .replace(/\.$/, '')
+ ;
+ }
+ catch (e) {
+ try {
+ // Wouldn't it be nice if that file mattered?
+ var d = fs.readFileSync('.git/description', 'utf8')
+ } catch (e) {}
+ if (d.trim() && !value) value = d
+ }
+ return prompt('description', value);
+ })(),
+ "main" : (function () {
+ var f
+ try {
+ f = fs.readdirSync(dirname).filter(function (f) {
+ return f.match(/\.js$/)
+ })
+ if (f.indexOf('index.js') !== -1)
+ f = 'index.js'
+ else if (f.indexOf('main.js') !== -1)
+ f = 'main.js'
+ else if (f.indexOf(basename + '.js') !== -1)
+ f = basename + '.js'
+ else
+ f = f[0]
+ } catch (e) {}
+
+ return prompt('entry point', f || 'index.js')
+ })(),
+ "bin" : function (cb) {
+ fs.readdir(dirname + '/bin', function (er, d) {
+ // no bins
+ if (er) return cb()
+ // just take the first js file we find there, or nada
+ return cb(null, d.filter(function (f) {
+ return f.match(/\.js$/)
+ })[0])
+ })
+ },
+ "directories" : function (cb) {
+ fs.readdir('.', function (er, dirs) {
+ if (er) return cb(er)
+ var res = {}
+ dirs.forEach(function (d) {
+ switch (d) {
+ case 'example': case 'examples': return res.example = d
+ case 'test': case 'tests': return res.test = d
+ case 'doc': case 'docs': return res.doc = d
+ case 'man': return res.man = d
+ }
+ })
+ if (Object.keys(res).length === 0) res = undefined
+ return cb(null, res)
+ })
+ },
+ "dependencies" : typeof dependencies !== 'undefined' ? dependencies
+ : function (cb) {
+ fs.readdir('node_modules', function (er, dir) {
+ if (er) return cb()
+ var deps = {}
+ var n = dir.length
+ dir.forEach(function (d) {
+ if (d.match(/^\./)) return next()
+ if (d.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/))
+ return next()
+ fs.readFile('node_modules/' + d + '/package.json', function (er, p) {
+ if (er) return next()
+ try { p = JSON.parse(p) } catch (e) { return next() }
+ if (!p.version) return next()
+ deps[d] = '~' + p.version
+ return next()
+ })
+ })
+ function next () {
+ if (--n === 0) return cb(null, deps)
+ }
+ })
+ },
+ "devDependencies" : typeof devDependencies !== 'undefined' ? devDependencies
+ : function (cb) {
+ // same as dependencies but for dev deps
+ fs.readdir('node_modules', function (er, dir) {
+ if (er) return cb()
+ var deps = {}
+ var n = dir.length
+ dir.forEach(function (d) {
+ if (d.match(/^\./)) return next()
+ if (!d.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/))
+ return next()
+ fs.readFile('node_modules/' + d + '/package.json', function (er, p) {
+ if (er) return next()
+ try { p = JSON.parse(p) } catch (e) { return next() }
+ if (!p.version) return next()
+ deps[d] = '~' + p.version
+ return next()
+ })
+ })
+ function next () {
+ if (--n === 0) return cb(null, deps)
+ }
+ })
+ },
+ "scripts" : (function () {
+ // check to see what framework is in use, if any
+ try { var d = fs.readdirSync('node_modules') }
+ catch (e) { d = [] }
+ var s = typeof scripts === 'undefined' ? {} : scripts
+
+ if (d.indexOf('coffee-script') !== -1)
+ s.prepublish = prompt('build command',
+ s.prepublish || 'coffee src/*.coffee -o lib')
+
+ var notest = 'echo "Error: no test specified" && exit 1'
+ function tx (test) {
+ return test || notest
+ }
+
+ if (!s.test || s.test === notest) {
+ if (d.indexOf('tap') !== -1)
+ s.test = prompt('test command', 'tap test/*.js', tx)
+ else if (d.indexOf('expresso') !== -1)
+ s.test = prompt('test command', 'expresso test', tx)
+ else if (d.indexOf('mocha') !== -1)
+ s.test = prompt('test command', 'mocha', tx)
+ else
+ s.test = prompt('test command', tx)
+ }
+
+ return s
+
+ })(),
+
+ "repository" : (function () {
+ try { var gconf = fs.readFileSync('.git/config') }
+ catch (e) { gconf = null }
+ if (gconf) {
+ gconf = gconf.split(/\r?\n/)
+ var i = gconf.indexOf('[remote "origin"]')
+ if (i !== -1) {
+ var u = gconf[i + 1]
+ if (!u.match(/^\s*url =/)) u = gconf[i + 2]
+ if (!u.match(/^\s*url =/)) u = null
+ else u = u.replace(/^\s*url = /, '')
+ }
+ if (u && u.match(/^git@github.com:/))
+ u = u.replace(/^git@github.com:/, 'git://github.com/')
+ }
+
+ return prompt('git repository', u)
+ })(),
+
+ "keywords" : prompt(function (s) {
+ if (!s) return undefined
+ if (Array.isArray(s)) s = s.join(' ')
+ if (typeof s !== 'string') return s
+ return s.split(/[\s,]+/)
+ }),
+ "author" : config['init.author.name']
+ ? {
+ "name" : config['init.author.name'],
+ "email" : config['init.author.email'],
+ "url" : config['init.author.url']
+ }
+ : undefined,
+ "license" : prompt('license', 'BSD')
+}
View
37 node_modules/init-package-json/node_modules/promzard/example/npm-init/init.js
@@ -0,0 +1,37 @@
+var PZ = require('../../promzard').PromZard
+var path = require('path')
+var input = path.resolve(__dirname, 'init-input.js')
+
+var fs = require('fs')
+var package = path.resolve(__dirname, 'package.json')
+var pkg
+
+fs.readFile(package, 'utf8', function (er, d) {
+ if (er) ctx = {}
+ try { ctx = JSON.parse(d); pkg = JSON.parse(d) }
+ catch (e) { ctx = {} }
+
+ ctx.dirname = path.dirname(package)
+ ctx.basename = path.basename(ctx.dirname)
+ if (!ctx.version) ctx.version = undefined
+
+ // this should be replaced with the npm conf object
+ ctx.config = {}
+
+ console.error('ctx=', ctx)
+
+ var pz = new PZ(input, ctx)
+
+ pz.on('data', function (data) {
+ console.error('pz data', data)
+ if (!pkg) pkg = {}
+ Object.keys(data).forEach(function (k) {
+ if (data[k] !== undefined && data[k] !== null) pkg[k] = data[k]
+ })
+ console.error('package data %s', JSON.stringify(data, null, 2))
+ fs.writeFile(package, JSON.stringify(pkg, null, 2), function (er) {
+ if (er) throw er
+ console.log('ok')
+ })
+ })
+})
View
10 node_modules/init-package-json/node_modules/promzard/example/npm-init/package.json
@@ -0,0 +1,10 @@
+{
+ "name": "npm-init",
+ "version": "0.0.0",
+ "description": "an initter you init wit, innit?",
+ "main": "index.js",
+ "scripts": {
+ "test": "asdf"
+ },
+ "license": "BSD"
+}
View
61 node_modules/init-package-json/node_modules/promzard/example/substack-input.js
@@ -0,0 +1,61 @@
+module.exports = {
+ "name" : basename.replace(/^node-/, ''),
+ "version" : "0.0.0",
+ "description" : (function (cb) {
+ var fs = require('fs');
+ var value;
+ try {
+ var src = fs.readFileSync('README.markdown', 'utf8');
+ value = src.split('\n').filter(function (line) {
+ return /\s+/.test(line)
+ && line.trim() !== basename.replace(/^node-/, '')
+ ;
+ })[0]
+ .trim()
+ .replace(/^./, function (c) { return c.toLowerCase() })
+ .replace(/\.$/, '')
+ ;
+ }
+ catch (e) {}
+
+ return prompt('description', value);
+ })(),
+ "main" : prompt('entry point', 'index.js'),
+ "bin" : function (cb) {
+ var path = require('path');
+ var fs = require('fs');
+ var exists = fs.exists || path.exists;
+ exists('bin/cmd.js', function (ex) {
+ var bin
+ if (ex) {
+ var bin = {}
+ bin[basename.replace(/^node-/, '')] = 'bin/cmd.js'
+ }
+ cb(null, bin);
+ });
+ },
+ "directories" : {
+ "example" : "example",
+ "test" : "test"
+ },
+ "dependencies" : {},
+ "devDependencies" : {
+ "tap" : "~0.2.5"
+ },
+ "scripts" : {
+ "test" : "tap test/*.js"
+ },
+ "repository" : {
+ "type" : "git",
+ "url" : "git://github.com/substack/" + basename + ".git"
+ },
+ "homepage" : "https://github.com/substack/" + basename,
+ "keywords" : prompt(function (s) { return s.split(/\s+/) }),
+ "author" : {
+ "name" : "James Halliday",
+ "email" : "mail@substack.net",
+ "url" : "http://substack.net"
+ },
+ "license" : "MIT",
+ "engine" : { "node" : ">=0.6" }
+}
View
26 node_modules/init-package-json/node_modules/promzard/package.json
@@ -0,0 +1,26 @@
+{
+ "author": {
+ "name": "Isaac Z. Schlueter",
+ "email": "i@izs.me",
+ "url": "http://blog.izs.me/"
+ },
+ "name": "promzard",
+ "description": "A reimplementation of @SubStack's",
+ "version": "0.1.5",
+ "repository": {
+ "url": "git://github.com/isaacs/promzard"
+ },
+ "dependencies": {
+ "read": "0"
+ },
+ "devDependencies": {
+ "tap": "~0.2.5"
+ },
+ "main": "promzard.js",
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "readme": "# promzard\n\nA reimplementation of @SubStack's\n[prompter](https://github.com/substack/node-prompter), which does not\nuse AST traversal.\n\nFrom another point of view, it's a reimplementation of\n[@Marak](https://github.com/marak)'s\n[wizard](https://github.com/Marak/wizard) which doesn't use schemas.\n\nThe goal is a nice drop-in enhancement for `npm init`.\n\n## Usage\n\n```javascript\nvar promzard = require('promzard')\npromzard(inputFile, optionalContextAdditions, function (er, data) {\n // .. you know what you doing ..\n})\n```\n\nIn the `inputFile` you can have something like this:\n\n```javascript\nvar fs = require('fs')\nmodule.exports = {\n \"greeting\": prompt(\"Who shall you greet?\", \"world\", function (who) {\n return \"Hello, \" + who\n }),\n \"filename\": __filename,\n \"directory\": function (cb) {\n fs.readdir(__dirname, cb)\n }\n}\n```\n\nWhen run, promzard will display the prompts and resolve the async\nfunctions in order, and then either give you an error, or the resolved\ndata, ready to be dropped into a JSON file or some other place.\n\n\n### promzard(inputFile, ctx, callback)\n\nThe inputFile is just a node module. You can require() things, set\nmodule.exports, etc. Whatever that module exports is the result, and it\nis walked over to call any functions as described below.\n\nThe only caveat is that you must give PromZard the full absolute path\nto the module (you can get this via Node's `require.resolve`.) Also,\nthe `prompt` function is injected into the context object, so watch out.\n\nWhatever you put in that `ctx` will of course also be available in the\nmodule. You can get quite fancy with this, passing in existing configs\nand so on.\n\n### Class: promzard.PromZard(file, ctx)\n\nJust like the `promzard` function, but the EventEmitter that makes it\nall happen. Emits either a `data` event with the data, or a `error`\nevent if it blows up.\n\nIf `error` is emitted, then `data` never will be.\n\n### prompt(...)\n\nIn the promzard input module, you can call the `prompt` function.\nThis prompts the user to input some data. The arguments are interpreted\nbased on type:\n\n1. `string` The first string encountered is the prompt. The second is\n the default value.\n2. `function` A transformer function which receives the data and returns\n something else. More than meets the eye.\n3. `object` The `prompt` member is the prompt, the `default` member is\n the default value, and the `transform` is the transformer.\n\nWhatever the final value is, that's what will be put on the resulting\nobject.\n\n### Functions\n\nIf there are any functions on the promzard input module's exports, then\npromzard will call each of them with a callback. This way, your module\ncan do asynchronous actions if necessary to validate or ascertain\nwhatever needs verification.\n\nThe functions are called in the context of the ctx object, and are given\na single argument, which is a callback that should be called with either\nan error, or the result to assign to that spot.\n\nIn the async function, you can also call prompt() and return the result\nof the prompt in the callback.\n\nFor example, this works fine in a promzard module:\n\n```\nexports.asyncPrompt = function (cb) {\n fs.stat(someFile, function (er, st) {\n // if there's an error, no prompt, just error\n // otherwise prompt and use the actual file size as the default\n cb(er, prompt('file size', st.size))\n })\n}\n```\n\nYou can also return other async functions in the async function\ncallback. Though that's a bit silly, it could be a handy way to reuse\nfunctionality in some cases.\n\n### Sync vs Async\n\nThe `prompt()` function is not synchronous, though it appears that way.\nIt just returns a token that is swapped out when the data object is\nwalked over asynchronously later, and returns a token.\n\nFor that reason, prompt() calls whose results don't end up on the data\nobject are never shown to the user. For example, this will only prompt\nonce:\n\n```\nexports.promptThreeTimes = prompt('prompt me once', 'shame on you')\nexports.promptThreeTimes = prompt('prompt me twice', 'um....')\nexports.promptThreeTimes = prompt('you cant prompt me again')\n```\n\n### Isn't this exactly the sort of 'looks sync' that you said was bad about other libraries?\n\nYeah, sorta. I wouldn't use promzard for anything more complicated than\na wizard that spits out prompts to set up a config file or something.\nMaybe there are other use cases I haven't considered.\n",
+ "_id": "promzard@0.1.5",
+ "_from": "promzard@~0.1.5"
+}
View
216 node_modules/init-package-json/node_modules/promzard/promzard.js
@@ -0,0 +1,216 @@
+module.exports = promzard
+promzard.PromZard = PromZard
+
+var fs = require('fs')
+var vm = require('vm')
+var util = require('util')
+var files = {}
+var crypto = require('crypto')
+var EventEmitter = require('events').EventEmitter
+var read = require('read')
+
+var Module = require('module').Module
+var path = require('path')
+
+function promzard (file, ctx, cb) {
+ if (typeof ctx === 'function') cb = ctx, ctx = null;
+ if (!ctx) ctx = {};
+ var pz = new PromZard(file, ctx)
+ pz.on('error', cb)
+ pz.on('data', function (data) {
+ cb(null, data)
+ })
+}
+
+function PromZard (file, ctx) {
+ if (!(this instanceof PromZard))
+ return new PromZard(file, ctx)
+ EventEmitter.call(this)
+ this.file = file
+ this.ctx = ctx
+ this.unique = crypto.randomBytes(8).toString('hex')
+ this.load()
+}
+
+PromZard.prototype = Object.create(
+ EventEmitter.prototype,
+ { constructor: {
+ value: PromZard,
+ readable: true,
+ configurable: true,
+ writable: true,
+ enumerable: false } } )
+
+PromZard.prototype.load = function () {
+ if (files[this.file])
+ return this.loaded()
+
+ fs.readFile(this.file, 'utf8', function (er, d) {
+ if (er && this.backupFile) {
+ this.file = this.backupFile
+ delete this.backupFile
+ return this.load()
+ }
+ if (er)
+ return this.emit('error', this.error = er)
+ files[this.file] = d
+ this.loaded()
+ }.bind(this))
+}
+
+PromZard.prototype.loaded = function () {
+ this.ctx.prompt = this.makePrompt()
+ this.ctx.__filename = this.file
+ this.ctx.__dirname = path.dirname(this.file)
+ this.ctx.__basename = path.basename(this.file)
+ var mod = this.ctx.module = this.makeModule()
+ this.ctx.require = function (path) {
+ return mod.require(path)
+ }
+ this.ctx.require.resolve = function(path) {
+ return Module._resolveFilename(path, mod);
+ }
+ this.ctx.exports = mod.exports
+
+ this.script = this.wrap(files[this.file])
+ var fn = vm.runInThisContext(this.script, this.file)
+ var args = Object.keys(this.ctx).map(function (k) {
+ return this.ctx[k]
+ }.bind(this))
+ try { var res = fn.apply(this.ctx, args) }
+ catch (er) { this.emit('error', er) }
+ if (res &&
+ typeof res === 'object' &&
+ exports === mod.exports &&
+ Object.keys(exports).length === 1) {
+ this.result = res
+ } else {
+ this.result = mod.exports
+ }
+ this.walk()
+}
+
+PromZard.prototype.makeModule = function () {
+ var mod = new Module(this.file, module)
+ mod.loaded = true
+ mod.filename = this.file
+ mod.id = this.file
+ mod.paths = Module._nodeModulePaths(path.dirname(this.file))
+ return mod
+}
+
+PromZard.prototype.wrap = function (body) {
+ var s = '(function( %s ) { %s\n })'
+ var args = Object.keys(this.ctx).join(', ')
+ return util.format(s, args, body)
+}
+
+PromZard.prototype.makePrompt = function () {
+ this.prompts = []
+ return prompt.bind(this)
+ function prompt () {
+ var p, d, t
+ for (var i = 0; i < arguments.length; i++) {
+ var a = arguments[i]
+ if (typeof a === 'string' && p)
+ d = a
+ else if (typeof a === 'string')
+ p = a
+ else if (typeof a === 'function')
+ t = a
+ else if (a && typeof a === 'object') {
+ p = a.prompt || p
+ d = a.default || d
+ t = a.tranform || t
+ }
+ }
+
+ try { return this.unique + '-' + this.prompts.length }
+ finally { this.prompts.push([p, d, t]) }
+ }
+}
+
+PromZard.prototype.walk = function (o, cb) {
+ o = o || this.result
+ cb = cb || function (er, res) {
+ if (er)
+ return this.emit('error', this.error = er)
+ this.result = res
+ return this.emit('data', res)
+ }
+ cb = cb.bind(this)
+ var keys = Object.keys(o)
+ var i = 0
+ var len = keys.length
+
+ L.call(this)
+ function L () {
+ if (this.error)
+ return
+ while (i < len) {
+ var k = keys[i]
+ var v = o[k]
+ i++
+
+ if (v && typeof v === 'object') {
+ return this.walk(v, function (er, res) {
+ if (er) return cb(er)
+ o[k] = res
+ L.call(this)
+ }.bind(this))
+ } else if (v &&
+ typeof v === 'string' &&
+ v.indexOf(this.unique) === 0) {
+ var n = +v.substr(this.unique.length + 1)
+ var prompt = this.prompts[n]
+ if (isNaN(n) || !prompt)
+ continue
+
+ // default to the key
+ if (undefined === prompt[0])
+ prompt[0] = k
+
+ // default to the ctx value, if there is one
+ if (undefined === prompt[1])
+ prompt[1] = this.ctx[k]
+
+ return this.prompt(prompt, function (er, res) {
+ if (er)
+ return this.emit('error', this.error = er);
+ o[k] = res
+ L.call(this)
+ }.bind(this))
+ } else if (typeof v === 'function') {
+ try { return v.call(this.ctx, function (er, res) {
+ if (er)
+ return this.emit('error', this.error = er)
+ o[k] = res
+ // back up so that we process this one again.
+ // this is because it might return a prompt() call in the cb.
+ i --
+ L.call(this)
+ }.bind(this)) }
+ catch (er) { this.emit('error', er) }
+ }
+ }
+ // made it to the end of the loop, maybe
+ if (i >= len)
+ return cb(null, o)
+ }
+}
+
+PromZard.prototype.prompt = function (pdt, cb) {
+ var prompt = pdt[0]
+ var def = pdt[1]
+ var tx = pdt[2]
+
+ if (tx) {
+ cb = function (cb) { return function (er, data) {
+ try { return cb(er, tx(data)) }
+ catch (er) { this.emit('error', er) }
+ }}(cb).bind(this)
+ }
+
+ read({ prompt: prompt + ': ' , default: def }, cb)
+}
+
View
83 node_modules/init-package-json/node_modules/promzard/test/basic.js
@@ -0,0 +1,83 @@
+var tap = require('tap')
+var pz = require('../promzard.js')
+var spawn = require('child_process').spawn
+
+tap.test('run the example', function (t) {
+
+ var example = require.resolve('../example/index.js')
+ var node = process.execPath
+
+ var expect = {
+ "name": "example",
+ "version": "0.0.0",
+ "description": "testing description",
+ "main": "test-entry.js",
+ "directories": {
+ "example": "example",
+ "test": "test"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "tap": "~0.2.5"
+ },
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/substack/example.git"
+ },
+ "homepage": "https://github.com/substack/example",
+ "keywords": [
+ "fugazi",
+ "function",
+ "waiting",
+ "room"
+ ],
+ "author": {
+ "name": "James Halliday",
+ "email": "mail@substack.net",
+ "url": "http://substack.net"
+ },
+ "license": "MIT",
+ "engine": {
+ "node": ">=0.6"
+ }
+ }
+
+ console.error('%s %s', node, example)
+ var c = spawn(node, [example], { customFds: [-1,-1,-1] })
+ var output = ''
+ c.stdout.on('data', function (d) {
+ output += d
+ respond()
+ })
+
+ var actual = ''
+ c.stderr.on('data', function (d) {
+ actual += d
+ })
+
+ function respond () {
+ console.error('respond', output)
+ if (output.match(/description: $/)) {
+ c.stdin.write('testing description\n')
+ return
+ }
+ if (output.match(/entry point: \(index\.js\) $/)) {
+ c.stdin.write('test-entry.js\n')
+ return
+ }
+ if (output.match(/keywords: $/)) {
+ c.stdin.write('fugazi function waiting room\n')
+ return
+ }
+ }
+
+ c.on('close', function () {
+ console.error('actual', actual)
+ actual = JSON.parse(actual)
+ t.deepEqual(actual, expect)
+ t.end()
+ })
+})
View
5 node_modules/init-package-json/node_modules/promzard/test/exports.input
@@ -0,0 +1,5 @@
+exports.a = 1 + 2
+exports.b = prompt('To be or not to be?', '!2b')
+exports.c = {}
+exports.c.x = prompt()
+exports.c.y = tmpdir + "/y/file.txt"
View
30 node_modules/init-package-json/node_modules/promzard/test/exports.js
@@ -0,0 +1,30 @@
+var test = require('tap').test;
+var promzard = require('../');
+
+test('exports', function (t) {
+ t.plan(1);
+
+ var ctx = { tmpdir : '/tmp' }
+ var file = __dirname + '/exports.input';
+ promzard(file, ctx, function (err, output) {
+ t.same(
+ {
+ a : 3,
+ b : '!2b',
+ c : {
+ x : 55,
+ y : '/tmp/y/file.txt',
+ }
+ },
+ output
+ );
+ });
+
+ setTimeout(function () {
+ process.stdin.emit('data', '\n');
+ }, 100);
+
+ setTimeout(function () {
+ process.stdin.emit('data', '55\n');
+ }, 200);
+});
View
18 node_modules/init-package-json/node_modules/promzard/test/fn.input
@@ -0,0 +1,18 @@
+var fs = require('fs')
+
+module.exports = {
+ "a": 1 + 2,
+ "b": prompt('To be or not to be?', '!2b', function (s) {
+ return s.toUpperCase() + '...'
+ }),
+ "c": {
+ "x": prompt(function (x) { return x * 100 }),
+ "y": tmpdir + "/y/file.txt"
+ },
+ a_function: function (cb) {
+ fs.readFile(__filename, 'utf8', cb)
+ },
+ asyncPrompt: function (cb) {
+ return cb(null, prompt('a prompt at any other time would smell as sweet'))
+ }
+}
View
39 node_modules/init-package-json/node_modules/promzard/test/fn.js
@@ -0,0 +1,39 @@
+var test = require('tap').test;
+var promzard = require('../');
+var fs = require('fs')
+
+test('prompt callback param', function (t) {
+ t.plan(1);
+
+ var ctx = { tmpdir : '/tmp' }
+ var file = __dirname + '/fn.input';
+ promzard(file, ctx, function (err, output) {
+ var expect =
+ {
+ a : 3,
+ b : '!2B...',
+ c : {
+ x : 5500,
+ y : '/tmp/y/file.txt',
+ }
+ }
+ expect.a_function = fs.readFileSync(file, 'utf8')
+ expect.asyncPrompt = 'async prompt'
+ t.same(
+ output,
+ expect
+ );
+ });
+
+ setTimeout(function () {
+ process.stdin.emit('data', '\n');
+ }, 100);
+
+ setTimeout(function () {
+ process.stdin.emit('data', '55\n');
+ }, 200);
+
+ setTimeout(function () {
+ process.stdin.emit('data', 'async prompt')
+ }, 300)
+});
View
8 node_modules/init-package-json/node_modules/promzard/test/simple.input
@@ -0,0 +1,8 @@
+module.exports = {
+ "a": 1 + 2,
+ "b": prompt('To be or not to be?', '!2b'),
+ "c": {
+ "x": prompt(),
+ "y": tmpdir + "/y/file.txt"
+ }
+}
View
30 node_modules/init-package-json/node_modules/promzard/test/simple.js
@@ -0,0 +1,30 @@
+var test = require('tap').test;
+var promzard = require('../');
+
+test('simple', function (t) {
+ t.plan(1);
+
+ var ctx = { tmpdir : '/tmp' }
+ var file = __dirname + '/simple.input';
+ promzard(file, ctx, function (err, output) {
+ t.same(
+ {
+ a : 3,
+ b : '!2b',
+ c : {
+ x : 55,
+ y : '/tmp/y/file.txt',
+ }
+ },
+ output
+ );
+ });
+
+ setTimeout(function () {
+ process.stdin.emit('data', '\n');
+ }, 100);
+
+ setTimeout(function () {
+ process.stdin.emit('data', '55\n');
+ }, 200);
+});
View
42 node_modules/init-package-json/package.json
@@ -0,0 +1,42 @@
+{
+ "name": "init-package-json",
+ "version": "0.0.2",
+ "main": "init-package-json.js",
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/isaacs/init-package-json"
+ },
+ "author": {
+ "name": "Isaac Z. Schlueter",
+ "email": "i@izs.me",
+ "url": "http://blog.izs.me/"
+ },
+ "license": "BSD",
+ "description": "A node module to get your node module started",
+ "dependencies": {
+ "promzard": "~0.1.5",
+ "read": "~0.1.0",
+ "read-package-json": "0.0.6",
+ "semver": "~1.0.14"
+ },
+ "devDependencies": {
+ "tap": "~0.2.5",
+ "rimraf": "~2.0.2"
+ },
+ "keywords": [
+ "init",
+ "package.json",
+ "package",
+ "helper",
+ "wizard",
+ "wizerd",
+ "prompt",
+ "start"
+ ],
+ "readme": "# init-package-json\n\nA node module to get your node module started.\n\n## Usage\n\n```javascript\nvar init = require('init-package-json')\nvar path = require('path')\n\n// a path to a promzard module. In the event that this file is\n// not found, one will be provided for you.\nvar initFile = path.resolve(process.env.HOME, '.npm-init')\n\n// the dir where we're doin stuff.\nvar dir = process.cwd()\n\n// extra stuff that gets put into the PromZard module's context.\n// In npm, this is the resolved config object. Exposed as 'config'\n// Optional.\nvar configData = { some: 'extra stuff' }\n\n// Any existing stuff from the package.json file is also exposed in the\n// PromZard module as the `package` object. There will also be free\n// vars for:\n// * `filename` path to the package.json file\n// * `basename` the tip of the package dir\n// * `dirname` the parent of the package dir\n\ninit(dir, initFile, configData, function (er, data) {\n // the data's already been written to {dir}/package.json\n // now you can do stuff with it\n})\n```\n\nOr from the command line:\n\n```\n$ npm-init\n```\n\nSee [PromZard](https://github.com/isaacs/promzard) for details about\nwhat can go in the config file.\n",
+ "_id": "init-package-json@0.0.2",
+ "_from": "init-package-json@0"
+}
View
15 node_modules/init-package-json/test/basic.input
@@ -0,0 +1,15 @@
+var assert = require('assert')
+
+exports.name = prompt('name', package.name || basename)
+exports.version = '1.2.5'
+exports.description = prompt('description', package.description)
+exports.author = 'me <em@i.l> (http://url)'
+exports.scripts = package.scripts || {}
+exports.scripts.test = 'make test'
+exports.main = package.main || 'main.js'
+exports.config = JSON.parse(JSON.stringify(config))
+try {delete exports.config.config}catch(e){}
+try {delete exports.package.config}catch(e){}
+try {delete exports.package.package}catch(e){}
+try {delete exports.config.package}catch(e){}
+exports.package = JSON.parse(JSON.stringify(package))
View
35 node_modules/init-package-json/test/basic.js
@@ -0,0 +1,35 @@
+var tap = require('tap')
+var init = require('../')
+var rimraf = require('rimraf')
+
+tap.test('the basics', function (t) {
+ var i = __dirname + '/basic.input'
+ var dir = __dirname
+ init(dir, i, {foo:'bar'}, function (er, data) {
+ if (er) throw er
+ var expect =
+ { name: 'the-name',
+ version: '1.2.5',
+ description: 'description',
+ author: 'me <em@i.l> (http://url)',
+ scripts: { test: 'make test' },
+ main: 'main.js',
+ config: { foo: 'bar' },
+ package: {} }
+ t.same(data, expect)
+ t.end()
+ })
+ setTimeout(function () {
+ process.stdin.emit('data', 'the-name\n')
+ }, 50)
+ setTimeout(function () {
+ process.stdin.emit('data', 'description\n')
+ }, 100)
+ setTimeout(function () {
+ process.stdin.emit('data', 'yes\n')
+ }, 150)
+})
+
+tap.test('teardown', function (t) {
+ rimraf(__dirname + '/package.json', t.end.bind(t))
+})
View
6 package.json
@@ -63,7 +63,8 @@
"npm-registry-client": "0",
"read-package-json": "0",
"read-installed": "0",
- "glob": "~3.1.9"
+ "glob": "~3.1.9",
+ "init-package-json": "0"
},
"bundleDependencies": [
"slide",
@@ -95,7 +96,8 @@
"npm-registry-client",
"read-package-json",
"read-installed",
- "glob"
+ "glob",
+ "init-package-json"
],
"devDependencies": {
"ronn": "https://github.com/isaacs/ronnjs/tarball/master"

0 comments on commit 7f4f404

Please sign in to comment.