Skip to content

Commit 3461b59

Browse files
Merge pull request #61 from kamranahmedse/master
Create a new pull request by comparing changes across two branches
2 parents ce7638f + e3adcda commit 3461b59

File tree

211 files changed

+1799
-524
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

211 files changed

+1799
-524
lines changed

bin/best-practice-content.cjs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const CONTENT_DIR = path.join(__dirname, '../content');
5+
// Directory containing the best-practices
6+
const BEST_PRACTICE_CONTENT_DIR = path.join(__dirname, '../src/best-practices');
7+
const bestPracticeId = process.argv[2];
8+
9+
const allowedBestPracticeId = fs.readdirSync(BEST_PRACTICE_CONTENT_DIR);
10+
if (!bestPracticeId) {
11+
console.error('bestPractice is required');
12+
process.exit(1);
13+
}
14+
15+
if (!allowedBestPracticeId.includes(bestPracticeId)) {
16+
console.error(`Invalid best practice key ${bestPracticeId}`);
17+
console.error(`Allowed keys are ${allowedBestPracticeId.join(', ')}`);
18+
process.exit(1);
19+
}
20+
21+
// Directory holding the best parctice content files
22+
const bestPracticeDirName = fs
23+
.readdirSync(BEST_PRACTICE_CONTENT_DIR)
24+
.find((dirName) => dirName.replace(/\d+-/, '') === bestPracticeId);
25+
26+
if (!bestPracticeDirName) {
27+
console.error('Best practice directory not found');
28+
process.exit(1);
29+
}
30+
31+
const bestPracticeDirPath = path.join(BEST_PRACTICE_CONTENT_DIR, bestPracticeDirName);
32+
const bestPracticeContentDirPath = path.join(
33+
BEST_PRACTICE_CONTENT_DIR,
34+
bestPracticeDirName,
35+
'content'
36+
);
37+
38+
// If best practice content already exists do not proceed as it would override the files
39+
if (fs.existsSync(bestPracticeContentDirPath)) {
40+
console.error(`Best Practice content already exists @ ${bestPracticeContentDirPath}`);
41+
process.exit(1);
42+
}
43+
44+
function prepareDirTree(control, dirTree) {
45+
// Directories are only created for groups
46+
if (control.typeID !== '__group__') {
47+
return;
48+
}
49+
50+
// e.g. 104-testing-your-apps:other-options
51+
const controlName = control?.properties?.controlName || '';
52+
53+
// No directory for a group without control name
54+
if (!controlName || controlName.startsWith('check:') || controlName.startsWith('ext_link:')) {
55+
return;
56+
}
57+
58+
// e.g. ['testing-your-apps', 'other-options']
59+
const dirParts = controlName.split(':');
60+
61+
// Nest the dir path in the dirTree
62+
let currDirTree = dirTree;
63+
dirParts.forEach((dirPart) => {
64+
currDirTree[dirPart] = currDirTree[dirPart] || {};
65+
currDirTree = currDirTree[dirPart];
66+
});
67+
68+
const childrenControls = control.children.controls.control;
69+
// No more children
70+
if (childrenControls.length) {
71+
childrenControls.forEach((childControl) => {
72+
prepareDirTree(childControl, dirTree);
73+
});
74+
}
75+
76+
return { dirTree };
77+
}
78+
79+
const bestPractice = require(path.join(__dirname, `../public/jsons/best-practices/${bestPracticeId}`));
80+
const controls = bestPractice.mockup.controls.control;
81+
82+
// Prepare the dir tree that we will be creating
83+
const dirTree = {};
84+
85+
controls.forEach((control) => {
86+
prepareDirTree(control, dirTree);
87+
});
88+
89+
/**
90+
* @param parentDir Parent directory in which directory is to be created
91+
* @param dirTree Nested dir tree to be created
92+
* @param filePaths The mapping from groupName to file path
93+
*/
94+
function createDirTree(parentDir, dirTree, filePaths = {}) {
95+
const childrenDirNames = Object.keys(dirTree);
96+
const hasChildren = childrenDirNames.length !== 0;
97+
98+
// @todo write test for this, yolo for now
99+
const groupName = parentDir
100+
.replace(bestPracticeContentDirPath, '') // Remove base dir path
101+
.replace(/(^\/)|(\/$)/g, '') // Remove trailing slashes
102+
.replaceAll('/', ':') // Replace slashes with `:`
103+
.replace(/:\d+-/, ':');
104+
105+
const humanizedGroupName = groupName
106+
.split(':')
107+
.pop()
108+
?.replaceAll('-', ' ')
109+
.replace(/^\w/, ($0) => $0.toUpperCase());
110+
111+
// If no children, create a file for this under the parent directory
112+
if (!hasChildren) {
113+
let fileName = `${parentDir}.md`;
114+
fs.writeFileSync(fileName, `# ${humanizedGroupName}`);
115+
116+
filePaths[groupName || 'home'] = fileName.replace(CONTENT_DIR, '');
117+
return filePaths;
118+
}
119+
120+
// There *are* children, so create the parent as a directory
121+
// and create `index.md` as the content file for this
122+
fs.mkdirSync(parentDir);
123+
124+
let readmeFilePath = path.join(parentDir, 'index.md');
125+
fs.writeFileSync(readmeFilePath, `# ${humanizedGroupName}`);
126+
127+
filePaths[groupName || 'home'] = readmeFilePath.replace(CONTENT_DIR, '');
128+
129+
// For each of the directory names, create a
130+
// directory inside the given directory
131+
childrenDirNames.forEach((dirName) => {
132+
createDirTree(
133+
path.join(parentDir, dirName),
134+
dirTree[dirName],
135+
filePaths
136+
);
137+
});
138+
139+
return filePaths;
140+
}
141+
142+
// Create directories and get back the paths for created directories
143+
createDirTree(bestPracticeContentDirPath, dirTree);
144+
console.log('Created best practice content directory structure');

bin/readme.md

Lines changed: 28 additions & 0 deletions

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"upgrade": "ncu -u",
1515
"roadmap-links": "node bin/roadmap-links.cjs",
1616
"roadmap-content": "node bin/roadmap-content.cjs",
17+
"best-practice-content": "node bin/best-practice-content.cjs",
1718
"test:e2e": "playwright test"
1819
},
1920
"dependencies": {
@@ -24,7 +25,7 @@
2425
"node-html-parser": "^6.1.4",
2526
"npm-check-updates": "^16.6.2",
2627
"rehype-external-links": "^2.0.1",
27-
"roadmap-renderer": "^1.0.1",
28+
"roadmap-renderer": "^1.0.4",
2829
"tailwindcss": "^3.2.4"
2930
},
3031
"devDependencies": {

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
379 KB

public/jsons/best-practices/frontend-performance.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

public/jsons/checklists/frontend-performance.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

public/jsons/roadmaps/blockchain.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
58.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)