forked from martinkadlec0/Smart-RSS
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·228 lines (194 loc) · 5.98 KB
/
build.js
File metadata and controls
executable file
·228 lines (194 loc) · 5.98 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
#!/usr/bin/env node
const { join, dirname, relative } = require("path");
const {
readdirSync,
lstatSync,
copyFileSync,
mkdirSync,
existsSync,
readFileSync,
writeFileSync,
} = require("fs");
const { execSync } = require("child_process");
const semver = require("semver");
const AdmZip = require("adm-zip");
// Utility functions
const scan = (dir) => {
const filesList = [];
readdirSync(dir).forEach((file) => {
if (file[0] === ".") {
return;
}
const filePath = join(dir, file);
if (lstatSync(filePath).isDirectory()) {
filesList.push(...scan(filePath));
return;
}
filesList.push(filePath);
});
return filesList;
};
const ensureDirectoryExists = (dirPath) => {
if (!existsSync(dirPath)) {
mkdirSync(dirPath, { recursive: true });
}
};
// Main functions
const bumpVersion = (level = "patch") => {
console.log(`Bumping version (${level})...`);
const manifestPath = join(__dirname, "src/manifest.json");
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
manifest.version = semver.inc(manifest.version, level);
writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
console.log(`Version bumped to ${manifest.version}`);
return manifest.version;
};
const commit = (level = "patch") => {
console.log("Committing changes...");
try {
execSync("git add *", { stdio: "inherit" });
execSync(`git commit -m "auto version bump: ${level}"`, {
stdio: "inherit",
});
console.log("Changes committed");
return true;
} catch (error) {
console.error("Git commit failed:", error.message);
return false;
}
};
const copyFiles = () => {
console.log("Copying files from src to dist...");
const srcDir = join(__dirname, "src");
const distDir = join(__dirname, "dist");
// Ensure dist directory exists
ensureDirectoryExists(distDir);
// Get all files from src
const srcFiles = scan(srcDir);
// Copy each file to dist, preserving directory structure
srcFiles.forEach((srcFile) => {
const relativePath = relative(srcDir, srcFile);
const destFile = join(distDir, relativePath);
const destDir = dirname(destFile);
ensureDirectoryExists(destDir);
copyFileSync(srcFile, destFile);
});
console.log("Files copied");
};
const stripComments = () => {
console.log("Stripping comments from JS files...");
const root = join(__dirname, "dist");
const filesList = scan(root);
const multilineComment = /^[\t\s]*\/\*\*?[^!][\s\S]*?\*\/[\r\n]/gm;
const specialComments = /^[\t\s]*\/\*!\*?[^!][\s\S]*?\*\/[\r\n]/gm;
const singleLineComment = /^[\t\s]*(\/\/)[^\n\r]*[\n\r]/gm;
filesList.forEach((filePath) => {
if (!filePath.endsWith(".js")) {
return;
}
const contents = readFileSync(filePath, "utf8")
.replace(multilineComment, "")
.replace(singleLineComment, "")
.replace(specialComments, "");
writeFileSync(filePath, contents);
});
console.log("Comments stripped");
};
const zipPackage = () => {
console.log("Creating zip package...");
const root = join(__dirname, "dist");
const manifestPath = join(root, "manifest.json");
const filesList = scan(root);
const version = JSON.parse(readFileSync(manifestPath, "utf8")).version;
const zipFile = new AdmZip();
filesList.forEach((file) => {
// Get the directory relative to the root, or empty string if it's at the root
const relativePath =
dirname(file) === root ? "" : relative(root, dirname(file));
zipFile.addLocalFile(file, relativePath);
});
const zipPath = join(__dirname, "dist", `SmartRSS_v${version}.zip`);
zipFile.writeZip(zipPath);
console.log(`Zip package created: ${zipPath}`);
};
const watch = () => {
console.log("Watching for changes in src directory...");
const chokidar = require("chokidar");
chokidar
.watch(join(__dirname, "src"), {
ignored: /(^|[\/\\])\../,
persistent: true,
})
.on("change", (path) => {
console.log(`File ${path} has been changed`);
prepare();
});
console.log("Watching for changes. Press Ctrl+C to stop.");
};
// Combined tasks
const prepare = () => {
copyFiles();
stripComments();
};
const packageTask = () => {
prepare();
zipPackage();
};
const release = (level = "patch") => {
if (!["major", "minor", "patch"].includes(level)) {
console.error("Wrong update level, aborting");
return false;
}
bumpVersion(level);
commit(level);
copyFiles();
stripComments();
zipPackage();
};
// Command line interface
const printUsage = () => {
console.log(`
Usage: node build.js [command] [options]
Commands:
prepare Copy files from src to dist and strip comments
package Prepare and create zip package
release [level] Bump version, commit, prepare, and create zip package
level can be: patch, minor, major (default: patch)
watch Watch for changes in src directory
bump-version [level] Bump version number
level can be: patch, minor, major (default: patch)
Examples:
node build.js prepare
node build.js release minor
node build.js watch
`);
};
// Main
const args = process.argv.slice(2);
const command = args[0];
const option = args[1];
if (!command) {
printUsage();
process.exit(0);
}
switch (command) {
case "prepare":
prepare();
break;
case "package":
packageTask();
break;
case "release":
release(option || "patch");
break;
case "watch":
watch();
break;
case "bump-version":
bumpVersion(option || "patch");
break;
default:
console.error(`Unknown command: ${command}`);
printUsage();
process.exit(1);
}