@ng-doc/builder
Version:
<!-- PROJECT LOGO --> <br /> <div align="center"> <a href="https://github.com/ng-doc/ng-doc"> <img src="https://ng-doc.com/assets/images/ng-doc.svg?raw=true" alt="Logo" height="150px"> </a>
89 lines • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePages = updatePages;
const tslib_1 = require("tslib");
const workspace_1 = require("@schematics/angular/utility/workspace");
const ng_morph_1 = require("ng-morph");
const path_1 = tslib_1.__importDefault(require("path"));
/**
*
*/
function updatePages() {
return async (tree) => {
return (0, workspace_1.updateWorkspace)(() => {
(0, ng_morph_1.setActiveProject)((0, ng_morph_1.createProject)(tree, './', ['**/**/ng-doc.page.ts']));
const sourceFiles = (0, ng_morph_1.getSourceFiles)('**/**/ng-doc.page.ts');
for (const sourceFile of sourceFiles) {
const objectExpression = getObjectExpressionFromDefault(sourceFile);
if (!objectExpression) {
continue;
}
removeTitleFromMarkdown(tree, objectExpression);
moveKeywordIntoMarkdown(tree, objectExpression);
}
(0, ng_morph_1.saveActiveProject)();
});
};
}
/**
*
* @param sourceFile
*/
function getObjectExpressionFromDefault(sourceFile) {
const defaultExport = sourceFile
.getExportedDeclarations()
?.get('default')?.[0];
if (ng_morph_1.Node.isVariableDeclaration(defaultExport)) {
return defaultExport?.getFirstChildByKindOrThrow(ng_morph_1.SyntaxKind.ObjectLiteralExpression);
}
return undefined;
}
/**
*
* @param tree
* @param objectExpression
*/
function removeTitleFromMarkdown(tree, objectExpression) {
const directoryPath = objectExpression.getSourceFile().getDirectoryPath();
const mdPathProperty = objectExpression.getProperty('mdFile');
if (mdPathProperty && ng_morph_1.Node.isPropertyAssignment(mdPathProperty)) {
const mdPathValue = mdPathProperty.getInitializer()?.getText();
if (mdPathValue) {
const mdPath = path_1.default.join(directoryPath, mdPathValue.replace(/['"]/g, ''));
const mdFile = tree.readText(mdPath);
if (mdFile) {
const newMdFile = mdFile.replace(/^\s*#?(\s)*{{(\s)*NgDocPage\.title(\s)*}}/m, '');
tree.overwrite(mdPath, newMdFile);
console.log('Removed title from:', mdPath);
}
}
}
}
/**
*
* @param tree
* @param objectExpression
*/
function moveKeywordIntoMarkdown(tree, objectExpression) {
const directoryPath = objectExpression.getSourceFile().getDirectoryPath();
const mdPathProperty = objectExpression.getProperty('mdFile');
const keywordProperty = objectExpression.getProperty('keyword');
if (mdPathProperty &&
ng_morph_1.Node.isPropertyAssignment(mdPathProperty) &&
keywordProperty &&
ng_morph_1.Node.isPropertyAssignment(keywordProperty)) {
const mdPathValue = mdPathProperty.getInitializer()?.getText();
const keywordValue = keywordProperty.getInitializer()?.getText();
if (mdPathValue && keywordValue) {
const mdPath = path_1.default.join(directoryPath, mdPathValue.replace(/['"]/g, ''));
const mdFile = tree.readText(mdPath);
if (mdFile) {
const newMdFile = `---\nkeyword: ${keywordValue}\n---\n${mdFile}`;
tree.overwrite(mdPath, newMdFile);
}
keywordProperty.remove();
console.log('Moved keyword into:', mdPath);
}
}
}
//# sourceMappingURL=update-pages.js.map