UNPKG

2.67 kBJavaScriptView Raw
1var exec = require('child-process-promise').exec;
2var spawn = require('child-process-promise').spawn;
3var status = require('../lib/status');
4let path = require('path');
5var program = null;
6
7function action(project_name, options) {
8 try {
9 console.log(`Creating ${project_name}`);
10 clone_repo(options.template, project_name)
11 .then(function (result) {
12 "use strict";
13 install_packages(options.template, project_name)
14 })
15 .catch(function (error) {
16 "use strict";
17 throw new Error(error.stack);
18 })
19 } catch (e) {
20 console.log(e.stack);
21 }
22}
23
24function install_packages(template, project_name) {
25 status.start();
26 console.log('Installing Packages');
27 let promise = spawn('npm', ['install'], { cwd: project_name });
28 let childProcess = promise.childProcess;
29
30 childProcess.stdout.on('data', function (data) {
31 console.log(data.toString());
32 });
33 childProcess.stderr.on('data', function (data) {
34 console.log(data.toString());
35 });
36
37 promise
38 .then(function () {
39 console.log('Done installing');
40 status.stop();
41 })
42 .catch(function (err) {
43 console.error(err);
44 status.stop();
45 });
46}
47
48function clone_repo(template, project_name) {
49 return new Promise(function (resolve, reject) {
50 status.start();
51 console.log('Cloning Packages');
52 let templatePath = path.join(__dirname, '/../make_templates/spice_template');
53 let promise = spawn('cp', ['-r', templatePath, `${project_name}`]);
54
55 let childProcess = promise.childProcess;
56
57 childProcess.stdout.on('data', function (data) {
58 console.log(data.toString());
59 });
60 childProcess.stderr.on('data', function (data) {
61 console.log(data.toString());
62 });
63 //remove the .git
64 promise
65 .then(function () {
66 console.log('Done cloning');
67 status.stop();
68 exec(`cd ${project_name} && rm -fr .git && mkdir storage && cd storage && mkdir logs`);
69 resolve('done');
70 })
71 .catch(function (err) {
72 console.error(err);
73 status.stop();
74 reject(err);
75 });
76 });
77}
78
79function command(prog) {
80 'use strict';
81 program = prog;
82 program
83 .version('0.0.1')
84 .command('init <project_name>')
85 .description('Create New Spice.js Project')
86 .action(action)
87}
88
89module.exports = {
90 command: command,
91 action: action
92};