UNPKG

2.52 kBJavaScriptView Raw
1const fs = require('fs-extra');
2const path = require('path');
3const _ = require('lodash');
4const chalk = require('chalk');
5const spawn = require('cross-spawn');
6const utils = require('../utils');
7const readlineSync = require('readline-sync');
8const createLogger = require('logging').default;
9const logger = createLogger('Init');
10
11const relativePath = __dirname.replace(process.cwd(), '.');
12
13const templateDir = path.join(relativePath,'../core_files');
14const destinyDir = path.join(process.cwd(),'frontend');
15
16logger.info('Start to initialize project');
17
18//copy file
19logger.info('Copy files');
20fs.copySync(templateDir, destinyDir);
21
22//copy gitignore
23logger.info('Generate .gitignore');
24if(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//add npm scripts to package.json
31logger.info('Modify package.json');
32let currentJSON = JSON.parse(fs.readFileSync(path.join(process.cwd(),'package.json')));
33let tasks = JSON.parse(fs.readFileSync(path.join(relativePath,'../config/tasks.json')));
34
35_.merge(currentJSON,tasks);
36
37let hasHelpers = true;
38
39if (!currentJSON.dependencies['@area17/a17-helpers']) {
40 currentJSON.dependencies['@area17/a17-helpers'] = '^1.x';
41 hasHelpers = false;
42}
43
44fs.writeFileSync(
45 path.join(process.cwd(),'package.json'),
46 JSON.stringify(currentJSON, null, 2)
47);
48
49if (!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//check if dot files are needed
62logger.info('Install dot files');
63let answer = readlineSync.question('Install .nvmrc, .editorconfig, .jshintrc and .scss-lint.yaml?(y/n)\n(be sure to install necessary plugins to your editor)');
64if(answer === 'y') {
65 fs.copySync(path.join(relativePath,'../dot_files'), process.cwd());
66}
67
68logger.info('Install default frontend.config.json (the scss setup variables)');
69answer = readlineSync.question('Install default frontend.config.json?(y/n)');
70if(answer === 'y') {
71 fs.copySync(path.join(relativePath,'../scss_variables'), process.cwd());
72}
73
74fs.copySync(path.join(relativePath,'../config/manifest.json'), path.join(process.cwd(), '/frontend/manifest.json'));
75
76logger.info(chalk.green('Finished'));
77
78process.exit();