1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 | const rc = require('rc');
|
6 | const spawn = require('cross-spawn');
|
7 | const router = require('./router');
|
8 |
|
9 | module.exports = (projectName, projectPath, templateType) => {
|
10 | const npm = router(templateType).npm;
|
11 | const npmrc = rc('npm');
|
12 |
|
13 | const author = (() => {
|
14 | const str = [];
|
15 |
|
16 | if (npmrc['init.author.url']) str.push(`(${npmrc['init.author.url']})`);
|
17 | if (npmrc['init.author.name']) str.push(npmrc['init.author.name']);
|
18 | if (npmrc['init.author.email']) str.push(`<${npmrc['init.author.email']}>`);
|
19 |
|
20 | return str.join(' ');
|
21 | })();
|
22 |
|
23 | const packageInfo = {
|
24 | name : projectName,
|
25 | main : path.join(npm.setting.main, 'index.js'),
|
26 | author : author === '' ? undefined : author,
|
27 | version : '0.0.1',
|
28 | license : npmrc['init.license'] === undefined ? 'ISC' : npmrc['init.license'],
|
29 | description: ''
|
30 | };
|
31 |
|
32 | npm.setting.key.forEach((e) => packageInfo[Object.keys(e)[0]] = e[Object.keys(e)[0]]);
|
33 |
|
34 | fs.writeFileSync(path.join(projectPath, 'package.json'),
|
35 | JSON.stringify(Object.assign(packageInfo, npm.tasks, npm.env), null, 2));
|
36 |
|
37 | if (process.env.NODE_ENV !== 'test') {
|
38 |
|
39 |
|
40 | install([
|
41 | 'install',
|
42 | 'my-dish'
|
43 | ]);
|
44 |
|
45 |
|
46 | install([
|
47 | 'install',
|
48 | '-S',
|
49 | ...npm.packages.dependencies
|
50 | ]);
|
51 |
|
52 |
|
53 | install([
|
54 | 'install',
|
55 | '-D',
|
56 | ...npm.packages.devDependencies
|
57 | ]);
|
58 | }
|
59 | };
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function install(args) {
|
65 | spawn.sync('npm', args, { stdio: 'inherit' });
|
66 | }
|