-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
235 lines (199 loc) · 6.95 KB
/
index.js
File metadata and controls
235 lines (199 loc) · 6.95 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env node
'use strict';
var chalk = require('chalk');
var clear = require('clear');
var CLI = require('clui');
var figlet = require('figlet');
var inquirer = require('inquirer');
var Preferences = require('preferences');
var Spinner = CLI.Spinner;
var GitHubApi = require('github');
var _ = require('lodash');
var git = require('simple-git')();
var touch = require('touch');
var fs = require('fs');
var files = require('./lib/files');
var program = require('commander');
var exec = require('child_process').exec;
var pkg = require('./package.json');
var download = require('download-git-repo');
var path = require('path');
var cpr = require('cpr');
var replace = require('replace');
var rename = require('rename');
var template = require('lodash.template');
var shell = require('shelljs');
clear();
console.log(
chalk.yellow(
figlet.textSync('Tango JS', {
font: 'thin',
horizontalLayout: 'full'
})
)
);
let capitalizeFirstLetter = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
}
let generate = (type, name) => {
switch (type) {
case 'module':
if (name.indexOf('/') >= 0) {
var nameArr = name.split('/');
var fixName = nameArr[nameArr.length - 1];
} else {
var fixName = name;
}
console.log(chalk.cyan('Generating ' + fixName + ' module.'));
cpr(files.workingPath() + '/blueprints/angular/module', name, {
deleteFirst: false, //Delete "to" before
overwrite: false, //If the file exists, overwrite it
confirm: true //After the copy, stat all the copied files to make sure they are there
}, function(err, files) {
files.shift();
files.forEach(function(file) {
fs.rename(file, file.replace('__name__', fixName), function(err) {
if (err) console.log(chalk.black.bgRed('ERROR: ' + err));
});
})
if (typeof nameArr !== 'undefined') {
var pathName = name;
} else {
var pathName = './' + name;
}
replace({
regex: "<%= moduleNameLC %>",
replacement: fixName,
paths: [pathName],
recursive: true,
silent: true,
});
replace({
regex: "<%= moduleNameUC %>",
replacement: capitalizeFirstLetter(fixName),
paths: [pathName],
recursive: true,
silent: true,
});
console.log(chalk.white.bgGreen(fixName + ' module generated.'));
});
break;
case 'component':
console.log(chalk.cyan('Generating component.'));
break;
case 'service':
console.log(chalk.cyan('Generating service.'));
break;
case 'filter':
console.log(chalk.cyan('Generating filter.'));
break;
default:
console.log(chalk.red('Error: ' + type + ' generator not supported.'));
}
};
let init = () => {
var questions = [{
type: 'input',
name: 'name',
message: 'What\'s the name of your project?',
default: 'my-tango-app'
}, {
type: 'list',
name: 'type',
message: 'Select the type of project you are creating:',
choices: ['angular', 'ionic'],
default: ['angular']
}];
inquirer
.prompt(questions)
.then(function(answers) {
if (answers.type == 'angular') {
var status = new Spinner('Downloading seed from: https://github.com/cogoo/angularStarter...');
status.start();
if (files.directoryExists(answers.name)) {
console.log(chalk.yellow('This directory already exists. Please re-run init with a different name'));
status.stop();
} else {
download('cogoo/angularStarter', answers.name, function(err) {
if (err) {
status.stop();
console.log(chalk.red('Error in download, check internet connection and try again ...'));
} else {
status.stop();
console.log(chalk.green('Download completed ...'));
process.chdir(answers.name + '/src');
// run in cmd line
var npmStatus = new Spinner('Running npm install...');
npmStatus.start();
let output = (error, stdout, stderr) => {
if (error) {
npmStatus.stop();
console.log(chalk.red.bold.underline("exec error:") + error);
}
if (stdout) {
npmStatus.stop();
console.log(chalk.green.bold.underline("Result:") + stdout);
}
};
shell.exec('npm install', output);
}
});
}
} else if (answers.type == 'ionic') {
var status = new Spinner('Downloading seed from: https://github.com/cogoo/ionicStarter...');
status.start();
if (files.directoryExists(answers.name)) {
console.log(chalk.bgYellow.black('This directory already exists. Please re-run init with a different name'));
status.stop();
} else {
download('cogoo/ionicStarter', answers.name, function(err) {
if (err) {
status.stop();
console.log(chalk.red('Error in download, check internet connection and try again ...'));
} else {
status.stop();
console.log(chalk.green('Download completed ...'));
process.chdir(answers.name);
// run in cmd line
var npmStatus = new Spinner('Running npm install...');
npmStatus.start();
let output = (error, stdout, stderr) => {
if (error) {
npmStatus.stop();
console.log(chalk.red.bold.underline("exec error:") + error);
}
if (stdout) {
npmStatus.stop();
console.log(chalk.green.bold.underline("Result:") + stdout);
var ionicStatus = new Spinner('Copying ionic folders...');
ionicStatus.start();
cpr(process.cwd() + '/@ionic_bundle', process.cwd()+ '/node_modules/@ionic_bundle', {
deleteFirst: false, //Delete "to" before
overwrite: false, //If the file exists, overwrite it
confirm: true //After the copy, stat all the copied files to make sure they are there
}, function(err, files) {
ionicStatus.stop();
shell.exec('rm -r @ionic_bundle');
console.log(chalk.white.bgGreen('Ionic project ready!'));
})
}
};
shell.exec('npm install', output);
}
});
}
}
});
};
program
.version(pkg.version)
.command('init')
.action(init);
program
.version(pkg.version)
.command('generate <type> <name>')
.alias('g')
.action(generate);
program.parse(process.argv);
// if program was called with no arguments, show help.
if (program.args.length === 0) program.help();