UNPKG

chimp

Version:

Your development companion for doing quality, faster.

75 lines (74 loc) 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const fs = tslib_1.__importStar(require("node:fs")); const path = tslib_1.__importStar(require("node:path")); // @ts-ignore const shelljs_1 = tslib_1.__importDefault(require("shelljs")); const get_chimp_version_1 = require("../helpers/get-chimp-version"); const core_1 = require("@oclif/core"); function updatePrefixes(appDirectory, appPrefix, generatedPrefix) { if (appPrefix || generatedPrefix) { for (const file of shelljs_1.default.ls('-R', appDirectory)) { if (shelljs_1.default.test('-f', `${appDirectory}/${file}`)) { if (appPrefix) { shelljs_1.default.sed('-i', '~app', appPrefix, `${appDirectory}/${file}`); } if (generatedPrefix) { shelljs_1.default.sed('-i', '~generated', generatedPrefix, `${appDirectory}/${file}`); } } } let gqlGenerateCustomPrefixString = ''; if (appPrefix !== '~app') { gqlGenerateCustomPrefixString += ` -a '${appPrefix}'`; } if (generatedPrefix !== '~generated') { gqlGenerateCustomPrefixString += ` -p '${generatedPrefix}'`; } shelljs_1.default.sed('-i', 'chimp generate', `chimp generate${gqlGenerateCustomPrefixString}`, `${appDirectory}/package.json`); const currentChimpVersion = (0, get_chimp_version_1.getChimpVersion)(); shelljs_1.default.sed('-i', '"chimp": "latest"', `"chimp": "${currentChimpVersion}"`, `${appDirectory}/package.json`); } } const create = (appDirectory, appPrefix, generatedPrefix) => { const scaffoldDir = `${process.cwd()}/${appDirectory}`; if (fs.existsSync(scaffoldDir)) { console.log(`Path: ${scaffoldDir} already exists. Can't create a new app in an already existing path.`); process.exit(1); } shelljs_1.default.cp('-R', path.join(__dirname, '../../scaffold'), `${scaffoldDir}`); shelljs_1.default.exec(`cd ${appDirectory} && git init .`); shelljs_1.default.mv(`${appDirectory}/gitignore`, `${appDirectory}/.gitignore`); updatePrefixes(appDirectory, appPrefix, generatedPrefix); console.log(`\n${appDirectory} created successfully!`); console.log(`run: cd ${appDirectory} npm install`); console.log('and start hacking! :-)'); }; class Create extends core_1.Command { async run() { const { args, flags } = await this.parse(Create); create(args.name, flags.appPrefix, flags.generatedPrefix); } } Create.description = 'create (scaffold) a new app'; Create.examples = ['$ chimp create my-new-app', '$ chimp create my-new-app -a ~src -g ~chimp-helpers']; Create.flags = { help: core_1.Flags.help({ char: 'h' }), appPrefix: core_1.Flags.string({ char: 'a', description: 'prefix that points to the sourcecode of your app', default: '~app', }), generatedPrefix: core_1.Flags.string({ char: 'g', description: 'prefix that points to the generated by chimp helper code', default: '~generated', }), }; Create.args = { name: core_1.Args.string({ required: true, description: 'name of the new app, also used as the directory' }), }; exports.default = Create;