Files
developer-roadmap/scripts/roadmaps-meta.js

93 lines
3.7 KiB
JavaScript
Raw Normal View History

2019-11-28 00:19:03 +04:00
const fs = require('fs');
const path = require('path');
2019-12-01 15:46:13 +04:00
const exec = require('child_process').execSync;
2019-11-28 00:19:03 +04:00
2020-01-18 22:47:48 +04:00
const STORAGE_PATH = path.join(__dirname, '../content');
const ROADMAPS_PATH = path.join(__dirname, '../content/roadmaps');
const META_FILE_PATH = path.join(__dirname, '../content/roadmaps.json');
2019-11-28 00:19:03 +04:00
const roadmapDirs = fs.readdirSync(ROADMAPS_PATH);
console.log(`[x] ${roadmapDirs.length} roadmaps found`);
const roadmapsMeta = roadmapDirs.reduce((metaAcc, roadmapDirName) => {
const roadmapSlug = roadmapDirName.replace(/^\d+-/, '');
const roadmapDir = path.join(ROADMAPS_PATH, roadmapDirName);
const roadmapMeta = require(path.join(roadmapDir, 'meta.json'));
2020-01-18 22:47:48 +04:00
// We can't use the absolute path in the build e.g. ~/Users/user/where-build-is-running/content
// So, we remove it and use the path relative to content directory
2020-01-28 17:48:22 +05:00
const roadmapLandingFilePath = path.join(roadmapDir.replace(STORAGE_PATH, ''), roadmapMeta.path);
2020-02-29 17:49:04 +04:00
const resourcesPath = path.join(roadmapDir.replace(STORAGE_PATH, ''), roadmapMeta.resources);
2019-12-01 16:16:45 +04:00
2019-12-01 15:46:13 +04:00
const contributors = exec(`git log --pretty=format:"%an%x09" ${roadmapDir} | uniq`)
.toString()
.split('\n')
.map(contributor => contributor.replace(/[\s\t]/g, ' ').trim()) || [];
const contributorNames = contributors.filter(contributor => !!contributor);
2019-11-28 00:19:03 +04:00
console.log(`----------------------------`);
console.log(`[#] Roadmap: ${roadmapMeta.title}`);
console.log(`[x] Generating sidebar`);
// Get all the directories in the roadmap dir
// these are going to be the parent menu items
const menuDirs = fs.readdirSync(roadmapDir)
.map(dirPath => path.join(roadmapDir, dirPath))
.filter(dirPath => fs.lstatSync(dirPath).isDirectory());
// Read the files inside each of the menu dirs and prepare menu items
// for the sidebar i.e. of the format below
// {
// landscape: [{ title: "Junior", path: "/path/to/file.md"}, ...],
// learn: [{ title: "Job Ready", path: "/path/to/file.md"}, ...],
// }
const sidebar = menuDirs.reduce((menus, menuDir) => {
2019-12-06 18:18:33 +04:00
const menuItemName = path.basename(menuDir).replace(/\d+-/, '').replace('-', ' ');
2019-11-28 00:19:03 +04:00
const pageFiles = fs.readdirSync(menuDir)
.filter(pageFileName => pageFileName.endsWith('.md'))
// Sort by the titles `1-something.md, 2-another.md`
.sort((a, b) => parseInt(a, 10) - parseInt(b, 10));
return {
...menus,
[menuItemName]: [
...(menus[menuItemName] || []),
...pageFiles.map(pageFile => {
const pageFileName = path.basename(pageFile, '.md');
const pageSlug = pageFileName.replace(/^\d+-/, '').toLowerCase();
return {
// Use the filename and remove the number from the beginning to generate slug
url: `/${roadmapSlug}/${pageSlug}`,
// Remove the number and replace the dashes to generate title
title: pageFileName.replace(/^\d+-/, '').replace(/-/g, ' '),
// Remove "STORAGE_PATH" because we don't want to push the absolute path while development
path: path.join(menuDir, pageFile).replace(STORAGE_PATH, ''),
}
}),
],
};
}, {});
return [
...metaAcc,
{
...roadmapMeta,
2019-12-01 15:46:13 +04:00
contributorsCount: contributorNames.length,
contributorsUrl: `/${roadmapSlug}/contributors`,
2019-11-28 00:19:03 +04:00
url: `/${roadmapSlug}`,
2020-01-28 17:48:22 +05:00
path: roadmapLandingFilePath,
2020-02-29 17:49:04 +04:00
resources: resourcesPath,
2019-11-28 00:19:03 +04:00
sidebar,
},
];
}, []);
console.log(`----------------------------`);
console.log(`[x] Meta generated for ${roadmapsMeta.length} roadmaps`);
console.log(`[x] Writing file ${META_FILE_PATH}`);
fs.writeFileSync(META_FILE_PATH, JSON.stringify(roadmapsMeta, null, 2));
console.log(`[x] Wrote file with content`);
console.log(JSON.stringify(roadmapsMeta, null, 2));