UNPKG

2.7 kBJavaScriptView Raw
1/**
2 * module : Uba-Scripts uba init
3 * author : Kvkens(yueming@yonyou.com)
4 * update : 2016-08-24 08:40:09
5 */
6
7var fs = require('fs-extra');
8var path = require('path');
9var spawn = require('cross-spawn');
10var pathExists = require('path-exists');
11var chalk = require('chalk');
12
13module.exports = function(appPath, appName) {
14 var ownPath = path.join(appPath, 'node_modules', 'uba-scripts');
15
16 var appPackage = require(path.join(appPath, 'package.json'));
17 var ownPackage = require(path.join(ownPath, 'package.json'));
18
19 // Copy over some of the devDependencies
20 appPackage.dependencies = appPackage.dependencies || {};
21 appPackage.devDependencies = appPackage.devDependencies || {};
22 [].forEach(function(key) {
23 appPackage.dependencies[key] = ownPackage.devDependencies[key];
24 });
25 [].forEach(function(key) {
26 appPackage.devDependencies[key] = ownPackage.devDependencies[key];
27 });
28
29 // Setup the script rules
30 appPackage.scripts = {};
31 ['start', 'build', 'test','publish-maven'].forEach(function(command) {
32 appPackage.scripts[command] = 'uba-scripts ' + command;
33 });
34
35 // explicitly specify ESLint config path for editor plugins
36 appPackage.eslintConfig = {
37 //extends: './node_modules/uba-scripts/config/eslint.js',
38 };
39
40 fs.writeFileSync(
41 path.join(appPath, 'package.json'),
42 JSON.stringify(appPackage, null, 2)
43 );
44
45
46 // Copy the files for the user
47 fs.copySync(path.join(ownPath, 'template'), appPath);
48
49 fs.move(path.join(appPath, 'gitignore'), path.join(appPath, '.gitignore'), [], function(err) {
50 if(err) {
51 if(err.code === 'EEXIST') {
52 var data = fs.readFileSync(path.join(appPath, 'gitignore'));
53 fs.appendFileSync(path.join(appPath, '.gitignore'), data);
54 fs.unlinkSync(path.join(appPath, 'gitignore'));
55 } else {
56 throw err;
57 }
58 }
59 });
60
61 // Run another npm install for react and react-dom
62 console.log(chalk.yellow('Installing package from npm...'));
63 console.log();
64 var args = [
65 'install'
66 ].filter(function(e) {
67 return e;
68 });
69 var proc = spawn('npm', args, {
70 stdio: 'inherit'
71 });
72 proc.on('close', function(code) {
73 if(code !== 0) {
74 console.error('`npm ' + args.join(' ') + '` failed');
75 return;
76 }
77
78 console.log();
79 console.log(chalk.green('Success! Created ' + appName + ' at ' + appPath + '.'));
80 console.log('Inside that directory, you can run several commands:');
81 console.log();
82 console.log(' * uba server: Starts the development server.');
83 console.log(' * uba build: Bundles the app into static files for production.');
84 console.log();
85 console.log('We suggest that you begin by typing:');
86 console.log();
87 console.log(' cd', appName + ' && uba server');
88 console.log();
89 console.log('uba say: Success :) ');
90 });
91};
\No newline at end of file