UNPKG

@onereach/orest-input-cli

Version:

The tool for creating, serving, and publishing OREST Inputs

190 lines (173 loc) 5.61 kB
const path = require('path'); const _ = require('lodash'); const { v4: uuidv4 } = require('uuid'); const Generator = require('yeoman-generator'); const { DEPENDENCIES } = require('./constants'); const { templateTypes, componentTypes } = require('../../packageType/types'); const PACKAGE_NAME_BASE = 'orest-input-'; module.exports = class extends Generator { constructor(args, opts) { super(args, opts); this.option('packageType', { type: String, alias: 't' }); this.packageType = _.get(this.options, 'packageType'); this.option('destination', { type: String, default: '.', alias: 'd' }); const userDestination = _.get(this.options, 'destination', '.'); this.destination = path.isAbsolute(userDestination) ? userDestination : path.join(process.cwd(), userDestination); this.destinationRoot(this.destination); this.packageNameSuffix = uuidv4(); } initializing() { this.composeWith(require.resolve('../tests'), { destinationRoot: this.destination, }); } async prompting() { this.answers = await this.prompt([ { type: 'list', name: 'componentTemplate', required: true, message: 'Select a component template', choices: Object.values(templateTypes).map(type => ({ value: type, name: type, })), default: () => templateTypes.REGULAR, validate: value => Object.values(templateTypes).includes(value), }, { type: 'list', name: 'componentType', required: true, message: 'Select a component type', choices: Object.values(componentTypes).map(type => ({ value: type, name: type, })), default: () => componentTypes.INPUT, validate: value => Object.values(componentTypes).includes(value), }, { type: 'input', name: 'label', message: 'Display name (e.g.: Text Input)', validate: value => Boolean(value), }, { type: 'confirm', name: 'useDefaultPackageName', message: 'Use default package name? (recommended)', validate: value => Boolean(value), }, { type: 'input', name: 'packageNameSuffix', message: `Package name ${PACKAGE_NAME_BASE}`, // TODO: check for packageName duplicates validate: value => Boolean(value), default: () => uuidv4(), when: ({ useDefaultPackageName }) => !useDefaultPackageName, }, ]); } writing() { this.log('Writing...\n'); this._writingSrc(); this._writingRegularTemplateFiles(); this._writingOther(); this._writingPkgJson(); } _writingSrc() { this.fs.copy( this.templatePath(`src/${this.answers.componentType}/**`), this.destinationPath('src') ); } _writingRegularTemplateFiles() { if (this.answers.componentTemplate === templateTypes.REGULAR) { this.fs.copy(this.templatePath('_nvmrc'), this.destinationPath('.nvmrc')); this.fs.copy(this.templatePath('_npmrc'), this.destinationPath('.npmrc')); this.fs.copy( this.templatePath('_editorconfig'), this.destinationPath('.editorconfig') ); this.fs.copy( this.templatePath('_gitignore'), this.destinationPath('.gitignore') ); this.fs.copy( this.templatePath('_eslintrc.js'), this.destinationPath('.eslintrc.js') ); this.fs.copy( this.templatePath('_stylelintrc.js'), this.destinationPath('.stylelintrc.js') ); } } _writingOther() { const packageName = `${PACKAGE_NAME_BASE}${ this.answers.useDefaultPackageName ? this.packageNameSuffix : this.answers.packageNameSuffix }`; this.fs.copy( this.templatePath('rollup.config.js'), this.destinationPath('rollup.config.js') ); this.fs.copy( this.templatePath('tailwind.config.js'), this.destinationPath('tailwind.config.js') ); this.fs.copyTpl( this.templatePath('README.md'), this.destinationPath('README.md'), { packageName } ); this.fs.copyTpl( this.templatePath('src/meta.js'), this.destinationPath('src/meta.js'), { label: this.answers.label, packageType: this.packageType, } ); } _writingPkgJson() { const packageName = `${PACKAGE_NAME_BASE}${ this.answers.useDefaultPackageName ? this.packageNameSuffix : this.answers.packageNameSuffix }`; this.fs.copyTpl( this.templatePath('_package.json'), this.destinationPath('package.json'), { packageName, } ); const pkgJson = { devDependencies: { '@onereach/orest-rollup-config': DEPENDENCIES['@onereach/orest-rollup-config'], '@onereach/orest-hoc-v-model-to-sync': DEPENDENCIES['@onereach/orest-hoc-v-model-to-sync'], '@onereach/orest-hoc-tailwind': DEPENDENCIES['@onereach/orest-hoc-tailwind'], '@onereach/orest-hoc-variable': DEPENDENCIES['@onereach/orest-hoc-variable'], }, dependencies: { '@onereach/orest-inputs-components': DEPENDENCIES['@onereach/orest-inputs-components'], '@onereach/orest-inputs-utils': DEPENDENCIES['@onereach/orest-inputs-utils'], '@onereach/styles': DEPENDENCIES['@onereach/styles'], '@onereach/ui-components': DEPENDENCIES['@onereach/ui-components'], }, }; this.fs.extendJSON(this.destinationPath('package.json'), pkgJson); } };