This repository was archived by the owner on Apr 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
441 lines (354 loc) · 11.6 KB
/
Copy pathindex.js
File metadata and controls
441 lines (354 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
const simpleGit = require("simple-git");
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const repoUrl = process.argv[2];
const EXCLUDE_FILES = ['bun.lockb', '*.scale']
if (!repoUrl) {
console.error(
"Please provide a GitHub repository URL as a command-line argument."
);
process.exit(1);
}
const repoName = repoUrl
.split("/")
.pop()
.replace(/\.git$/, "");
const repoPath = path.join(__dirname, repoName);
const sourcePath = path.join(repoPath, "source");
// Sanitize the source path
const sanitizedSourcePath = sourcePath.toString().replace(/ /g, "\\ ");
// Clone the repository
console.log(`Cloning repository: ${repoUrl}`);
simpleGit().clone(repoUrl, sourcePath, (err, _) => {
if (err) {
console.error(`Error cloning repository: ${err}`);
process.exit();
}
// Switch branch to gitorial
console.log("Switching to gitorial branch...");
execSync(`git -C ${sanitizedSourcePath} checkout gitorial`);
// Get the list of commits
console.log("Fetching commits...");
const commitHashes = execSync(
`git -C ${sanitizedSourcePath} log --format=%H::%s`,
{
encoding: "utf-8",
}
)
.trim()
.split("\n");
let stepCounter = 0;
let templateFound = false;
let solutionFound = false;
let templateFiles = [];
let solutionFiles = [];
let sourceFiles = [];
let sectionFiles = [];
let stepNames = [];
// Create a folder for each commit
// Reverse to make the oldest commit first
commitHashes.reverse().forEach((commitInfo, index) => {
const [commitHash, commitMessage] = commitInfo.split("::");
const isReadme = commitMessage.toLowerCase().startsWith("readme: ");
const isTemplate = commitMessage.toLowerCase().startsWith("template: ");
const isSolution = commitMessage.toLowerCase().startsWith("solution: ");
const isSection = commitMessage.toLowerCase().startsWith("section: ");
const isAction = commitMessage.toLowerCase().startsWith("action: ");
const isStartingTemplate = commitMessage.toLowerCase().startsWith("starting-template");
let stepFolder = path.join(repoPath, stepCounter.toString());
if (!fs.existsSync(stepFolder)) {
fs.mkdirSync(stepFolder);
}
let sectionFolder = path.join(stepFolder, "section");
let sourceFolder = path.join(stepFolder, "source");
let templateFolder = path.join(stepFolder, "template");
let solutionFolder = path.join(stepFolder, "solution");
// Default assumption is output is not a template or solution
let outputFolder = sourceFolder;
// We skip the starting template commit since it is only used for starting the project.
if (isStartingTemplate) {
return;
}
if (isSection) {
outputFolder = sectionFolder;
}
if (isTemplate) {
// Check there isn't a template already in queue
if (templateFound) {
console.error("A second template was found before a solution.");
process.exit(1);
}
templateFound = true;
// make step folder
outputFolder = templateFolder;
}
if (isSolution) {
// Check that there is a template in queue
if (!templateFound) {
console.error("No template was found for this solution.");
process.exit(1);
}
// Check that a solution is not already found.
if (solutionFound) {
console.error("A second solution was found before a template.");
process.exit(1);
}
solutionFound = true;
outputFolder = solutionFolder;
}
fs.mkdirSync(outputFolder);
// Checkout the commit
console.log(`Checking out commit: ${commitHash}`);
execSync(`git -C ${sanitizedSourcePath} checkout ${commitHash}`);
// Sanitize outputFolder
const sanitizedOutputFolder = outputFolder.toString().replace(/ /g, "\\ ");
// Copy the contents to the commit folder
execSync(
`rsync -a ${EXCLUDE_FILES.map((file) => `--exclude='${file}'`).join(
" "
)} ${sanitizedSourcePath}/ ${sanitizedOutputFolder}`
);
console.log(`Contents of commit ${index} copied to ${sanitizedOutputFolder}`);
let diffOutput = "";
let diffRaw = "";
let previousCommit = "HEAD~1";
// This is the commit hash for an empty git project.
let emptyTree = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
if (index == 0) {
previousCommit = emptyTree;
}
// Get the list of modified or created files in the commit
diffOutput = execSync(
`git -C ${sanitizedSourcePath} diff --name-status ${previousCommit} HEAD`,
{ encoding: "utf-8" }
)
.trim()
.split("\n");
diffRaw = execSync(
`git -C ${sanitizedSourcePath} diff ${previousCommit} HEAD ':(exclude)README.md'`,
{ encoding: "utf-8" }
);
// Create a raw output
let diff_name = "changes.diff";
if (isSolution) {
diff_name = "solution.diff";
} else if (isTemplate) {
diff_name = "template.diff";
}
const diffFilePath = path.join(outputFolder, diff_name);
fs.writeFileSync(diffFilePath, diffRaw);
// Create a JSON file in the commit folder
const jsonFilePath = path.join(outputFolder, "commit_info.json");
const commitInfoObject = {
commitHash,
commitMessage,
files: diffOutput.map((line) => {
const [status, file] = line.split("\t");
return { status, file };
}),
};
if (isTemplate) {
templateFiles = commitInfoObject.files;
} else if (isSolution) {
solutionFiles = commitInfoObject.files;
} else if (isSection) {
sectionFiles = commitInfoObject.files;
} else {
sourceFiles = commitInfoObject.files;
}
fs.writeFileSync(jsonFilePath, JSON.stringify(commitInfoObject, null, 2));
// Reset sanity check and increment step
// Handle when both template and solution is found,
// or when there is a step that is neither a template or solution
if (
(templateFound && solutionFound) ||
(!templateFound && !solutionFound)
) {
if (isReadme) {
markdownContent = sectionMarkdown;
} else if (isSection) {
markdownContent = sectionMarkdown;
stepNames.push({
name: getStepName(sectionFolder),
is_section: true,
});
} else if (templateFound) {
markdownContent = templateMarkdown;
let templateFileText = generateFileMarkdown("template", templateFiles);
let solutionFileText = generateFileMarkdown("solution", solutionFiles);
markdownContent = markdownContent.replace(
"<!-- insert_template_files -->",
templateFileText
);
markdownContent = markdownContent.replace(
"<!-- insert_solution_files -->",
solutionFileText
);
let diffText = generateDiffMarkdown("template");
markdownContent = markdownContent.replace(
"<!-- insert_diff_files -->",
diffText
);
stepNames.push({
name: getStepName(templateFolder),
is_section: false,
});
} else {
markdownContent = sourceMarkdown;
let sourceFileText = generateFileMarkdown("source", sourceFiles);
markdownContent = markdownContent.replace(
"<!-- insert_source_files -->",
sourceFileText
);
let diffText = generateDiffMarkdown("source");
markdownContent = markdownContent.replace(
"<!-- insert_diff_files -->",
diffText
);
stepNames.push({
name: getStepName(sourceFolder),
is_section: false,
});
}
// Create a Markdown file in the commit folder
const markdownFilePath = path.join(stepFolder, "README.md");
fs.writeFileSync(markdownFilePath, markdownContent);
stepCounter += 1;
templateFound = false;
solutionFound = false;
}
});
generateSidebar(stepNames);
// Clean up source folder
fs.rmSync(sourcePath, { recursive: true, force: true });
console.log("Process completed.");
});
// Generate the markdown text for files.
function generateFileMarkdown(type, files) {
// type is expected to be one of "source", "solution", or "template"
if (type != "solution" && type != "source" && type != "template") {
process.exit(1);
}
let output = "";
for (file of files) {
if (!file.file) {
continue;
}
// Check if file matches any exclusion pattern
if (EXCLUDE_FILES.some(pattern => {
const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
return regex.test(file.file);
})) {
continue;
}
let filepath = `./${type}/${file.file}`;
let filename = path.parse(filepath).base;
// Skip README
if (filename == "README.md") {
continue;
}
// Skip hidden files
if (filename.startsWith(".")) {
continue;
}
// Skip Cargo.lock
if (filename == "Cargo.lock") {
continue;
}
let classStyle = `file-${type}`;
if (file.status == "M") {
classStyle += " file-modified";
} else if (file.status == "A") {
classStyle += " file-added";
} else if (file.status == "D") {
classStyle += " file-deleted";
}
let codeStyle = "text";
let extname = path.extname(filepath);
if (extname == ".rs") {
codeStyle = "rust";
} else if (extname == ".toml") {
codeStyle = "toml";
}
output += `#### **<span class="${classStyle}">${file.file}</span>**\n\n`;
output += `[${filepath}](${filepath} ':include :type=code ${codeStyle}')\n\n`;
}
if (output == "") {
output = "No files edited in this step.";
}
return output;
}
function generateDiffMarkdown(type) {
let output = "";
if (type == "template" || type == "solution") {
let filepath = `./template/template.diff`;
output += `#### **template.diff**\n\n`;
output += `[${filepath}](${filepath} ':include :type=code diff')\n\n`;
filepath = `./solution/solution.diff`;
output += `#### **solution.diff**\n\n`;
output += `[${filepath}](${filepath} ':include :type=code diff')\n\n`;
} else {
let filepath = `./${type}/changes.diff`;
output += `#### **changes.diff**\n\n`;
output += `[${filepath}](${filepath} ':include :type=code diff')\n\n`;
}
return output;
}
let templateMarkdown = `
[filename](./template/README.md ':include')
<!-- slide:break -->
<!-- tabs:start -->
#### **template**
<!-- tabs:start -->
<!-- insert_template_files -->
<!-- tabs:end -->
#### **solution**
<!-- tabs:start -->
<!-- insert_solution_files -->
<!-- tabs:end -->
#### **diff**
<!-- tabs:start -->
<!-- insert_diff_files -->
<!-- tabs:end -->
<!-- tabs:end -->
`;
let sourceMarkdown = `
[filename](./source/README.md ':include')
<!-- slide:break -->
<!-- tabs:start -->
#### **source**
<!-- tabs:start -->
<!-- insert_source_files -->
<!-- tabs:end -->
#### **diff**
<!-- tabs:start -->
<!-- insert_diff_files -->
<!-- tabs:end -->
<!-- tabs:end -->
`;
let sectionMarkdown = `
[filename](./section/README.md ':include')
`;
function getStepName(folder) {
const filePath = path.join(folder, "README.md");
const markdownContent = fs.readFileSync(filePath, "utf8");
const titleMatch = markdownContent.match(/^#\s+(.*)/m);
if (titleMatch) {
return titleMatch[1];
} else {
console.error(`Error getting markdown title.`);
process.exit(1);
}
}
function generateSidebar(steps) {
const sidebarFilePath = path.join(repoPath, "_sidebar.md");
let output = "- [Home](/)\n\n---\n\n";
steps.forEach(({ name, is_section }, index) => {
if (!is_section) {
output += ` `;
}
output += `- [${index}. ${name}](${repoName}/${index}/README.md)\n`;
});
fs.writeFileSync(sidebarFilePath, output);
}