-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (85 loc) · 2.6 KB
/
index.js
File metadata and controls
105 lines (85 loc) · 2.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
#!/usr/bin/env node
import path from "path";
import archiver from "archiver";
import { program } from "commander";
import fs from "fs-extra";
import yaml from "js-yaml";
const packageJson = JSON.parse(fs.readFileSync(new URL("./package.json", import.meta.url), "utf8"));
const ignorePatterns = [
"dist/**",
"node_modules/**",
"**/node_modules/**",
".git/**",
".github/**",
".idea/**",
".DS_Store",
"**/.DS_Store",
];
const essentialPaths = ["templates/**", "README.md", "LICENSE", "i18n/**", "*.yaml", "*.yml"];
program
.version(packageJson.version)
.description("A CLI tool for packaging Halo theme template files");
program.option(
"-a, --all",
"Package all files, excluding some unnecessary directories and files",
false,
);
program.parse(process.argv);
const options = program.opts();
async function main() {
try {
const themeYamlPath = path.join(process.cwd(), "theme.yaml");
if (!(await fs.pathExists(themeYamlPath))) {
console.error("Error: theme.yaml file not found in the current directory");
process.exit(1);
}
const themeYamlContent = await fs.readFile(themeYamlPath, "utf8");
const themeConfig = yaml.load(themeYamlContent);
if (
!themeConfig?.metadata ||
!themeConfig.metadata.name ||
!themeConfig.spec ||
!themeConfig.spec.version
) {
console.error("Error: theme.yaml file format is incorrect, missing required fields");
process.exit(1);
}
const distDir = path.join(process.cwd(), "dist");
await fs.ensureDir(distDir);
const zipFileName = `${themeConfig.metadata.name}-${themeConfig.spec.version}.zip`;
const zipFilePath = path.join(distDir, zipFileName);
const output = fs.createWriteStream(zipFilePath);
const archive = archiver("zip", {
zlib: { level: 9 },
});
output.on("close", function () {
console.log(`✅ Packaged successfully: ${zipFilePath}`);
console.log(`Theme version: ${themeConfig.spec.version}`);
console.log(`File size: ${(archive.pointer() / 1024 / 1024).toFixed(2)} MB`);
});
archive.on("error", function (err) {
throw err;
});
archive.pipe(output);
if (options.all) {
archive.glob("**/*", {
cwd: process.cwd(),
ignore: ignorePatterns,
dot: true,
});
} else {
essentialPaths.forEach((pattern) => {
archive.glob(pattern, {
cwd: process.cwd(),
ignore: ignorePatterns,
dot: true,
});
});
}
await archive.finalize();
} catch (error) {
console.error("Error:", error.message);
process.exit(1);
}
}
main();