Add functionality to create content directory for a roadmap

pull/3278/head
Kamran Ahmed 2 years ago
parent 5d164198d4
commit a67a27299e
  1. 163
      bin/roadmap-content.cjs
  2. 1
      src/roadmaps/spring-boot/content/100-spring-core/100-introduction/100-terminology.md
  3. 1
      src/roadmaps/spring-boot/content/100-spring-core/100-introduction/101-architecture.md
  4. 1
      src/roadmaps/spring-boot/content/100-spring-core/100-introduction/102-why-spring.md
  5. 1
      src/roadmaps/spring-boot/content/100-spring-core/100-introduction/index.md
  6. 1
      src/roadmaps/spring-boot/content/100-spring-core/101-configuration.md
  7. 1
      src/roadmaps/spring-boot/content/100-spring-core/102-dependency-injection.md
  8. 1
      src/roadmaps/spring-boot/content/100-spring-core/103-spring-ioc.md
  9. 1
      src/roadmaps/spring-boot/content/100-spring-core/104-spring-aop.md
  10. 1
      src/roadmaps/spring-boot/content/100-spring-core/105-spring-mvc.md
  11. 1
      src/roadmaps/spring-boot/content/100-spring-core/106-annotations.md
  12. 1
      src/roadmaps/spring-boot/content/100-spring-core/107-spring-bean-scope.md
  13. 1
      src/roadmaps/spring-boot/content/100-spring-core/index.md
  14. 1
      src/roadmaps/spring-boot/content/101-spring-security/100-authentication.md
  15. 1
      src/roadmaps/spring-boot/content/101-spring-security/101-authorization.md
  16. 1
      src/roadmaps/spring-boot/content/101-spring-security/102-oauth2.md
  17. 1
      src/roadmaps/spring-boot/content/101-spring-security/103-jwt-authentication.md
  18. 1
      src/roadmaps/spring-boot/content/101-spring-security/index.md
  19. 1
      src/roadmaps/spring-boot/content/102-spring-boot-starters.md
  20. 1
      src/roadmaps/spring-boot/content/103-autconfiguration.md
  21. 1
      src/roadmaps/spring-boot/content/104-spring-boot-actuators.md
  22. 1
      src/roadmaps/spring-boot/content/105-embedded-server.md
  23. 1
      src/roadmaps/spring-boot/content/106-hibernate/100-transactions.md
  24. 1
      src/roadmaps/spring-boot/content/106-hibernate/101-relationships.md
  25. 1
      src/roadmaps/spring-boot/content/106-hibernate/102-entity-lifecycle.md
  26. 1
      src/roadmaps/spring-boot/content/106-hibernate/index.md
  27. 1
      src/roadmaps/spring-boot/content/107-spring-data/100-spring-data-jpa.md
  28. 1
      src/roadmaps/spring-boot/content/107-spring-data/101-spring-data-mongodb.md
  29. 1
      src/roadmaps/spring-boot/content/107-spring-data/102-spring-data-jdbc.md
  30. 1
      src/roadmaps/spring-boot/content/107-spring-data/index.md
  31. 1
      src/roadmaps/spring-boot/content/108-microservices/100-spring-cloud/100-spring-cloud-gateway.md
  32. 1
      src/roadmaps/spring-boot/content/108-microservices/100-spring-cloud/101-cloud-config.md
  33. 1
      src/roadmaps/spring-boot/content/108-microservices/100-spring-cloud/102-spring-cloud-circuit-breaker.md
  34. 1
      src/roadmaps/spring-boot/content/108-microservices/100-spring-cloud/103-spring-cloud-openfeign.md
  35. 1
      src/roadmaps/spring-boot/content/108-microservices/100-spring-cloud/104-hystrix.md
  36. 1
      src/roadmaps/spring-boot/content/108-microservices/100-spring-cloud/105-sleuth.md
  37. 1
      src/roadmaps/spring-boot/content/108-microservices/100-spring-cloud/106-eureka.md
  38. 1
      src/roadmaps/spring-boot/content/108-microservices/100-spring-cloud/index.md
  39. 1
      src/roadmaps/spring-boot/content/108-microservices/index.md
  40. 1
      src/roadmaps/spring-boot/content/109-spring-mvc/100-servlet.md
  41. 1
      src/roadmaps/spring-boot/content/109-spring-mvc/101-jsp-files.md
  42. 1
      src/roadmaps/spring-boot/content/109-spring-mvc/102-architecture.md
  43. 1
      src/roadmaps/spring-boot/content/109-spring-mvc/103-components.md
  44. 1
      src/roadmaps/spring-boot/content/109-spring-mvc/index.md
  45. 1
      src/roadmaps/spring-boot/content/110-testing/100-jpa-test.md
  46. 1
      src/roadmaps/spring-boot/content/110-testing/101-mock-mvc.md
  47. 1
      src/roadmaps/spring-boot/content/110-testing/102-spring-boot-test-annotation.md
  48. 1
      src/roadmaps/spring-boot/content/110-testing/103-mockbean-annotation.md
  49. 1
      src/roadmaps/spring-boot/content/110-testing/index.md

