forked from killme2008/gen-node-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_module.js
executable file
·44 lines (33 loc) · 1.08 KB
/
gen_module.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
#!/usr/bin/env node
var fs=require('fs');
var path=require('path');
var argv=process.argv;
argv.splice(0,2);
if(argv.length<1)
throw new Error("I need project name");
var name=argv[0];
var parentPath=argv[1] || process.cwd();
var projPath=path.join(parentPath,name);
if(path.existsSync(projPath)){
console.error(projPath+" is exists");
process.reallyExit(1);
}
fs.mkdirSync(projPath,0777);
fs.mkdirSync(path.join(projPath,'lib'),0777);
fs.mkdirSync(path.join(projPath,'test'),0777);
fs.writeFile(path.join(projPath,'lib',name+".js"),'','utf-8',function(err){
if(err) throw err;
});
var author=process.env['USER'] || 'unknow';
function merge(fromPath,targetPath){
fs.readFile(path.join(__dirname,'templates',fromPath),'utf-8',function(err,data){
if(err) throw error;
data=data.replace(/@name/gm,name);
data=data.replace(/@author/gm,author);
fs.writeFile(path.join(projPath,targetPath),data,'utf-8',function(err){
if(err) throw err;
});
});
}
merge('package.json','package.json');
merge('test.js',"test/"+name+'_test.js');