|
| 1 | +#!/usr/bin/env node |
| 2 | +/* eslint-disable no-console */ |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const { blue, red, green, yellow, magenta } = require('chalk'); |
| 7 | +const config = require('./config'); |
| 8 | + |
| 9 | +const createIfNotExists = folder => { |
| 10 | + if (!fs.existsSync(folder)) { |
| 11 | + console.log(`Creating new jest-runner in ${green(folder)}`); |
| 12 | + fs.mkdirSync(folder); |
| 13 | + } else { |
| 14 | + throw new Error('Folder already exists'); |
| 15 | + } |
| 16 | +}; |
| 17 | + |
| 18 | +const scaffoldRunner = () => { |
| 19 | + try { |
| 20 | + const fixturesPath = path.resolve(__dirname, 'fixtures'); |
| 21 | + let outputDirname; |
| 22 | + const projectName = process.argv[2]; |
| 23 | + if (projectName) { |
| 24 | + outputDirname = path.resolve('.', projectName); |
| 25 | + createIfNotExists(outputDirname); |
| 26 | + } else { |
| 27 | + throw new Error('Project name not specified'); |
| 28 | + } |
| 29 | + |
| 30 | + config.dirs.forEach(item => { |
| 31 | + console.log(`Creating ${blue('directory')} ${magenta(item)}`); |
| 32 | + fs.mkdirSync(path.resolve(outputDirname, item)); |
| 33 | + }); |
| 34 | + |
| 35 | + config.createList.forEach(file => { |
| 36 | + const filePath = path.resolve(outputDirname, file.output); |
| 37 | + const content = fs.readFileSync(path.resolve(fixturesPath, file.input)); |
| 38 | + console.log(`Creating ${yellow('file')} ${magenta(file.output)}`); |
| 39 | + fs.writeFileSync(filePath, content); |
| 40 | + }); |
| 41 | + console.log(green('Scaffolding successfull')); |
| 42 | + console.log(blue('Run cd npm/yarn install')); |
| 43 | + console.log(red('Update the package name package.json')); |
| 44 | + } catch (e) { |
| 45 | + console.log(`${red('Scaffolding failed')} ${e}`); |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +scaffoldRunner(); |
| 50 | + |
| 51 | +module.exports = { |
| 52 | + scaffoldRunner, |
| 53 | +}; |
0 commit comments