UNPKG

generator-loom

Version:
214 lines (183 loc) 6.06 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?' }, { 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(); } _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; } } processing() { var pageInput = this.props_page.pageInput; this._isNull(pageInput, "Page"); var page = pluralize.singular( pageInput.trim().toLowerCase() ); 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.trim().toLowerCase() ); dirname = dirname + '/' + directory + '/' + pages; } else { dirname = dirname + '/' + pages; } this._doesExists(dirname + '/' + pages + ".component.ts"); this.props = { apidirname: apidirname, dirname: dirname, page: page, Page: Page, pages: pages, Pages: Pages, directory: directory }; } // =========== Main Module Files ================= writing_main() { this.fs.copyTpl( this.templatePath('pages.component.ts'), this.destinationPath(this.props.dirname + '/' + this.props.pages + '.component.ts'), { page: this.props.page, pages: this.props.pages, Pages: this.props.Pages, Page: this.props.Page, directory: this.props.directory } ); this.fs.copyTpl( this.templatePath('pages.module.ts'), this.destinationPath(this.props.dirname + '/' + this.props.pages + '.module.ts'), { page: this.props.page, pages: this.props.pages, Pages: this.props.Pages, Page: this.props.Page, directory: this.props.directory } ); this.fs.copyTpl( this.templatePath('pages.component.spec.ts'), this.destinationPath(this.props.dirname + '/' + this.props.pages + '.component.spec.ts'), { page: this.props.page, pages: this.props.pages, Pages: this.props.Pages, Page: this.props.Page, directory: this.props.directory } ); this.fs.copyTpl( this.templatePath('pages.component.html'), this.destinationPath(this.props.dirname + '/' + this.props.pages + '.component.html'), { page: this.props.page, pages: this.props.pages, Pages: this.props.Pages, Page: this.props.Page, directory: this.props.directory } ); this.fs.copyTpl( this.templatePath('pages.component.scss'), this.destinationPath(this.props.dirname + '/' + this.props.pages + '.component.scss'), { page: this.props.page, pages: this.props.pages, Pages: this.props.Pages, Page: this.props.Page, directory: this.props.directory } ); this.fs.copyTpl( this.templatePath('pages.service.ts'), this.destinationPath(this.props.dirname + '/' + this.props.pages + '.service.ts'), { page: this.props.page, pages: this.props.pages, Pages: this.props.Pages, Page: this.props.Page, directory: this.props.directory } ); this.fs.copyTpl( this.templatePath('pages.model.ts'), this.destinationPath(this.props.dirname + '/' + this.props.pages + '.model.ts'), { page: this.props.page, pages: this.props.pages, Pages: this.props.Pages, Page: this.props.Page, directory: this.props.directory } ); } // =========== Dummy Data Files ================= writing_data() { this.fs.copyTpl( this.templatePath('data/pages-data.json'), this.destinationPath(this.props.apidirname + '/' + this.props.pages + '-data.json'), { page: this.props.page, pages: this.props.pages, Pages: this.props.Pages, Page: this.props.Page, directory: this.props.directory } ); } end() { this.log(chalk.bold.yellow('\n\nYou need to manually update these files!\n')); this.log("src/app/main/main.module.ts - Add a reference to " + this.props.Page + "Module in this file."); this.log("src/app/navigation/navigation.model.ts - Update sidebar navigation to routes.path in your module file\n"); } };