UNPKG

generator-loom

Version:
216 lines (184 loc) 6.64 kB
'use strict'; const Generator = require('yeoman-generator'); const chalk = require('chalk'); const yosay = require('yosay'); const pluralize = require('pluralize'); module.exports = class extends Generator { prompting_page() { // Have Yeoman greet the user. this.log(yosay( 'Welcome to the grand ' + chalk.red('generator-loom') + ' generator! \n\n' + 'Some questions to set you up.' )); const prompts = [{ type: 'input', name: 'pageInput', message: 'Name of the page you wish to create? (singular-allsmall) E.g. customer: ' }, { type: 'confirm', name: 'isDir', message: 'Do you need this page inside a directory?' }]; return this.prompt(prompts).then(props => { // To access props later use this.props.pageName; this.props_page = props; }); } prompting_dir() { // Have Yeoman greet the user. if(this.props_page.isDir === true) { this.log(yosay( 'Directory details.' )); } let prompts = []; if(this.props_page.isDir === true) { prompts.push({ type: 'input', name: 'directory', message: 'The directory where the page is located. E.g. General : ' }); } return this.prompt(prompts).then(props => { // To access props later use this.props.pageName; this.props_dir = props; }); } _capitalize(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); } _pluralize(str) { return str + 's'; } _isNull(name, strName) { if(!name) { this.log(strName + ' name cannot be empty!'); process.exit(1); return; } } _doesExists(path) { if(this.fs.exists( path )) { this.log("This page already exists at path: " + path); process.exit(1); return; } } writing() { var pageInput = this.props_page.pageInput; var page = pluralize.singular( pageInput.toLowerCase() ); this._isNull(page, "Page"); var Page = this._capitalize( page ); var pages = pluralize.plural( page ); var Pages = this._capitalize( pages ); var directory = null; var apidirname = this.destinationRoot() + '/src/api'; var dirname = this.destinationRoot() + '/src/app/main/content/pages'; if(this.props_dir.hasOwnProperty("directory")) { directory = this.props_dir.directory; this._isNull(directory, "Directory"); directory = pluralize.singular( directory.toLowerCase() ); dirname = dirname + '/' + directory + '/' + pages; } else { dirname = dirname + '/' + pages; } this._doesExists(dirname + '/' + pages + ".component.ts"); // =========== Main Module Files ================= this.fs.copyTpl( this.templatePath('pages.component.ts'), this.destinationPath(dirname + '/' + pages + '.component.ts'), { page: page, pages: pages, Pages: Pages, Page: Page, directory: directory } ); this.fs.copyTpl( this.templatePath('pages.module.ts'), this.destinationPath(dirname + '/' + pages + '.module.ts'), { page: page, pages: pages, Pages: Pages, Page: Page, directory: directory } ); // this.fs.copyTpl( // this.templatePath('_component.html'), // this.destinationPath(dirname + '/' + pluralPageName + '.component.html'), { // pageName: pageName, // pluralPageName: pluralPageName, // capitalizedPluralPageName: capitalizedPluralPageName, // capitalizedPageName: capitalizedPageName // } // ); // this.fs.copyTpl( // this.templatePath('_component.scss'), // this.destinationPath(dirname + '/' + pluralPageName + '.component.scss'), { // pageName: pageName, // pluralPageName: pluralPageName, // capitalizedPluralPageName: capitalizedPluralPageName, // capitalizedPageName: capitalizedPageName // } // ); // this.fs.copyTpl( // this.templatePath('_model.ts'), // this.destinationPath(dirname + '/' + pluralPageName + '.model.ts'), { // pageName: pageName, // pluralPageName: pluralPageName, // capitalizedPluralPageName: capitalizedPluralPageName, // capitalizedPageName: capitalizedPageName // } // ); // this.fs.copyTpl( // this.templatePath('_component.spec.ts'), // this.destinationPath(dirname + '/' + pluralPageName + '.component.spec.ts'), { // pageName: pageName, // pluralPageName: pluralPageName, // capitalizedPluralPageName: capitalizedPluralPageName, // capitalizedPageName: capitalizedPageName // } // ); // this.fs.copyTpl( // this.templatePath('_service.ts'), // this.destinationPath(dirname + '/' + pluralPageName + '.service.ts'), { // pageName: pageName, // pluralPageName: pluralPageName, // capitalizedPluralPageName: capitalizedPluralPageName, // capitalizedPageName: capitalizedPageName // } // ); // this.fs.copyTpl( // this.templatePath('_list/_list.component.html'), // this.destinationPath(dirname + '/' + pageName + '-list/' + pageName + '-list.component.html'), { // pageName: pageName, // pluralPageName: pluralPageName, // capitalizedPluralPageName: capitalizedPluralPageName, // capitalizedPageName: capitalizedPageName // } // ); // this.fs.copyTpl( // this.templatePath('_list/_list.component.scss'), // this.destinationPath(dirname + '/' + pageName + '-list/' + pageName + '-list.component.scss'), { // pageName: pageName, // pluralPageName: pluralPageName, // capitalizedPluralPageName: capitalizedPluralPageName, // capitalizedPageName: capitalizedPageName // } // ); // this.fs.copyTpl( // this.templatePath('_list/_list.component.ts'), // this.destinationPath(dirname + '/' + pageName + '-list/' + pageName + '-list.component.ts'), { // pageName: pageName, // pluralPageName: pluralPageName, // capitalizedPluralPageName: capitalizedPluralPageName, // capitalizedPageName: capitalizedPageName // } // ); } // end() { // this.log(chalk.bold.yellow('You need to manually update these files!')); // this.log("src/app/main/main.module.ts - Add reference to " + this.props.capitalizedPageName + "Module"); // this.log("src/app/navigation/navigation.model.ts - Update sidebar navigation to routes.path in your module file"); // } };