UNPKG

2.29 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var path = require('path');
4var fs = require('fs-extra');
5var commander = require('commander')
6var chalk = require('chalk');
7var spawn = require('cross-spawn');
8// var validateProjectName = require("validate-npm-package-name");
9
10var projectName;
11commander.usage('<app-name>')
12 .action(function(name) {
13 projectName = name;
14 })
15
16commander.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
24function help() {
25 commander.parse(process.argv)
26 if (commander.args.length < 1) return commander.help()
27}
28help()
29
30
31var currentNodeVersion = process.versions.node;
32if (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
44create(projectName);
45
46function 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}