@procore/core-scripts
Version:
A CLI to enhance your development experience
219 lines • 8.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const enquirer_1 = require("enquirer");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
const validate_npm_package_name_1 = tslib_1.__importDefault(require("validate-npm-package-name"));
const BaseCommand_1 = require("../../BaseCommand");
const Generator_1 = require("../../Generator");
class AppGenCommand extends BaseCommand_1.BaseCommand {
constructor() {
super(...arguments);
this.templatePath = path_1.default.resolve(__dirname, '..', '..', '..', 'templates', 'app', 'gen');
this.validateNpmName = (name) => {
const { errors, warnings } = (0, validate_npm_package_name_1.default)(name);
if (errors) {
return errors.join('.');
}
if (warnings) {
return warnings.join('.');
}
return true;
};
this.formatDestPath = (destPath) => {
const formattedDest = destPath.replace(/\s/g, '-');
return this.workspace.resolve(formattedDest);
};
}
async run() {
const appName = await this.askForAppName();
const orgName = await this.askForOrgName();
const destPath = await this.askDestPath(appName);
const fileExt = await this.askForFileExt();
this.generator = new Generator_1.Generator({
env: this.env,
workspace: this.workspace,
templatePath: this.templatePath,
destPath,
});
await this.generateProject({ appName, fileExt, orgName });
}
printFailedGeneration(error) {
this.newline();
this.log(chalk_1.default.red(error.message));
this.newline();
this.log(chalk_1.default.red([
'Project creation failed. Use the following command if you would ',
'like to remove failed generation:',
].join('')));
this.newline();
this.log(chalk_1.default.red(` $ cores-scripts rm ${this.generator.destPath}`));
this.newline();
this.exit(1);
}
async askForAppName() {
const response = await (0, enquirer_1.prompt)({
type: 'input',
name: 'appName',
message: 'What is the application name?',
validate: this.validateNpmName,
});
return response.appName;
}
async askForOrgName() {
const response = await (0, enquirer_1.prompt)({
type: 'input',
name: 'orgName',
message: 'What is the organization name?',
initial: 'procore',
validate: this.validateNpmName,
});
return response.orgName;
}
async askDestPath(appName) {
const response = await (0, enquirer_1.prompt)({
type: 'input',
name: 'destPath',
message: 'Where would you like to generate the application?',
initial: this.workspace.resolve(appName),
result: this.formatDestPath,
format: this.formatDestPath,
validate: (dest) => {
const formattedDest = this.formatDestPath(dest);
if (fs_1.default.existsSync(formattedDest)) {
return `Folder ${dest} already exists.`;
}
return true;
},
});
return response.destPath;
}
async askForFileExt() {
const response = await (0, enquirer_1.prompt)({
type: 'select',
name: 'fileExt',
message: 'Which initial language would you like to use?',
choices: [
{ name: 'ts', message: 'TypeScript' },
{ name: 'js', message: 'JavaScript' },
],
});
return response.fileExt;
}
async generateProject(opts) {
try {
this.newline();
await this.createAllRequiredFolders();
await this.generatePackageJSON(opts.appName, opts.orgName);
await this.generateIndex(opts.fileExt);
await this.generateApplication(opts.fileExt);
await this.generateApplicationTest(opts.fileExt);
await this.generateSetupFiles(opts.fileExt);
await this.generateSetupFramework(opts.fileExt);
await this.generateTsConfig();
await this.generateGitIgnore();
await this.generateYarnrc();
await this.generatePrettierConfig();
await this.generateReadMe(opts.appName);
await this.installDependencies(this.generator.destPath);
await this.generator.initializeGit();
this.printHelp(opts.appName, opts.orgName);
}
catch (error) {
this.printFailedGeneration(error);
}
}
async installDependencies(destPath) {
await this.generator.installDependencies(['@procore/core-react', 'react', 'react-dom'], {
exact: true,
cwd: destPath,
});
await this.generator.installDependencies([
'@procore/core-scripts',
'jest-enzyme',
'enzyme',
'enzyme-adapter-react-16',
'@types/react',
'@types/react-dom',
'@types/enzyme',
], {
exact: true,
dev: true,
cwd: destPath,
});
}
printHelp(appName, orgName) {
const relativePath = path_1.default.relative(this.workspace.cwd, this.generator.destPath);
this.newline();
this.log(chalk_1.default.green(`${this.packageName(appName, orgName)} Application Generated`));
this.newline();
this.log(chalk_1.default.gray(` $ cd ${relativePath}`));
this.log(chalk_1.default.gray(' $ yarn test --watch'));
this.newline();
this.log(chalk_1.default.blue('Happy coding!'));
this.newline();
}
async createAllRequiredFolders() {
await this.createDestFolder();
await this.createTypesFolder();
await this.generateGlobalsTs();
await this.createTestSetupsFolder();
}
createDestFolder() {
return this.generator.makeDir('.');
}
createTypesFolder() {
return this.generator.makeDir('src', '@types');
}
createTestSetupsFolder() {
return this.generator.makeDir('src', '__tests__');
}
generateIndex(ext) {
return this.generator.render('index.ejs', `src/index.${ext}x`);
}
generateApplication(ext) {
return this.generator.render('Application.ejs', `src/Application.${ext}x`);
}
generateApplicationTest(ext) {
return this.generator.render('Application.test.ejs', `src/Application.test.${ext}x`);
}
generateTsConfig() {
return this.generator.render('tsconfig.json.ejs', 'tsconfig.json');
}
generatePackageJSON(appName, orgName) {
return this.generator.render('package.json.ejs', 'package.json', {
pkgName: this.packageName(appName, orgName),
});
}
generateGlobalsTs() {
return this.generator.render('globals.d.ts.ejs', 'src/@types/globals.d.ts');
}
generateGitIgnore() {
return this.generator.render('gitignore.ejs', '.gitignore');
}
generateYarnrc() {
return this.generator.render('yarnrc.ejs', '.yarnrc');
}
generateReadMe(appName) {
return this.generator.render('README.md.ejs', 'README.md', {
appName,
});
}
generatePrettierConfig() {
return this.generator.render('prettier.config.js.ejs', 'prettier.config.js');
}
generateSetupFiles(ext) {
return this.generator.render('setupFiles.ejs', `src/__tests__/setupFiles.${ext}`);
}
generateSetupFramework(ext) {
return this.generator.render('setupFramework.ejs', `src/__tests__/setupFramework.${ext}`);
}
packageName(appName, orgName) {
return `@${orgName}/${appName}`;
}
}
AppGenCommand.description = 'Generates application scaffolding.';
exports.default = AppGenCommand;
//# sourceMappingURL=gen.js.map