Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (116 loc) · 3.53 KB
/
Copy pathindex.js
File metadata and controls
137 lines (116 loc) · 3.53 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
#!/usr/bin/env node
import path from "path";
import archiver from "archiver";
import { program } from "commander";
import fs from "fs-extra";
import ignore from "ignore";
import yaml from "js-yaml";
import picomatch from "picomatch";
const packageJson = JSON.parse(fs.readFileSync(new URL("./package.json", import.meta.url), "utf8"));
const ignorePatterns = [
"dist/**",
"node_modules/**",
"**/node_modules/**",
"**/.git",
"**/.git/**",
".github/**",
".idea/**",
".DS_Store",
"**/.DS_Store",
];
const essentialPaths = [
"templates/**",
"ui-plugin/dist/**",
"README.md",
"LICENSE",
"i18n/**",
"*.yaml",
"*.yml",
"screenshot.png",
"screenshot.jpeg",
"screenshot.jpg",
"screenshot.webp",
];
program
.version(packageJson.version)
.description("A CLI tool for packaging Halo theme template files");
program.option(
"-a, --all",
"Package all files using .gitignore, always including essential paths",
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) {
const isEssential = picomatch(essentialPaths, { dot: true });
const gitignore = ignore();
const gitignorePath = path.join(process.cwd(), ".gitignore");
if (await fs.pathExists(gitignorePath)) {
gitignore.add(await fs.readFile(gitignorePath, "utf8"));
}
archive.directory(process.cwd(), false, (entry) => {
const name = entry.name.split(path.sep).join("/");
// Never include the archive being written in itself.
if (path.resolve(process.cwd(), entry.name) === zipFilePath) {
return false;
}
if (name.split("/").includes(".git")) {
return false;
}
if (isEssential(name)) {
return entry;
}
const ignorePath = entry.stats.isDirectory() ? `${name}/` : name;
return gitignore.ignores(ignorePath) ? false : entry;
});
} 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();