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 { options.fn = {}; options.fn.require = require; options.fn.JSON = JSON; return (host: Tree) => { try { // console.log(`cwd `, process.cwd()); // console.log(`options `, options); 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); if (!options.jsonPath) { const formJSONPath = `${process.cwd()}/${options.path}/${options.name}.json`; options.formJSON = require(formJSONPath); } else { options.jsonPath = require(options.jsonPath); } // if relative path that provided by user == null, then use the default one // console.log(`formJSONPath `, formJSONPath); options.name = parsedPath.name; options.path = parsedPath.path; options.formControls = []; options.formControlNames = []; options.fileVar = []; if (!isJSON(options.formJSON)) throw { code: "INVALID_JSON" }; options.formJSON.map((form: any) => { if (getFormControlName(form) != undefined) { options.formControlNames.push(getFormControlName(form)); } else if(getFormControl(form) != undefined){ options.formControls.push(getFormControl(form)); } else if(getFileVar(form) != undefined) { options.fileVar.push({fVar: getFileVar(form), formType: form}); } }); // console.log(`fcn `, options.formControlNames); // console.log(`fc `, options.formControls); // console.log(`parsedPath `, parsedPath); return chain([ (_tree: Tree, context: SchematicContext) => { // console.log(`_tree `, _tree); // console.log(`context `, context); context.logger.info("Bagubagu Form Create: " + 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() : addFormModulesToModule(options) ]); } catch(e) { // console.log(`options.path `, options.path); 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 getFormControlName(obj: any) { switch (obj.formType) { case "select": return obj.select.formControlName; break; case "date": return obj.date.formControlName; break; case "textarea": return obj.textarea.formControlName; break; case "input": return obj.input.formControlName; break; default: return undefined; break; } } function getFormControl(obj: any) { switch (obj.formType) { case "autocomplete": return obj.autocomplete.formControl; break; default: break; } } function getFileVar(obj: any) { switch (obj.formType) { case "fileVideo": return obj.file.video.if; break; case "fileImage": return obj.file.image.if; default: return undefined; break; } } 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 addFormModulesToModule(options: Schema) { return (host: Tree) => { const modulePath = findModuleFromOptions(host, options)!; const matModule = [ 'MatTableModule', 'MatPaginatorModule', 'MatSortModule', 'MatButtonModule', 'MatSnackBarModule', 'MatAutocompleteModule', 'MatFormFieldModule', 'MatProgressBarModule', 'MatIconModule', 'MatInputModule', 'MatDialogModule', ]; const flexModule = ['FlexLayoutModule', 'FlexModule']; const formModule = ['FormsModule', 'ReactiveFormsModule']; formModule.map(form => (() => addModuleImportToModule(host, modulePath, form, '@angular/forms'))()); flexModule.map(flex => (() => addModuleImportToModule(host, modulePath, flex, '@angular/flex-layout'))() ); matModule.map(mat => (() => addModuleImportToModule(host, modulePath, mat, '@angular/material'))()); return host; }; }