generator-loom
Version:
Code generator for loom projects
338 lines (289 loc) • 10.9 kB
JavaScript
'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 => {
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 => {
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_main() {
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
}
);
}
// =========== Prompting for table =================
prompting_table() {
let prompts = [{
type: 'input',
name: 'tableCols',
message: 'Type the table columns with comma separation. E.g.: id,name,age : '
}, {
type: 'input',
name: 'rows',
message: 'Number of rows of data you wish to display : '
}]
return this.prompt(prompts).then(props => {
this.props_table = props;
});
}
processing_table() {
var cols = this.props_table.tableCols;
var rows = this.props_table.rows;
this._isNull(cols);
this._isNull(rows);
cols = cols.split(",").map(function(item) {
return item.trim().toLowerCase();
});
cols = cols.filter(Boolean); //Clean up empty cells
this.props_table.tableCols = cols;
this.props_table.rows = rows;
}
_getTableRow(nouns) {
var tableRow = [];
for (var i = 0; i < this.props_table.tableCols.length; i++) {
var r = Math.floor(Math.random() * nouns.length);
tableRow.push(this._capitalize(nouns[r]));
}
return tableRow;
}
name_generator() {
const nouns = ["ninja", "chair", "pancake", "statue", "unicorn", "rainbows", "laser", "senor", "bunny", "captain", "nibblets", "cupcake", "carrot", "gnomes", "glitter", "potato", "salad", "toejam", "curtains", "beets", "toilet", "exorcism", "stick figures", "mermaid eggs", "sea barnacles", "dragons", "jellybeans", "snakes", "dolls", "bushes", "cookies", "apples", "ice cream", "ukulele", "kazoo", "banjo", "opera singer", "circus", "trampoline", "carousel", "carnival", "locomotive", "hot air balloon", "praying mantis", "animator", "artisan", "artist", "colorist", "inker", "coppersmith", "director", "designer", "flatter", "stylist", "leadman", "limner", "make-up artist", "model", "musician", "penciller", "producer", "scenographer", "set decorator", "silversmith", "teacher", "auto mechanic", "beader", "bobbin boy", "clerk of the chapel", "filling station attendant", "foreman", "maintenance engineering", "mechanic", "miller", "moldmaker", "panel beater", "patternmaker", "plant operator", "plumber", "sawfiler", "shop foreman", "soaper", "stationary engineer", "wheelwright", "woodworkers"];
var tableVals = [];
for (var i = 0; i < this.props_table.rows; i++) {
var tableVal = this._getTableRow(nouns);
tableVals.push( tableVal );
}
this.props_table.tableVals = tableVals;
}
// =========== Model Files =================
writing_model() {
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,
tableCols: this.props_table.tableCols,
tableVals: this.props_table.tableVals,
rows: this.props_table.rows
}
);
}
// =========== 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,
tableCols: this.props_table.tableCols,
tableVals: this.props_table.tableVals,
rows: this.props_table.rows
}
);
}
processing_columns() {
var capTableCols = [];
for (var i = 0; i < this.props_table.tableCols.length; i++) {
capTableCols.push( this._capitalize(this.props_table.tableCols[i]) );
}
this.props_table.capTableCols = capTableCols;
}
// =========== List Files =================
writing_list() {
this.fs.copyTpl(
this.templatePath('page-list/page-list.component.ts'),
this.destinationPath(this.props.dirname + '/' + this.props.page + '-list/' +
this.props.page + '-list.component.ts'), {
page: this.props.page,
pages: this.props.pages,
Pages: this.props.Pages,
Page: this.props.Page,
directory: this.props.directory,
tableCols: this.props_table.tableCols,
tableVals: this.props_table.tableVals,
rows: this.props_table.rows,
capTableCols: this.props_table.capTableCols
}
);
this.fs.copyTpl(
this.templatePath('page-list/page-list.component.html'),
this.destinationPath(this.props.dirname + '/' + this.props.page + '-list/' +
this.props.page + '-list.component.html'), {
page: this.props.page,
pages: this.props.pages,
Pages: this.props.Pages,
Page: this.props.Page,
directory: this.props.directory,
tableCols: this.props_table.tableCols,
tableVals: this.props_table.tableVals,
rows: this.props_table.rows,
capTableCols: this.props_table.capTableCols
}
);
this.fs.copyTpl(
this.templatePath('page-list/page-list.component.scss'),
this.destinationPath(this.props.dirname + '/' + this.props.page + '-list/' +
this.props.page + '-list.component.scss'), {
page: this.props.page,
pages: this.props.pages,
Pages: this.props.Pages,
Page: this.props.Page,
directory: this.props.directory,
tableCols: this.props_table.tableCols,
tableVals: this.props_table.tableVals,
rows: this.props_table.rows,
capTableCols: this.props_table.capTableCols
}
);
}
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");
}
};