Repository navigation
Expand file tree
/
Copy pathversion.cjs
More file actions
65 lines (53 loc) · 1.61 KB
/
Copy pathversion.cjs
File metadata and controls
65 lines (53 loc) · 1.61 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
const fs = require("fs");
const path = require("path");
const checkOnly = process.argv.includes("--check");
// Path to package.json
const packageJsonPath = path.resolve(__dirname, "../package.json");
// Define files to generate
const filesToGenerate = [
{
outputPath: path.resolve(
__dirname,
"../docs/layouts/shortcodes/version.html",
),
getContent: (version) => `<p>Version: ${version}</p>\n`,
},
{
outputPath: path.resolve(
__dirname,
"../docs/layouts/partials/hooks/head-end.html",
),
getContent: (version) =>
`<script src="https://cdn.jsdelivr.net/npm/@angular-wave/angular.ts@${version}/dist/angular-ts.umd.js"></script>\n`,
},
];
try {
// Read package.json once
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
const version = packageJson.version || "unknown";
// Generate all files
let stale = false;
filesToGenerate.forEach(({ outputPath, getContent }) => {
const content = getContent(version);
if (checkOnly) {
const current = fs.existsSync(outputPath)
? fs.readFileSync(outputPath, "utf-8")
: undefined;
if (current !== content) {
stale = true;
console.error(`Stale generated version file: ${outputPath}`);
}
return;
}
// Ensure the directory exists
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, content, "utf-8");
console.log(`Generated: ${outputPath}`);
});
if (stale) {
process.exit(1);
}
} catch (error) {
console.error("Error generating version files:", error);
process.exit(1);
}