UNPKG

@area17/a17-boilerplate

Version:

The official AREA 17 boilerplate

72 lines (56 loc) 2.26 kB
const fs = require('fs-extra'); const path = require('path'); const _ = require('lodash'); const chalk = require('chalk'); const spawn = require('cross-spawn'); const utils = require('../utils'); const readlineSync = require('readline-sync'); const createLogger = require('logging').default; const logger = createLogger('Init'); const relativePath = __dirname.replace(process.cwd(), '.'); const templateDir = path.join(relativePath,'../core_files'); const destinyDir = path.join(process.cwd(),'frontend'); logger.info('Start to initialize project'); //copy file logger.info('Copy files'); fs.copySync(templateDir, destinyDir); //copy gitignore logger.info('Generate .gitignore'); if(utils.hasFile('.gitignore')) { logger.warn('A .gitignore file is found, will skip generating .gitignore'); } else { fs.copySync(path.join(relativePath,'../config/.ignore'),path.join(process.cwd(),'.gitignore'),{ overwrite: false }); } //add npm scripts to package.json logger.info('Modify package.json'); let currentJSON = JSON.parse(fs.readFileSync(path.join(process.cwd(),'package.json'))); let tasks = JSON.parse(fs.readFileSync(path.join(relativePath,'../config/tasks.json'))); _.merge(currentJSON,tasks); let hasHelpers = true; if (!currentJSON.dependencies['@area17/a17-helpers']) { currentJSON.dependencies['@area17/a17-helpers'] = '^0.x'; hasHelpers = false; } fs.writeFileSync( path.join(process.cwd(),'package.json'), JSON.stringify(currentJSON, null, 2) ); if (!hasHelpers) { logger.info('a17-helpers package is missing, installing now.'); let result = spawn.sync('npm', ['install'], {stdio: 'inherit'}); if(result.status === 1) { logger.error('Exit with an error'); process.exit(); } else { logger.info('a17-helpers is installed'); } } //check if dot files are needed logger.info('Install dot files'); let answer = readlineSync.question('Install .editorconfig, .jshintrc and .scss-lint.yaml?(y/n)\n(be sure to install necessary plugins to your editor)'); if(answer === 'y') { fs.copySync(path.join(relativePath,'../dot_files'), process.cwd()); } fs.copySync(path.join(relativePath,'../config/manifest.json'), path.join(process.cwd(), '/frontend/manifest.json')); logger.info(chalk.green('Finished')); process.exit();