Comparing version 0.0.2 to 0.0.3
@@ -1,202 +0,86 @@ | ||
#!/usr/bin/env node | ||
exports = module.exports = function(args) { | ||
console.log(args); | ||
}; | ||
var fs = require('fs'), | ||
path = require('path'); | ||
path = require('path'), | ||
mkdirp = require('mkdirp'), | ||
wash = require('wash'), | ||
exec = require('child_process').exec, | ||
_ = require('underscore'); | ||
// commandline: node d5 {command} {args} | ||
var commandName = process.argv[2]; | ||
var args = process.argv.slice(3); | ||
var yargs = require('yargs') | ||
.option('t', { alias: 'template', default: 'node', describe: 'template type: node' }) | ||
.option('p', { alias: 'path', default: '.', describe: 'parent directory to create a project directory under' }) | ||
.option('d', { alias: 'desc', default: '', describe: 'project description '}); | ||
var command = require('./command/' + commandName); | ||
var projectName = null; | ||
var argv = null; | ||
var templateContext = {}; | ||
// find in alias | ||
function setupContext() { | ||
for(var key in settings) { | ||
var val = settings[key]; | ||
var type = cmd = args[0] | ||
type = alias[type] || type | ||
// project name | ||
var name = args[1] | ||
// rest arguments | ||
var rest = args.slice(2) | ||
// project description | ||
var desc = rest.join(' ') | ||
// modify/fetch settings | ||
if ('set' === cmd) { | ||
set(name, rest.join(' ')) | ||
return | ||
if(_.isString(val)) { | ||
templateContext[key] = val | ||
.replace('$(projectName)', projectName) | ||
.replace('$(projectDesc)', argv.d); | ||
} else { | ||
templateContext[key] = val; | ||
} | ||
} | ||
} | ||
if ('get' === cmd) { | ||
console.log(settings[name]) | ||
return | ||
function writeFile(srcFile, destPath) { | ||
var srcContents = fs.readFileSync(srcFile, { encoding: 'utf8' }); | ||
var rendered = wash.render(srcContents, templateContext); | ||
fs.writeFileSync(destPath, rendered); | ||
} | ||
/** | ||
* Replaces local %vars% in string. | ||
* | ||
* @param {string} str | ||
* @param {object} locals | ||
* @return {string} | ||
*/ | ||
function copyDir(srcDir, destDir) { | ||
mkdirp.sync(destDir); | ||
function render (str, locals) { | ||
locals = merge(locals, render.locals) | ||
str = str.replace(/%([_a-z0-9]*)%/gi, function (_, key) { | ||
return locals[key] || '' | ||
}) | ||
return str | ||
} | ||
var entries = fs.readdirSync(srcDir); | ||
for(var i in entries) { | ||
var entry = entries[i]; | ||
var srcFile = path.join(srcDir, entry); | ||
var destFile = path.join(destDir, entry); | ||
/** | ||
* Read a dir tree. | ||
* | ||
* @param {string} dirname | ||
* @return {array} dirs | ||
*/ | ||
function readdirtree (dirname, arr) { | ||
arr = arr || [] | ||
fs.readdirSync(dirname) | ||
.map(function (file) { return path.join(dirname, file) }) | ||
.forEach(function (file) { | ||
if (fs.statSync(file).isDirectory()) { | ||
arr.push(file) | ||
readdirtree(file, arr) | ||
} | ||
}) | ||
return arr | ||
var stat = fs.statSync(srcFile); | ||
if(stat.isDirectory()) { | ||
copyDir(srcFile, destFile); | ||
} else { | ||
writeFile(srcFile, destFile); | ||
} | ||
} | ||
} | ||
/** | ||
* Reads the files from a dir. | ||
* | ||
* @param {string} dirname | ||
* @return {array} files | ||
*/ | ||
exports = module.exports = function(args) { | ||
if(args.length == 0) { | ||
console.error('Usage: d5 project {name} [opts ...]'); | ||
process.exit(1); | ||
} | ||
function readdir (dirname, arr) { | ||
arr = arr || [] | ||
fs.readdirSync(dirname) | ||
.map(function (file) { return path.join(dirname, file) }) | ||
.forEach(function (file) { | ||
if (fs.statSync(file).isDirectory()) { | ||
readdir(file, arr) | ||
} | ||
else arr.push(file) | ||
}) | ||
projectName = args[0]; | ||
argv = yargs.parse(args.slice(1)); | ||
return arr | ||
} | ||
setupContext(); | ||
/** | ||
* Reads a file in sync and returns an object. | ||
* | ||
* @param {string} filename | ||
* @return {object} file | ||
*/ | ||
var projectDir = path.join(argv.p, projectName); | ||
if(fs.existsSync(projectDir)) { | ||
console.error('Error: target directory already exists "' + projectDir + '"'); | ||
process.exit(1); | ||
} | ||
function readfile (filename) { | ||
return { name: filename, contents: fs.readFileSync(filename, 'utf8') } | ||
} | ||
var templateDir = path.join(__dirname, '../../templates/project/' + argv.t); | ||
if(!fs.existsSync(templateDir)) { | ||
console.error('Error: template "' + argv.t + '" not available'); | ||
process.exit(1); | ||
} | ||
// read settings | ||
copyDir(templateDir, projectDir); | ||
}; | ||
var isWindows = process.platform === 'win32' | ||
var home = isWindows ? process.env.USERPROFILE : process.env.HOME | ||
var settings | ||
try { settings = require(home+'/project.json') } | ||
catch (e) { settings = require('../defaults.json') } | ||
/** | ||
* Sets key val pair in settings | ||
* and saves it on disk in sync. | ||
* | ||
* @param {string} key | ||
* @param {string} val | ||
*/ | ||
function set (key, val) { | ||
settings[key] = val | ||
var filename = path.join(home, 'project.json') | ||
var contents = JSON.stringify(settings, null, ' ') | ||
fs.writeFileSync(filename, contents, 'utf8') | ||
console.log('set "%s" to "%s"', key, val) | ||
} | ||
// create project from template | ||
var template = path.join(__dirname, '..', 'templates', type) | ||
var target = path.join(process.cwd(), name) | ||
console.log('creating project "%s"', name) | ||
console.log('on path : %s', target) | ||
// populate locals | ||
render.locals = {} | ||
merge(render.locals, settings) | ||
merge(render.locals, { name: name, desc: desc, project: name }) | ||
// make project root | ||
try { fs.mkdirSync(target) } catch(_) {} | ||
// make project dirs | ||
readdirtree(template) | ||
.map(function (dirname) { | ||
dirname = dirname.substr(template.length + 1) | ||
console.log('make dir : %s', dirname) | ||
return path.join(target, dirname) | ||
}) | ||
.forEach(function (dirname) { | ||
try { fs.mkdirSync(dirname, 0755) } catch(_) {} | ||
}) | ||
// make project files | ||
readdir(template) | ||
.map(readfile) | ||
.forEach(function (file) { | ||
var filename = file.name.substr(template.length + 1) | ||
filename = filename | ||
filename = render(filename) | ||
file.contents = render(file.contents) | ||
console.log('write file : %s', filename) | ||
fs.writeFileSync(path.join(target, filename), file.contents, 'utf8') | ||
}) | ||
/** | ||
* Merge two objects. | ||
* | ||
* @param {object} t | ||
* @param {object} s | ||
* @return {object} t | ||
*/ | ||
function merge (t, s) { | ||
t = t || {} | ||
s = s || {} | ||
for (var k in s) { | ||
if (s.hasOwnProperty(k)) { | ||
t[k] = s[k] | ||
} | ||
} | ||
return t | ||
} | ||
exports.help = function() { | ||
console.log('Create a project from templates.'); | ||
console.log('Usage: d5 project {name} [opts ...]'); | ||
console.log(); | ||
yargs.showHelp(); | ||
}; |
@@ -1,29 +0,76 @@ | ||
#!/usr/bin/env node | ||
var fs = require('fs'), | ||
path = require('path'); | ||
path = require('path'), | ||
mkdirp = require('mkdirp'); | ||
/* | ||
var args = process.argv.slice(1); | ||
do { | ||
arg = args.shift(); | ||
} while (fs.realpathSync(arg) !== __filename | ||
&& (base = path.basename(arg)) !== "node-supervisor" | ||
&& base !== "supervisor" | ||
&& base !== "supervisor.js" | ||
) | ||
function getHomeDirectory() { | ||
var isWindows = process.platform === 'win32'; | ||
return isWindows ? process.env.USERPROFILE : process.env.HOME; | ||
} | ||
require("./supervisor").run(args) | ||
function getSettings() { | ||
var settings = null; | ||
// commandline: node d5 {command} {args} | ||
var commandName = process.argv[2]; | ||
var args = process.argv.slice(3); | ||
var targetDir = path.join(getHomeDirectory(), '.d5'); | ||
mkdirp.sync(targetDir); | ||
var targetFilePath = path.join(targetDir, 'defaults.json'); | ||
var command = require('./command/' + commandName); | ||
try { | ||
settings = require(targetFilePath); | ||
} catch(e) { | ||
settings = require('./defaults.json'); | ||
command.run(args); | ||
*/ | ||
var contents = JSON.stringify(settings); | ||
fs.writeFileSync(targetFilePath, contents, 'utf8'); | ||
} | ||
exports.start = function() { | ||
console.log('system args: ' + process.argv); | ||
settings.save = function() { | ||
var contents = JSON.stringify(settings); | ||
fs.writeFileSync(targetFilePath, contents, 'utf8'); | ||
}; | ||
return settings; | ||
} | ||
function loadModule(name) { | ||
try { | ||
return require('./command/' + name); | ||
} catch(e) { | ||
return null; | ||
} | ||
} | ||
exports = module.exports = function(commandArgs) { | ||
if(commandArgs.length == 0) { | ||
console.error('Usage: d5 {command} [command-args ...]'); | ||
process.exit(1); | ||
} | ||
var commandName = commandArgs[0]; | ||
if(commandName === 'help') { | ||
if(commandArgs.length > 1) { | ||
var module = loadModule(commandArgs[1]); | ||
if(module) { | ||
if(module.help) { | ||
module.help(); | ||
} else { | ||
console.error('Error: "help" not available for command "' + commandArgs[1] + '"'); | ||
process.exit(1); | ||
} | ||
} else { | ||
console.error('Error: unknown command "' + commandArgs[1] + '"'); | ||
process.exit(1); | ||
} | ||
} else { | ||
console.log('Usage: d5 {command} [command-args ...]'); | ||
} | ||
} else { | ||
var module = loadModule(commandArgs[0]); | ||
if(module) { | ||
global.settings = getSettings(); | ||
module(commandArgs.slice(1)); | ||
} else { | ||
console.error('Error: unknown command "' + commandArgs[0] + '"'); | ||
process.exit(1); | ||
} | ||
} | ||
}; |
{ | ||
"name": "d5", | ||
"description": "", | ||
"version": "0.0.2", | ||
"description": "small utilities for node development", | ||
"version": "0.0.3", | ||
"author": { | ||
@@ -11,6 +11,9 @@ "name" : "Daniel Kang", | ||
"dependencies": { | ||
"mkdirp": "0.5.x", | ||
"yargs": "1.2.x", | ||
"wash": "0.1.x", | ||
"underscore": "1.6.0" | ||
}, | ||
"bin": "bin/d5", | ||
"bin": "./bin/d5", | ||
"engines": { "node": "*" } | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
8995
15
226
4
8
3
+ Addedmkdirp@0.5.x
+ Addedwash@0.1.x
+ Addedyargs@1.2.x
+ Addedminimist@0.1.01.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedunderscore@1.5.0(transitive)
+ Addedwash@0.1.1(transitive)
+ Addedyargs@1.2.6(transitive)