-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-manifest.js
58 lines (49 loc) · 1.6 KB
/
generate-manifest.js
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
const fs = require("fs");
const path = require("path");
const FILE_EXTENSION = ".kcl";
const MANIFEST_FILE = "manifest.json";
const COMMENT_PREFIX = "//";
// Function to read and parse .kcl files
const getKclMetadata = (filePath) => {
const content = fs.readFileSync(filePath, "utf-8");
const lines = content.split("\n");
if (lines.length < 2) {
return null;
}
const title = lines[0].replace(COMMENT_PREFIX, "").trim();
const description = lines[1].replace(COMMENT_PREFIX, "").trim();
return {
file: path.basename(filePath),
title,
description,
};
};
// Function to scan the directory and generate the manifest.json
const generateManifest = (dir) => {
const projectDirectories = fs.readdirSync(dir);
const manifest = [];
projectDirectories.forEach((file) => {
const filePath = path.join(dir, file);
const stattedDir = fs.statSync(filePath);
if (stattedDir.isDirectory()) {
const files = fs
.readdirSync(filePath)
.filter((f) => f.endsWith(FILE_EXTENSION));
if (files.length === 0) {
return;
}
const metadata = getKclMetadata(path.join(filePath, files[0]));
if (metadata) {
manifest.push(metadata);
}
}
});
// Write the manifest.json
const outputPath = path.join(dir, MANIFEST_FILE);
fs.writeFileSync(outputPath, JSON.stringify(manifest, null, 2));
console.log(`Manifest of ${manifest.length} items written to ${outputPath}`);
};
// Run the script
console.log(`Generating ${MANIFEST_FILE}...`);
const projectDir = path.resolve(__dirname); // Set project root directory
generateManifest(projectDir);