import { Rule, Tree, SchematicContext, chain, noop } from "@angular-devkit/schematics"; import { buildComponent, findModuleFromOptions, addModuleImportToModule } from "@angular/cdk/schematics"; import { getProject, buildDefaultPath } from "../utility/project"; import { parseName } from "../utility/parse-name"; import { Schema } from '@schematics/angular/component/schema'; export default function(options: any): Rule { return (host: Tree) => { try { if (!options.project) { const angularJSON = require(`${process.cwd()}/angular.json`); options.project = angularJSON.defaultProject; } const project = getProject(host, options.project); // console.log('project ',options.project); // console.log(`------------------`); // console.log(`builldDefaultPath `, buildDefaultPath(project)); if (options.path === undefined) { options.path = buildDefaultPath(project); } const parsedPath = parseName(options.path, options.name); console.log(`options.jsonPath `, typeof options.jsonPath); if (!options.jsonPath || options.jsonPath.length < 1) { const tableJSONPath = `${process.cwd()}/${options.path}/${options.name}.json`; options.tableJSON = require(tableJSONPath); } else { options.jsonPath = require(options.jsonPath); } options.name = parsedPath.name; options.path = parsedPath.path; options.headerCell = []; if (!isJSON(options.tableJSON)) throw { code: "INVALID_JSON" }; console.log(options.tableJSON); options.tableJSON.map((table: any) => { if (table.headerCell) { options.headerCell.push(table.headerCell); } }); return chain([ (_tree: Tree, context: SchematicContext) => { context.logger.info("Bagubagu Table: " + JSON.stringify(options)); }, buildComponent( { ...options }, { template: "./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html", stylesheet: "./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__" } ), options.skipImport ? noop() : addTableModulesToModule(options) ]); } catch(e) { const jsonPath = options.jsonPath ? options.jsonPath : `${process.cwd()}/${options.path}/${options.name}.json`; if (e.code === 'MODULE_NOT_FOUND') { console.log(`Please make sure that ${jsonPath} is exists`); } else if (e.code === 'INVALID_JSON') { console.log(`JSON that stored in ${jsonPath} is not a valid JSON`); } else if(e.message.includes("'sourceRoot' of undefined")) { console.log(`Please use correct project name!`); } else { console.log(e); } } } } function isJSON(json: any) { let parsed = json; if (typeof json === 'string') { parsed = JSON.parse(json); } return parsed instanceof Array || parsed instanceof Object; } /** * Adds the required modules to the relative module. */ function addTableModulesToModule(options: Schema) { return (host: Tree) => { const modulePath = findModuleFromOptions(host, options)!; let matModule = [ 'MatTableModule', 'MatPaginatorModule', 'MatSortModule', 'MatButtonModule', 'MatSnackBarModule', 'MatAutocompleteModule', 'MatFormFieldModule', 'MatProgressBarModule', 'MatFlexModule' ]; const flexModule = ['FlexLayoutModule', 'FlexModule']; // addModuleImportToModule(host, modulePath, 'MatTableModule', '@angular/material'); // matModule.forEach(mat => { // console.log(`fe `,mat); // }) flexModule.map(flex => (() => addModuleImportToModule(host, modulePath, flex, '@angular/flex-layout'))()) matModule.map(mat => { (() => addModuleImportToModule(host, modulePath, mat, '@angular/material'))(); }); return host; }; }