1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | var C = require('./lib/palette'),
|
6 | cli = require('cli').enable('glob'),
|
7 | path = require('path'),
|
8 | fse = require('fs-extra'),
|
9 | Case = require('case'),
|
10 | pluralize = require('pluralize'),
|
11 | banner = require('./lib/banner.js'),
|
12 | _ = require('lodash'),
|
13 | Task = {},
|
14 | Project = {},
|
15 | task_command, task_args;
|
16 |
|
17 | Task.spawn = require('./lib/spawn_task');
|
18 | Task.handle = require('./lib/handle_task');
|
19 | Project.create = require('./lib/create');
|
20 | Project.install = require('spawn-npm-install');
|
21 | Project.generate = require('./lib/generate');
|
22 | Project.refill = require('./lib/refill');
|
23 | Project.upgrade = require('./lib/upgrade');
|
24 |
|
25 | cli.parse({
|
26 | create: ['c', 'Generate resource from template. Ex: feds -c "My COOL Website" ./my_cool_website"', 'string'],
|
27 | generate: ['g', 'Generate resource from template. Ex: feds -g block menu common/main_menu', 'string'],
|
28 | refill: ['r', 'Install refill. Ex: feds -r slider common/full_screen_slider', 'string'],
|
29 | build: ['b', 'Build assets and resources for production enviroment.'],
|
30 | development: ['d', 'Watches files, builds assets and resources for development.'],
|
31 | install: ['i', 'Install dependencies.'],
|
32 | report: [null, 'Code quality test and report.'],
|
33 | docs: [null, 'Create docs from sources and _resource_.md files.'],
|
34 | upgrade: ['U', 'Upgrade boilerplate build tasks.'],
|
35 | verbose: [null, 'Debug.'],
|
36 | version: ['v', 'Project version']
|
37 | });
|
38 |
|
39 | cli.main(function (args, options) {
|
40 |
|
41 | if (_.result(_.uniq(_.valuesIn(options)), 0) === null) {
|
42 | cli.fatal('Missing options. Use feds --help to list available commands');
|
43 | return;
|
44 | }
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 | if (options.version) {
|
52 | banner('CML cli info', '');
|
53 | }
|
54 | else if (typeof options.create === 'string') {
|
55 | if (args.length < 1) {
|
56 | cli.fatal('Create project requires 2 arguments. Ex: feds --create "Project name" ./target_directory');
|
57 | return;
|
58 | }
|
59 | var dest = path.join(process.cwd(), args[0]);
|
60 |
|
61 | banner('Creating project ' + options.create, dest);
|
62 |
|
63 | cli.info(C.key('Create project ') + C.value(options.create) + C.key(' into ') + C.value(dest));
|
64 | cli.spinner('Working...');
|
65 | Project.create({
|
66 | repository: args[1] || 'https://github.com/viktorbezdek/feds-boilerplate.git',
|
67 | destination: dest,
|
68 | name: Case.snake(options.create).replace('_', '-')
|
69 | }, function (err) {
|
70 | cli.spinner('Working...done', true);
|
71 | if (err) {
|
72 | cli.error('Error creating project...');
|
73 | cli.debug(C.error(err));
|
74 | return false;
|
75 | }
|
76 | cli.ok(C.success('Project created successfully...'));
|
77 | return true;
|
78 | });
|
79 |
|
80 | return;
|
81 | }
|
82 |
|
83 |
|
84 | |
85 |
|
86 |
|
87 | else {
|
88 |
|
89 | if (!args) console.log('no args');
|
90 |
|
91 | try {
|
92 |
|
93 | Project.info = fse.readJsonSync(path.join(process.cwd(), 'feds.json'));
|
94 |
|
95 | Project.pkg = fse.readJsonSync(path.join(process.cwd(), 'package.json'));
|
96 | }
|
97 | catch (e) {
|
98 | cli.fatal('You are not in FEDS project folder. Can\'t find feds.json file.');
|
99 | return;
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 | if (typeof options.refill === 'string') {
|
105 |
|
106 | banner('Refilling resource ' + options.refill, args[0]);
|
107 |
|
108 | cli.info(C.key('Refill resource ') + C.value(options.refill) + C.key(' into ') + C.value(args[0]));
|
109 | cli.spinner('Working...');
|
110 | Project.refill({
|
111 | repository: args[1] || 'https://github.com/viktorbezdek/feds-refill-' + Case.snake(options.refill).replace('_', '-'),
|
112 | destination: path.join(process.cwd(), Project.info.sources.views),
|
113 | subfolder: args[0] || null,
|
114 | name: Case.pascal(options.refill)
|
115 | },
|
116 | function (err) {
|
117 | cli.spinner('Working...done', true);
|
118 | if (err) {
|
119 | cli.error('Error creating project...');
|
120 | cli.debug(C.error(err));
|
121 | return false;
|
122 | }
|
123 | cli.ok(C.success('Resource refilled successfully...'));
|
124 | return true;
|
125 | });
|
126 |
|
127 | return;
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 | if (options.install) {
|
133 | var join = function(sep) { return function(arr) { return arr.join(sep); }; };
|
134 |
|
135 | var packagesToInstall = _(Project.pkg.dependencies)
|
136 | .merge(Project.pkg.devDependencies)
|
137 | .pairs()
|
138 | .map(join('@'))
|
139 | .value();
|
140 |
|
141 | banner('Install project dependencies', process.cwd());
|
142 |
|
143 | cli.spinner('Working...');
|
144 | Project.install(packagesToInstall, {
|
145 | stdio: 'inherit',
|
146 | cwd: process.cwd()
|
147 | }, function () {
|
148 | cli.spinner('Working...done', true);
|
149 | });
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 | if (options.upgrade) {
|
155 | banner('Upgrade project build scripts', process.cwd());
|
156 | Project.upgrade(null, function (err) {
|
157 | cli.spinner('Working...done', true);
|
158 | if (err) {
|
159 | cli.error('Error creating upgrading build scripts...');
|
160 | cli.debug(C.error(err));
|
161 | return false;
|
162 | }
|
163 | cli.ok(C.success('Build scripts upgraded successfully...'));
|
164 | return true;
|
165 | });
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 | if (options.build) {
|
171 | task_args = Project.info.tasks.build.split(' ');
|
172 | task_command = task_args.shift();
|
173 | options.verbose && task_args.push('--silent');
|
174 | options.release && task_args.push('--release');
|
175 | banner('Build for production', process.cwd());
|
176 | cli.spinner('Working...');
|
177 | Task.handle(Task.spawn({
|
178 | command: task_command,
|
179 | arguments: task_args,
|
180 | cwd: process.cwd()
|
181 | }), function (err) {
|
182 | cli.spinner('Working...done', true);
|
183 | if (!err) {
|
184 | cli.ok('Production build completed successfully.');
|
185 | return;
|
186 | }
|
187 |
|
188 | cli.error(err);
|
189 | });
|
190 | }
|
191 |
|
192 |
|
193 |
|
194 | if (options.development) {
|
195 | task_args = Project.info.tasks.development.split(' ');
|
196 | task_command = task_args.shift();
|
197 | options.verbose && task_args.push('--silent');
|
198 | banner('Build for development', process.cwd());
|
199 | cli.spinner('Working...');
|
200 | Task.handle(Task.spawn({
|
201 | command: task_command,
|
202 | arguments: task_args,
|
203 | cwd: process.cwd()
|
204 | }), function (err) {
|
205 | cli.spinner('Working...done', true);
|
206 | if (!err) {
|
207 | cli.ok('Development build completed successfully.');
|
208 | return;
|
209 | }
|
210 |
|
211 | cli.error(err);
|
212 | });
|
213 | }
|
214 |
|
215 |
|
216 |
|
217 | if (options.report) {
|
218 | task_args = Project.info.tasks.report.split(' ');
|
219 | task_command = task_args.shift();
|
220 | banner('Code quality test and report', process.cwd());
|
221 | cli.spinner('Working...');
|
222 | Task.handle(Task.spawn({
|
223 | command: task_command,
|
224 | arguments: task_args,
|
225 | cwd: process.cwd()
|
226 | }), function (err) {
|
227 | cli.spinner('Working...done', true);
|
228 | if (!err) {
|
229 | cli.ok('Code quality test and report completed successfully.');
|
230 | return;
|
231 | }
|
232 |
|
233 | cli.error(err);
|
234 | });
|
235 | }
|
236 |
|
237 |
|
238 |
|
239 | if (options.docs) {
|
240 | task_args = Project.info.tasks.docs.split(' ');
|
241 | task_command = task_args.shift();
|
242 | banner('Create docs', process.cwd());
|
243 | cli.spinner('Working...');
|
244 | Task.handle(Task.spawn({
|
245 | command: task_command,
|
246 | arguments: task_args,
|
247 | cwd: process.cwd()
|
248 | }), function (err) {
|
249 | cli.spinner('Working...done', true);
|
250 | if (!err) {
|
251 | cli.ok('Docs created successfully.');
|
252 | return;
|
253 | }
|
254 | cli.error(err);
|
255 | });
|
256 | }
|
257 |
|
258 |
|
259 |
|
260 | if (options.generate) {
|
261 |
|
262 | if (!Project.info.generators.hasOwnProperty(options.generate)) {
|
263 | cli.fatal('Generator ' + options.generate + ' not found in feds.json.');
|
264 | return;
|
265 | }
|
266 |
|
267 | var opts = {
|
268 | type: options.generate,
|
269 | files: Project.info.generators[options.generate].files,
|
270 | destRoot: Project.info.sources[pluralize(options.generate)],
|
271 | names: args
|
272 | };
|
273 |
|
274 | cli.spinner('Working...');
|
275 | Project.generate(opts, function (err) {
|
276 | cli.spinner('Working...done', true);
|
277 | if (!err) {
|
278 | cli.ok('Resource(s) generated successfully.');
|
279 | return;
|
280 | }
|
281 | cli.error(err);
|
282 | });
|
283 | }
|
284 |
|
285 | }
|
286 | });
|