UNPKG

devui-admin-test

Version:

Schematics for ng-devui-admin

139 lines 6.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.insertStyleFile = exports.insertHTMLFile = exports.insertTSArray = exports.insertTSFile = exports.readIntoSourceFile = exports.getSourceNodes = void 0; const schematics_1 = require("@angular-devkit/schematics"); const change_1 = require("@schematics/angular/utility/change"); const ts = require("typescript"); function getSourceNodes(sourceFile) { const nodes = [sourceFile]; const result = []; while (nodes.length > 0) { const node = nodes.shift(); if (node) { result.push(node); if (node.getChildCount(sourceFile) >= 0) { nodes.unshift(...node.getChildren()); } } } return result; } exports.getSourceNodes = getSourceNodes; function readIntoSourceFile(tree, filePath) { const text = tree.read(filePath); if (text === null) { throw new schematics_1.SchematicsException(`${filePath} does not exist.`); } const sourceText = text.toString('utf-8'); return ts.createSourceFile(filePath, sourceText, ts.ScriptTarget.Latest, true); } exports.readIntoSourceFile = readIntoSourceFile; function insertTSFile(filePath, type, insertText, targetText) { return (tree) => { const sourceFile = readIntoSourceFile(tree, filePath); const nodes = getSourceNodes(sourceFile); let componentNode = null; let componentNodeSiblings = null; let expressionNode = null; if (type === 'property') { // Find the target position according to kind and text componentNode = nodes.find((n) => n.kind === ts.SyntaxKind.Identifier && n.getText() === targetText); } else { componentNode = nodes.find((n) => n.kind === ts.SyntaxKind.Constructor); } if (!componentNode || !componentNode.parent) { throw new schematics_1.SchematicsException(`expected ${filePath} is a ts file.`); } if (type === 'property') { componentNodeSiblings = componentNode.parent.getChildren(); // Find the position of node in syntax tree. let componentNodeIndex = componentNodeSiblings.indexOf(componentNode); // split syntax tree with node position componentNodeSiblings = componentNodeSiblings.slice(componentNodeIndex); } if (type === 'property' && componentNodeSiblings) { // Find the position of OpenBrackToken expressionNode = componentNodeSiblings.find((n) => n.kind === ts.SyntaxKind.OpenBraceToken); } else if (type === 'method') { // Find the position of Constructor expressionNode = componentNode; } if (!expressionNode) { throw new schematics_1.SchematicsException('The target component node is not defined'); } let change = new change_1.InsertChange(filePath, expressionNode.getEnd(), insertText); const declarationRecorder = tree.beginUpdate(filePath); if (change instanceof change_1.InsertChange) { declarationRecorder.insertRight(change.pos, change.toAdd); } tree.commitUpdate(declarationRecorder); return tree; }; } exports.insertTSFile = insertTSFile; function insertTSArray(tree, path, insertText, targetText, insertPosition = 'end') { const sourceFile = readIntoSourceFile(tree, path); const nodes = getSourceNodes(sourceFile); let componentNodeSiblings = null; let expressionNode = null; // Find the target position according to kind and text const componentNode = nodes.find((n) => (n.kind === ts.SyntaxKind.Identifier || n.kind === ts.SyntaxKind.PropertyAccessExpression) && n.getText() === targetText); if (!componentNode || !componentNode.parent) { throw new schematics_1.SchematicsException(`expected ${path} is a ts file.`); } componentNodeSiblings = componentNode.parent.getChildren(); // Find the position of node in syntax tree. let componentNodeIndex = componentNodeSiblings.indexOf(componentNode); // split syntax tree with node position componentNodeSiblings = componentNodeSiblings.slice(componentNodeIndex); // Find the position of array expressionNode = componentNodeSiblings.find((n) => n.kind === ts.SyntaxKind.ArrayLiteralExpression); if (!expressionNode) { throw new schematics_1.SchematicsException('The target component node is not defined'); } // If you want to insert into an object or array let listNode = expressionNode.getChildren().find((n) => n.kind === ts.SyntaxKind.SyntaxList); if (!listNode) { throw new schematics_1.SchematicsException(`${targetText} list node is not defined`); } let changePosition = insertPosition === 'start' ? listNode.getStart() : listNode.getEnd(); let change = new change_1.InsertChange(path, changePosition, insertText); const declarationRecorder = tree.beginUpdate(path); if (change instanceof change_1.InsertChange) { declarationRecorder.insertRight(change.pos, change.toAdd); } tree.commitUpdate(declarationRecorder); return tree; } exports.insertTSArray = insertTSArray; function insertHTMLFile(filePath, content) { return (tree) => { const buffer = tree.read(filePath); let strContent = ''; if (buffer) strContent = buffer.toString('utf-8'); const appendIndex = strContent.lastIndexOf('</div>'); if (appendIndex < 0) { throw new schematics_1.SchematicsException('Please make sure your template is wrapped within a "div" tag'); } const updatedContent = strContent.slice(0, appendIndex) + `\t${content}\n` + strContent.slice(appendIndex); tree.overwrite(filePath, updatedContent); return tree; }; } exports.insertHTMLFile = insertHTMLFile; function insertStyleFile(filePath, content) { return (tree) => { const buffer = tree.read(filePath); let strContent = ''; if (buffer) strContent = buffer.toString('utf-8'); const updatedContent = strContent + content; tree.overwrite(filePath, updatedContent); return tree; }; } exports.insertStyleFile = insertStyleFile; //# sourceMappingURL=insert.js.map