From 4be3ec8bffc9cf0d91e1b16fe05a92f456957752 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Sat, 27 Nov 2021 20:10:26 +0100 Subject: [PATCH] Add roadmap content skeleton generation command --- scripts/content-skeleton.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/scripts/content-skeleton.ts b/scripts/content-skeleton.ts index 6238734cd..48796043b 100644 --- a/scripts/content-skeleton.ts +++ b/scripts/content-skeleton.ts @@ -62,6 +62,7 @@ if (!roadmapDirName) { } const roadmapContentPath = path.join(CONTENT_DIR, roadmapDirName, 'content'); + // If roadmap content already exists do not proceed as it would override the files if (fs.existsSync(roadmapContentPath)) { console.error(`Roadmap content already exists @ ${roadmapContentPath}`); @@ -149,10 +150,25 @@ controls.forEach((control) => { prepareDirTree(control, dirTree, dirSortOrders); }); -function createDirTree(parentDir: string, dirTree: DirTreeType) { +function createDirTree(parentDir: string, dirTree: DirTreeType, sortOrders: DirSortOrdersType) { const childrenDirNames = Object.keys(dirTree); 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 (!hasChildren) { fs.writeFileSync(`${parentDir}.md`, ''); @@ -167,10 +183,12 @@ function createDirTree(parentDir: string, dirTree: DirTreeType) { // For each of the directory names, create a // directory inside the given directory childrenDirNames.forEach((dirName) => { - createDirTree(dirName, dirTree[dirName]); + createDirTree( + path.join(parentDir, dirName), + dirTree[dirName], + dirSortOrders + ); }); } -createDirTree(roadmapContentPath, dirTree); - -console.log(JSON.stringify(dirTree, null, 2)); +createDirTree(roadmapContentPath, dirTree, dirSortOrders);