1 | #!/usr/bin/env node
|
2 |
|
3 | var path = require('path');
|
4 | var fs = require('fs-extra');
|
5 | var commander = require('commander')
|
6 | var chalk = require('chalk');
|
7 | var spawn = require('cross-spawn');
|
8 |
|
9 |
|
10 | var projectName;
|
11 | commander.usage('<app-name>')
|
12 | .action(function(name) {
|
13 | projectName = name;
|
14 | })
|
15 |
|
16 | commander.on('--help', function() {
|
17 | console.log(' Examples:')
|
18 | console.log()
|
19 | console.log(chalk.gray(' # create a new project with an official template'))
|
20 | console.log(' $ zaro init my-app')
|
21 | console.log()
|
22 | })
|
23 |
|
24 | function help() {
|
25 | commander.parse(process.argv)
|
26 | if (commander.args.length < 1) return commander.help()
|
27 | }
|
28 | help()
|
29 |
|
30 |
|
31 | var currentNodeVersion = process.versions.node;
|
32 | if (currentNodeVersion.split('.')[0] < 6) {
|
33 | console.error(
|
34 | chalk.red(
|
35 | 'You are running Node ' + currentNodeVersion + '.\n' +
|
36 | 'Zaro requires Node 6 or higher. \n' +
|
37 | 'Please update your version of Node.'
|
38 | )
|
39 | );
|
40 | process.exit(1);
|
41 | }
|
42 |
|
43 |
|
44 | create(projectName);
|
45 |
|
46 | function create(name) {
|
47 | var projectPath = path.resolve(name);
|
48 | fs.ensureDirSync(name);
|
49 | console.log('Createing a new Zaro project named ' + name + ' in ' + projectPath);
|
50 |
|
51 | var packageJson = {
|
52 | name: name,
|
53 | version: '0.1.0',
|
54 | private: true,
|
55 | dependencies: {
|
56 | "zaro": require('../package').version
|
57 | },
|
58 | scripts: {
|
59 | "start": "webpack-dev-server --config webpack.local.js"
|
60 | }
|
61 | }
|
62 | fs.writeFileSync(
|
63 | path.join(projectPath, 'package.json'),
|
64 | JSON.stringify(packageJson, null, 2)
|
65 | );
|
66 |
|
67 |
|
68 | console.log('Installing ' + chalk.cyan('Zaro') + ' and dependent packages');
|
69 | process.chdir(projectPath);
|
70 | new Promise(function(resolve, reject) {
|
71 | var command = 'npm';
|
72 | var args = ['install'];
|
73 | var child = spawn(command, args, { stdio: 'inherit' });
|
74 | child.on('close', function(code) {
|
75 | if (code !== 0) {
|
76 | reject({
|
77 | command: command + ' ' + args.join(' ')
|
78 | });
|
79 | return;
|
80 | }
|
81 | resolve();
|
82 | })
|
83 | }).then(function () {
|
84 | console.log('Package installing succeed!')
|
85 | console.log(projectPath)
|
86 | spawn('cp', ['-R', projectPath + '/node_modules/zaro/example/', './'], { stdio: 'inherit' });
|
87 | }).catch(function () {
|
88 | console.log(chalk.red('Package installing failed'));
|
89 | });
|
90 | }
|