Add roadmap content skeleton generation command

pull/1657/head
Kamran Ahmed 3 years ago
parent 4c0a673488
commit 4be3ec8bff
  1. 28
      scripts/content-skeleton.ts

@ -62,6 +62,7 @@ if (!roadmapDirName) {
} }
const roadmapContentPath = path.join(CONTENT_DIR, roadmapDirName, 'content'); const roadmapContentPath = path.join(CONTENT_DIR, roadmapDirName, 'content');
// If roadmap content already exists do not proceed as it would override the files // If roadmap content already exists do not proceed as it would override the files
if (fs.existsSync(roadmapContentPath)) { if (fs.existsSync(roadmapContentPath)) {
console.error(`Roadmap content already exists @ ${roadmapContentPath}`); console.error(`Roadmap content already exists @ ${roadmapContentPath}`);
@ -149,10 +150,25 @@ controls.forEach((control) => {
prepareDirTree(control, dirTree, dirSortOrders); prepareDirTree(control, dirTree, dirSortOrders);
}); });
function createDirTree(parentDir: string, dirTree: DirTreeType) { function createDirTree(parentDir: string, dirTree: DirTreeType, sortOrders: DirSortOrdersType) {
const childrenDirNames = Object.keys(dirTree); const childrenDirNames = Object.keys(dirTree);
const hasChildren = childrenDirNames.length !== 0; const hasChildren = childrenDirNames.length !== 0;
const groupName = parentDir
.replace(roadmapContentPath, '') // Remove base dir path
.replace(/(^\/)|(\/$)/g, '') // Remove trailing slashes
.replace(/(^\d+?-)/g, '') // Remove sorting information
.replace('/', ':'); // Replace slashes with `:`
const sortOrder = sortOrders[groupName] || '';
// Attach sorting information to dirname
// e.g. /roadmaps/100-frontend/content/internet
// ———> /roadmaps/100-frontend/content/103-internet
if (sortOrder) {
parentDir = parentDir.replace(/(.+?)([^\/]+)?$/, `$1${sortOrder}-$2`);
}
// If no children, create a file for this under the parent directory // If no children, create a file for this under the parent directory
if (!hasChildren) { if (!hasChildren) {
fs.writeFileSync(`${parentDir}.md`, ''); fs.writeFileSync(`${parentDir}.md`, '');
@ -167,10 +183,12 @@ function createDirTree(parentDir: string, dirTree: DirTreeType) {
// For each of the directory names, create a // For each of the directory names, create a
// directory inside the given directory // directory inside the given directory
childrenDirNames.forEach((dirName) => { childrenDirNames.forEach((dirName) => {
createDirTree(dirName, dirTree[dirName]); createDirTree(
path.join(parentDir, dirName),
dirTree[dirName],
dirSortOrders
);
}); });
} }
createDirTree(roadmapContentPath, dirTree); createDirTree(roadmapContentPath, dirTree, dirSortOrders);
console.log(JSON.stringify(dirTree, null, 2));

Loading…
Cancel
Save