generator-zionapps
Version:
Angular 9 Code Generator
217 lines (199 loc) • 8.62 kB
JavaScript
'use strict';
const Generator = require('yeoman-generator');
const { paramCase, stringUtils } = require('@zionapps/core');
const prompts = require('../../../utils/prompts');
const {
getAvatarImageUrl,
getComponentNamePrompt,
getFallbackAvatarUrl,
getNgRxStorePath,
getNgRxSubStoreLocation,
getPluralSubStoreNamePrompt,
getSingularSubStoreNamePrompt,
getAddRoutingModuleOptionPrompt,
getNotFoundUrl,
getRoutingComponentFolderPathPrompt,
getListSubTitlePropertyName,
getListTitlePropertyName
} = prompts;
module.exports = class extends Generator {
// Arguments should be defined in the constructor
constructor(args, opts) {
super(args, opts);
// Pass along props from a parent generator
this.props = opts.props || {};
}
// Your initialization methods (checking current project state, getting configs, etc)
initializing() {
this.log('Initializing Angular Table View');
}
// Where you prompt users for options (where you’d call this.prompt())
async prompting() {
const firstProps = await this.prompt([
getComponentNamePrompt(),
getRoutingComponentFolderPathPrompt(),
getSingularSubStoreNamePrompt(),
getPluralSubStoreNamePrompt(),
getNgRxSubStoreLocation()
]);
const { ngRxSubStoreLocation } = firstProps;
let secondProps = {};
if (!ngRxSubStoreLocation) {
secondProps = await this.prompt([getNgRxStorePath()]);
}
const thirdProps = await this.prompt([
getListTitlePropertyName(),
getListSubTitlePropertyName(),
getAvatarImageUrl(),
getFallbackAvatarUrl(),
getAddRoutingModuleOptionPrompt()
]);
const { componentName } = firstProps;
this.props = { ...this.props, ...firstProps, ...secondProps, ...thirdProps };
this.destinationRoot(`${this.props.path}/${paramCase(componentName)}`);
}
// Saving configurations and configure the project (creating .editorconfig files and other metadata files)
configuring() {}
// If the method name doesn't match a priority, it will be pushed to this group.
default() {}
// Where you write the generator specific files (routes, controllers, etc)
writing() {
// Copy smart component
const { componentName, pluralDomainName, singularDomainName } = this.props;
this.fs.copyTpl(
this.templatePath('component.component.html'),
this.destinationPath(`${paramCase(componentName)}.component.html`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('component.component.scss'),
this.destinationPath(`${paramCase(componentName)}.component.scss`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('component.component.ts'),
this.destinationPath(`${paramCase(componentName)}.component.ts`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('component.module.ts'),
this.destinationPath(`${paramCase(componentName)}.module.ts`),
{ ...stringUtils, ...this.props }
);
// Copy presentation component
// eslint-disable-next-line prettier/prettier
const destinationFileName = `components/${paramCase(pluralDomainName)}-responsive-table/${paramCase(pluralDomainName)}-responsive-table`;
this.fs.copyTpl(
this.templatePath('components/item-table/item-table.component.html'),
this.destinationPath(`${destinationFileName}.component.html`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('components/item-table/item-table.component.scss'),
this.destinationPath(`${destinationFileName}.component.scss`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('components/item-table/item-table.component.ts'),
this.destinationPath(`${destinationFileName}.component.ts`),
{ ...stringUtils, ...this.props }
);
// Copy Edit Page
// eslint-disable-next-line prettier/prettier
const editDestinationFileName = `pages/${paramCase(singularDomainName)}-edit/${paramCase(singularDomainName)}-edit`;
this.fs.copyTpl(
this.templatePath('pages/item-edit/item-edit.component.html'),
this.destinationPath(`${editDestinationFileName}.component.html`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('pages/item-edit/item-edit.component.scss'),
this.destinationPath(`${editDestinationFileName}.component.scss`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('pages/item-edit/item-edit.component.ts'),
this.destinationPath(`${editDestinationFileName}.component.ts`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('pages/item-edit/item-edit.module.ts'),
this.destinationPath(`${editDestinationFileName}.module.ts`),
{ ...stringUtils, ...this.props }
);
// Copy View Page
// eslint-disable-next-line prettier/prettier
const viewDestinationFileName = `pages/${paramCase(singularDomainName)}-view/${paramCase(singularDomainName)}-view`;
this.fs.copyTpl(
this.templatePath('pages/item-view/item-view.component.html'),
this.destinationPath(`${viewDestinationFileName}.component.html`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('pages/item-view/item-view.component.scss'),
this.destinationPath(`${viewDestinationFileName}.component.scss`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('pages/item-view/item-view.component.ts'),
this.destinationPath(`${viewDestinationFileName}.component.ts`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath('pages/item-view/item-view.module.ts'),
this.destinationPath(`${viewDestinationFileName}.module.ts`),
{ ...stringUtils, ...this.props }
);
// Copy Form Component
const formTemplatePath = 'pages/item-edit/item-form/item-form';
// eslint-disable-next-line prettier/prettier
const formDestinationFileName = `pages/${paramCase(singularDomainName)}-edit/${paramCase(singularDomainName)}-form/${paramCase(singularDomainName)}-form`;
this.fs.copyTpl(
this.templatePath(`${formTemplatePath}.component.html`),
this.destinationPath(`${formDestinationFileName}.component.html`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath(`${formTemplatePath}.component.scss`),
this.destinationPath(`${formDestinationFileName}.component.scss`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath(`${formTemplatePath}.component.ts`),
this.destinationPath(`${formDestinationFileName}.component.ts`),
{ ...stringUtils, ...this.props }
);
// Copy Widget Component
const widgetTemplatePath = 'pages/item-view/item-detail-widget/item-detail-widget';
// eslint-disable-next-line prettier/prettier
const widgetDestinationFileName = `pages/${paramCase(singularDomainName)}-view/${paramCase(singularDomainName)}-detail-widget/${paramCase(singularDomainName)}-detail-widget`;
this.fs.copyTpl(
this.templatePath(`${widgetTemplatePath}.component.html`),
this.destinationPath(`${widgetDestinationFileName}.component.html`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath(`${widgetTemplatePath}.component.scss`),
this.destinationPath(`${widgetDestinationFileName}.component.scss`),
{ ...stringUtils, ...this.props }
);
this.fs.copyTpl(
this.templatePath(`${widgetTemplatePath}.component.ts`),
this.destinationPath(`${widgetDestinationFileName}.component.ts`),
{ ...stringUtils, ...this.props }
);
}
// Where conflicts are handled (used internally)
conflicts() {}
// Where installations are run (npm, bower)
install() {
// Const angularMaterialInstalledKey = 'angularMaterial5Installed';
// const angularMaterialInstalled = this.config.get(angularMaterialInstalledKey);
// if (angularMaterialInstalled !== 'true') {
// this.npmInstall(['@angular/cdk@latest', '@angular/material@latest'], { save: true });
// this.config.set(angularMaterialInstalledKey, 'true');
// }
}
// Called last, cleanup, say good bye, etc
end() {}
};