-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.js
57 lines (50 loc) · 1.78 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env node
var common = require('./archetype').common,
opt = require('optimist'),
pwd = process.env.PWD,
util = require('util');
var create_dir = function (path) {
console.log('creating directory: '+path);
common.dir.create(path);
};
var create_file = function (src, dst, re, str) {
console.log('creating file: '+dst);
common.file.writeFile(dst, common.file.read(src).replace(re, str));
};
var commands = {
create_module: function (name, others) {
['model', 'route', 'service'].forEach(function (e, i) {
create_file(__dirname+'/lib/module/'+e+'.js', pwd+'/'+e+'s/'+name+'.js', /%MODULE%/g, name);
});
create_file(__dirname+'/lib/module/template.html', pwd+'/templates/'+name+'_index.html', '', '');
},
create_project: function (name, others) {
create_dir(pwd+'/'+name);
['models', 'routes', 'services', 'templates'].forEach(function (e, i) {
create_dir(pwd+'/'+name+'/'+e);
});
['option', 'server'].forEach(function (e, i) {
create_file(__dirname+'/lib/project/'+e+'.js', pwd+'/'+name+'/'+e+'.js', /%PROJECT%/g, name);
});
}
};
(function () {
var args = opt.argv._.slice(), min = 2, valid = false;
if (args.length >= min) {
var key = args.shift().replace(/-/, '_');
if (typeof(commands[key]) == 'function') {
var value = args.shift(), others = {}, valid = true;
console.log(key+' called with '+value+' and '+util.inspect(others));
commands[key](value, others);
}
}
if (!valid)
opt.usage(
'archetype: command-line interface to the archetype web framework for Node.js\n' +
'Usage:\n' +
' $0 [ options ] [ action ]\n' +
'Options: []\n' +
'Actions: [ create-project PROJECT ]\n' +
' [ create-module MODULE ]\n'
).demandCount(min).demand(['A valid action is required.']);
})();