Skip to content

Commit

Permalink
An example that is like npm init
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jun 13, 2012
1 parent fd121e6 commit 45b1aac
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 0 deletions.
34 changes: 34 additions & 0 deletions example/npm-init/init.js
@@ -0,0 +1,34 @@
var PZ = require('../../promzard').PromZard
var path = require('path')
var input = path.resolve(__dirname, 'init.json')

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 = {}

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]
})
fs.writeFile(package, JSON.stringify(pkg, null, 2), function (er) {
if (er) throw er
console.log('ok')
})
})
})
177 changes: 177 additions & 0 deletions example/npm-init/init.json
@@ -0,0 +1,177 @@
{
"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 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) {
try {
// Wouldn't it be nice if that file mattered?
value = fs.readFileSync('.git/description', 'utf8')
} catch (e) {}
}
return prompt('description', value);
})(),
"main" : (function () {
var fs = require('fs')
var f
try {
f = fs.readdirSync(dirname).filter(function (f) {
return f.match(/\.js$/)
})[0]
} catch (e) {}

return prompt('entry point', f || 'index.js')
})(),
"bin" : function (cb) {
var path = require('path');
var fs = require('fs');
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
}
})
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
var fs = require('fs')
try { var d = fs.readdirSync('node_modules') }
catch (e) { d = null }
var s = typeof scripts === 'undefined' ? {} : scripts

if (!d) {
s.test = s.test || prompt('test command')
return s
}

if (d.indexOf('coffee-script') !== -1)
s.build = prompt('build command', s.build || 'coffee src/*.coffee -o lib')

if (d.indexOf('tap') !== -1)
s.test = prompt('test command', 'tap test/*.js')
else if (d.indexOf('expresso') !== -1)
s.test = prompt('test command', 'expresso test')
else if (d.indexOf('mocha') !== -1)
s.test = prompt('test command', 'mocha')
else
s.test = prompt('test command')
})(),

"repository" : (function () {
var fs = require('fs')
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')
}

0 comments on commit 45b1aac

Please sign in to comment.