1 | const fs = require('fs-extra');
|
2 | const path = require('path');
|
3 | const _ = require('lodash');
|
4 | const chalk = require('chalk');
|
5 | const spawn = require('cross-spawn');
|
6 | const utils = require('../utils');
|
7 | const readlineSync = require('readline-sync');
|
8 | const createLogger = require('logging').default;
|
9 | const logger = createLogger('Init');
|
10 |
|
11 | const relativePath = __dirname.replace(process.cwd(), '.');
|
12 |
|
13 | const templateDir = path.join(relativePath,'../core_files');
|
14 | const destinyDir = path.join(process.cwd(),'frontend');
|
15 |
|
16 | logger.info('Start to initialize project');
|
17 |
|
18 |
|
19 | logger.info('Copy files');
|
20 | fs.copySync(templateDir, destinyDir);
|
21 |
|
22 |
|
23 | logger.info('Generate .gitignore');
|
24 | if(utils.hasFile('.gitignore')) {
|
25 | logger.warn('A .gitignore file is found, will skip generating .gitignore');
|
26 | } else {
|
27 | fs.copySync(path.join(relativePath,'../config/.ignore'),path.join(process.cwd(),'.gitignore'),{ overwrite: false });
|
28 | }
|
29 |
|
30 |
|
31 | logger.info('Modify package.json');
|
32 | let currentJSON = JSON.parse(fs.readFileSync(path.join(process.cwd(),'package.json')));
|
33 | let tasks = JSON.parse(fs.readFileSync(path.join(relativePath,'../config/tasks.json')));
|
34 |
|
35 | _.merge(currentJSON,tasks);
|
36 |
|
37 | let hasHelpers = true;
|
38 |
|
39 | if (!currentJSON.dependencies['@area17/a17-helpers']) {
|
40 | currentJSON.dependencies['@area17/a17-helpers'] = '^1.x';
|
41 | hasHelpers = false;
|
42 | }
|
43 |
|
44 | fs.writeFileSync(
|
45 | path.join(process.cwd(),'package.json'),
|
46 | JSON.stringify(currentJSON, null, 2)
|
47 | );
|
48 |
|
49 | if (!hasHelpers) {
|
50 | logger.info('a17-helpers package is missing, installing now.');
|
51 | let result = spawn.sync('npm', ['install'], {stdio: 'inherit'});
|
52 |
|
53 | if(result.status === 1) {
|
54 | logger.error('Exit with an error');
|
55 | process.exit();
|
56 | } else {
|
57 | logger.info('a17-helpers is installed');
|
58 | }
|
59 | }
|
60 |
|
61 |
|
62 | logger.info('Install dot files');
|
63 | let answer = readlineSync.question('Install .nvmrc, .editorconfig, .jshintrc and .scss-lint.yaml?(y/n)\n(be sure to install necessary plugins to your editor)');
|
64 | if(answer === 'y') {
|
65 | fs.copySync(path.join(relativePath,'../dot_files'), process.cwd());
|
66 | }
|
67 |
|
68 | logger.info('Install default frontend.config.json (the scss setup variables)');
|
69 | answer = readlineSync.question('Install default frontend.config.json?(y/n)');
|
70 | if(answer === 'y') {
|
71 | fs.copySync(path.join(relativePath,'../scss_variables'), process.cwd());
|
72 | }
|
73 |
|
74 | fs.copySync(path.join(relativePath,'../config/manifest.json'), path.join(process.cwd(), '/frontend/manifest.json'));
|
75 |
|
76 | logger.info(chalk.green('Finished'));
|
77 |
|
78 | process.exit();
|