-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (133 loc) · 3.71 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
var shelljs = require('shelljs');
var jsonfile = require('jsonfile')
var colors = require('colors');
var fs = require('fs');
var args = require('args');
const VERSION = require('./package').version;
var options = args.Options.parse([
{
name : 'VERSION',
shortName : 'V',
help : "version of this app",
},
{
name : 'protoName',
shortName : 'n',
help : "prototype name",
},
{
name : 'version',
shortName : 'v',
help : "bare (es6 bare boilerpate), budo/npm, jspm, rje (react + jspm + express)",
defaultValue : 'jspm'
},
{
name : 'help',
shortName : 'h',
help : "show instructions",
}
]);
var initArgs;
if(process.argv.length >= 3)
{
initArgs = process.argv.slice(2);
} else {
console.log("you need to specify a project name".red);
shelljs.exit(1);
}
var protoName, params;
if(initArgs.length == 1)
{
if(initArgs[0] == '-h' || initArgs[0] == '--help')
{
console.log('');
console.log(options.getHelp());
shelljs.exit(1);
} else if(initArgs[0] == '-V')
{
console.log('rapid-proto '.green + VERSION.bold.yellow);
shelljs.exit(1);
} else if(initArgs[0].indexOf('-') == -1) {
protoName = initArgs
} else {
console.log("parameter not valid".red + "\nrun " + "rp -h".green +" to see all the options");
shelljs.exit(1);
}
} else {
params = args.parser(process.argv).parse(options);
protoName = params.protoName;
}
if(!protoName)
{
shelljs.echo('Need to specify the prototype name');
shelljs.exit(1);
}
console.log("Starting new prototype: ".green + protoName.blue)
if (!shelljs.which('git')) {
console.log("Sorry, this script requires git".red);
return;
}
var versionOfPrototype = params.version;
var urlToPull = null;
switch(versionOfPrototype)
{
case "budo":
urlToPull = "https://github.com/silviopaganini/rapid-prototype.git";
break;
case "rje":
urlToPull = "https://github.com/silviopaganini/jspm-react-express-proto.git"
break;
case 'bare':
urlToPull = "https://github.com/silviopaganini/rapid-proto-es6-babel-bare.git"
break;
default:
urlToPull = "https://github.com/silviopaganini/rapid-prototyping-jspm.git";
break;
}
if(shelljs.exec("git clone " + urlToPull + " " + protoName).code !==0)
{
console.log("Error: git pull failed".red);
return;
}
shelljs.cd(protoName);
console.log("Updating ".green + "package.json".italic.gray);
var USR=shelljs.exec("git config --global user.name", {silent:true}).output.split("\n").join("");
var file = "package.json";
jsonfile.readFile(file, function(err, obj){
var newPackage = JSON.parse(JSON.stringify(obj));
newPackage.name = protoName;
newPackage.version = "0.0.1";
newPackage.repository = {
"type": "git",
"url": "git://github.com/username/repository.git"
};
newPackage.bugs = "";
newPackage.author = USR;
jsonfile.writeFile(file, newPackage, function(err){
generateReadme();
})
})
function generateReadme()
{
console.log("Generating ".green + "README.md".italic.gray);
shelljs.rm("README.md");
var readmeText = "#"+protoName+" \n\nDescription of your prototype\n\n\n###Usage:\n\n\`npm start\`";
fs.writeFile("README.md", readmeText, continueInstall);
}
function continueInstall(err)
{
if(err)
{
console.log(err.red);
return;
}
console.log("Cleaning up...".green);
// remove git from old prototype
shelljs.rm("-rf", ".git");
console.log("Installing dependencies...".green);
shelljs.exec('npm install');
console.log("\nReady to fly =)\n".black.inverse);
console.log("Starting local server".green.bold);
shelljs.exec('npm start');
}