@ -0,0 +1,163 @@
const fs = require('fs');
const path = require('path');
const CONTENT_DIR = path.join(__dirname, '../content');
// Directory containing the roadmaps
const ROADMAP_CONTENT_DIR = path.join(__dirname, '../src/roadmaps');
const roadmapId = process.argv[2];
const allowedRoadmapIds = fs.readdirSync(ROADMAP_CONTENT_DIR);
if (!roadmapId) {
console.error('roadmapId is required');
process.exit(1);
}
if (!allowedRoadmapIds.includes(roadmapId)) {
console.error(`Invalid roadmap key ${roadmapId}`);
console.error(`Allowed keys are ${allowedRoadmapIds.join(', ')}`);
process.exit(1);
}
// Directory holding the roadmap content files
const roadmapDirName = fs
.readdirSync(ROADMAP_CONTENT_DIR)
.find((dirName) => dirName.replace(/\d+-/, '') === roadmapId);
if (!roadmapDirName) {
console.error('Roadmap directory not found');
process.exit(1);
}
const roadmapDirPath = path.join(ROADMAP_CONTENT_DIR, roadmapDirName);
const roadmapContentDirPath = path.join(
ROADMAP_CONTENT_DIR,
roadmapDirName,
'content'
);
// If roadmap content already exists do not proceed as it would override the files
if (fs.existsSync(roadmapContentDirPath)) {
console.error(`Roadmap content already exists @ ${roadmapContentDirPath}`);
process.exit(1);
}
function prepareDirTree(control, dirTree, dirSortOrders) {
// Directories are only created for groups
if (control.typeID !== '__group__') {
return;
}
// e.g. 104-testing-your-apps:other-options
const controlName = control?.properties?.controlName || '';
// e.g. 104
const sortOrder = controlName.match(/^\d+/)?.[0];
// No directory for a group without control name
if (!controlName || !sortOrder) {
return;
}
// e.g. testing-your-apps:other-options
const controlNameWithoutSortOrder = controlName.replace(/^\d+-/, '');
// e.g. ['testing-your-apps', 'other-options']
const dirParts = controlNameWithoutSortOrder.split(':');
// Nest the dir path in the dirTree
let currDirTree = dirTree;
dirParts.forEach((dirPart) => {
currDirTree[dirPart] = currDirTree[dirPart] || {};
currDirTree = currDirTree[dirPart];
});
dirSortOrders[controlNameWithoutSortOrder] = Number(sortOrder);
const childrenControls = control.children.controls.control;
// No more children
if (childrenControls.length) {
childrenControls.forEach((childControl) => {
prepareDirTree(childControl, dirTree, dirSortOrders);
});
}
return { dirTree, dirSortOrders };
}
const roadmap = require(path.join(__dirname, `../public/jsons/${roadmapId}`));
const controls = roadmap.mockup.controls.control;
// Prepare the dir tree that we will be creating and also calculate the sort orders
const dirTree = {};
const dirSortOrders = {};
controls.forEach((control) => {
prepareDirTree(control, dirTree, dirSortOrders);
});
/**
* @param parentDir Parent directory in which directory is to be created
* @param dirTree Nested dir tree to be created
* @param sortOrders Mapping from groupName to sort order
* @param filePaths The mapping from groupName to file path
*/
function createDirTree(parentDir, dirTree, sortOrders, filePaths = {}) {
const childrenDirNames = Object.keys(dirTree);
const hasChildren = childrenDirNames.length !== 0;
// @todo write test for this, yolo for now
const groupName = parentDir
.replace(roadmapContentDirPath, '') // Remove base dir path
.replace(/(^\/)|(\/$)/g, '') // Remove trailing slashes
.replace(/(^\d+?-)/g, '') // Remove sorting information
.replaceAll('/', ':') // Replace slashes with `:`
.replace(/:\d+-/, ':');
const humanizedGroupName = groupName
.split(':')
.pop()
?.replaceAll('-', ' ')
.replace(/^\w/, ($0) => $0.toUpperCase());
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) {
let fileName = `${parentDir}.md`;
fs.writeFileSync(fileName, `# ${humanizedGroupName}`);
filePaths[groupName || 'home'] = fileName.replace(CONTENT_DIR, '');
return filePaths;
}
// There *are* children, so create the parent as a directory
// and create `index.md` as the content file for this
fs.mkdirSync(parentDir);
let readmeFilePath = path.join(parentDir, 'index.md');
fs.writeFileSync(readmeFilePath, `# ${humanizedGroupName}`);
filePaths[groupName || 'home'] = readmeFilePath.replace(CONTENT_DIR, '');
// For each of the directory names, create a
// directory inside the given directory
childrenDirNames.forEach((dirName) => {
createDirTree(
path.join(parentDir, dirName),
dirTree[dirName],
dirSortOrders,
filePaths
);
});
return filePaths;
}
// Create directories and get back the paths for created directories
createDirTree(roadmapContentDirPath, dirTree, dirSortOrders);
console.log('Created roadmap content directory structure');
Loading…
Cancel
Save