devui-admin-test
Version:
Schematics for ng-devui-admin
103 lines • 5.47 kB
JavaScript
;
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 = utils_1.readIntoSourceFile(tree, filePath);
const nodes = 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(schematics_1.schematic('plugin', {
modulePath: modulePath,
insertModuleName: m,
}));
});
return 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 schematics_1.chain([
utils_1.insertTSFile(`${options.dirPath}/${utils_1.getPageNameFromFilePath(options.dirPath)}.component.ts`, 'property', blockContent.properties, `${core_1.strings.classify(utils_1.getPageNameFromFilePath(options.dirPath))}Component`),
utils_1.insertTSFile(`${options.dirPath}/${utils_1.getPageNameFromFilePath(options.dirPath)}.component.ts`, 'method', blockContent.methods),
utils_1.insertHTMLFile(`${options.dirPath}/${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`)),
utils_1.insertStyleFile(`${options.dirPath}/${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) => {
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 = find_module_1.buildRelativePath(modulePath, `/${options.dirPath}/${core_1.strings.dasherize(options.blockName)}/${core_1.strings.dasherize(options.blockName)}.module`);
return schematics_1.chain([
utils_1.addModule(modulePath, `${core_1.strings.classify(options.blockName)}Module`, relativeModulePath),
utils_1.insertHTMLFile(`${options.dirPath}/${utils_1.getPageNameFromFilePath(options.dirPath)}.component.html`, `<${blockInfo.selector}></${blockInfo.selector}>`),
]);
}
}
function default_1(options) {
return (tree) => {
const modulePath = 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 schematics_1.chain([insertOptions(tree, options, blockInfo, modulePath)]);
};
}
exports.default = default_1;
//# sourceMappingURL=index.js.map