UNPKG

ng-devui-admin

Version:

Schematics for ng-devui-admin

110 lines 6.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const find_module_1 = require("@schematics/angular/utility/find-module"); const utils_1 = require("../utils"); const fs = require("fs"); const path = require("path"); const ts = require("typescript"); function readFileIntoString(tree, filePath) { const buffer = tree.read(filePath); let strContent = ''; if (buffer) strContent = buffer.toString('utf-8'); return strContent; } function readTsFileContent(tree, filePath) { const methods = []; const properties = []; const sourceFile = (0, utils_1.readIntoSourceFile)(tree, filePath); const nodes = (0, utils_1.getSourceNodes)(sourceFile); nodes.forEach((n) => { if (n.kind === ts.SyntaxKind.MethodDeclaration) { methods.push('\n\t' + n.getText()); } if (n.kind === ts.SyntaxKind.PropertyDeclaration) { properties.push('\n\t' + n.getText()); } }); return { methods: methods.join(''), properties: properties.join('') + '\n', }; } function genAddModulesRules(modules, modulePath) { let rules = []; modules.forEach((m) => { rules.push((0, schematics_1.schematic)('plugin', { modulePath: modulePath, insertModuleName: m, })); }); return (0, schematics_1.chain)(rules); } function getBlockInfo(tree, blockPath) { let blockPackage = null; const blockPackageBuffer = tree.read(blockPath); if (blockPackageBuffer) { blockPackage = JSON.parse(blockPackageBuffer.toString()); } else { throw new schematics_1.SchematicsException(`Cannot find block ${blockPath}, please download first.`); } return { componentModules: blockPackage['blockConfig']['componentModules'], selector: blockPackage['blockConfig']['selector'], }; } function writeBlock(tree, srcPath, desPath) { let buffer = tree.read(srcPath); if (buffer) { tree.create(desPath, buffer); } } function insertOptions(tree, options, blockInfo, modulePath) { const blockContent = readTsFileContent(tree, `node_modules/ng-devui-materials/${core_1.strings.dasherize(options.blockName)}/src/${core_1.strings.dasherize(options.blockName)}.component.ts`); if (options.importWay === 'by-code') { return (0, schematics_1.chain)([ (0, utils_1.insertTSFile)(`${options.dirPath}/${(0, utils_1.getPageNameFromFilePath)(options.dirPath)}.component.ts`, 'property', blockContent.properties, `${core_1.strings.classify((0, utils_1.getPageNameFromFilePath)(options.dirPath))}Component`), (0, utils_1.insertTSFile)(`${options.dirPath}/${(0, utils_1.getPageNameFromFilePath)(options.dirPath)}.component.ts`, 'method', blockContent.methods), (0, utils_1.insertHTMLFile)(`${options.dirPath}/${(0, utils_1.getPageNameFromFilePath)(options.dirPath)}.component.html`, readFileIntoString(tree, `node_modules/ng-devui-materials/${core_1.strings.dasherize(options.blockName)}/src/${core_1.strings.dasherize(options.blockName)}.component.html`)), (0, utils_1.insertStyleFile)(`${options.dirPath}/${(0, utils_1.getPageNameFromFilePath)(options.dirPath)}.component.scss`, readFileIntoString(tree, `node_modules/ng-devui-materials/${core_1.strings.dasherize(options.blockName)}/src/${core_1.strings.dasherize(options.blockName)}.component.scss`)), genAddModulesRules(blockInfo.componentModules, modulePath), ]); } else { const srcPath = path.join(__dirname, '..', '..', '..', '..', 'node_modules', `ng-devui-materials/${core_1.strings.dasherize(options.blockName)}/src`); fs.readdirSync(srcPath).forEach((p) => { if (fs.statSync(`${srcPath}/${p}`).isDirectory()) { fs.readdirSync(`${srcPath}/${p}`).forEach((d) => { if (fs.statSync(`${srcPath}/${p}/${d}`).isDirectory()) { fs.readdirSync(`${srcPath}/${p}/${d}`).forEach((t) => { writeBlock(tree, `node_modules/ng-devui-materials/${core_1.strings.dasherize(options.blockName)}/src/${p}/${d}/${t}`, `${options.dirPath}/${core_1.strings.dasherize(options.blockName)}/${p}/${d}/${t}`); }); } else { writeBlock(tree, `node_modules/ng-devui-materials/${core_1.strings.dasherize(options.blockName)}/src/${p}/${d}`, `${options.dirPath}/${core_1.strings.dasherize(options.blockName)}/${p}/${d}`); } }); } else { writeBlock(tree, `node_modules/ng-devui-materials/${core_1.strings.dasherize(options.blockName)}/src/${p}`, `${options.dirPath}/${core_1.strings.dasherize(options.blockName)}/${p}`); } }); const relativeModulePath = (0, find_module_1.buildRelativePath)(modulePath, `/${options.dirPath}/${core_1.strings.dasherize(options.blockName)}/${core_1.strings.dasherize(options.blockName)}.module`); return (0, schematics_1.chain)([ (0, utils_1.addModule)(modulePath, `${core_1.strings.classify(options.blockName)}Module`, relativeModulePath), (0, utils_1.insertHTMLFile)(`${options.dirPath}/${(0, utils_1.getPageNameFromFilePath)(options.dirPath)}.component.html`, `<${blockInfo.selector}></${blockInfo.selector}>`), ]); } } function default_1(options) { return (tree) => { const modulePath = (0, find_module_1.findModule)(tree, options.dirPath); const blockInfo = getBlockInfo(tree, `node_modules/ng-devui-materials/${core_1.strings.dasherize(options.blockName)}/package.json`); return (0, schematics_1.chain)([insertOptions(tree, options, blockInfo, modulePath)]); }; } exports.default = default_1; //# sourceMappingURL=index.js.map