-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpub.js
59 lines (48 loc) · 1.79 KB
/
pub.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
59
import { readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
import { inc } from "semver";
import { execSync } from "child_process";
// Define the default version increment type
let releaseType = "patch";
// Process command line arguments
const args = process.argv.slice(2);
if (args.includes("-m")) {
releaseType = "minor";
} else if (args.includes("-M")) {
releaseType = "major";
}
try {
// Get package.json content
const packageJsonPath = resolve("./package.json");
const packageJsonData = readFileSync(packageJsonPath, "utf8");
// Parse and increment the version
const packageJson = JSON.parse(packageJsonData);
const oldVersion = packageJson.version;
const newVersion = inc(oldVersion, releaseType);
if (!newVersion) {
console.error("Error incrementing version number.");
process.exit(1);
}
// Replace the old version with the new version
packageJson.version = newVersion;
// Write the updated JSON back to package.json
const updatedPackageJsonData = JSON.stringify(packageJson, null, 2);
writeFileSync(packageJsonPath, updatedPackageJsonData);
// Log the version change
console.log(`Version bumped from ${oldVersion} to ${newVersion}`);
// Build the package
execSync("npm run build");
// Publish to npm
// Make sure you're already logged in to npm (npm login)
const tag =
args.includes("-p") || args.includes("--prod") ? `` : `--tag alpha`;
execSync(`npm publish ${tag}`, { stdio: "inherit" });
console.log("Successfully published the package");
// Commit the version change
execSync(`cd .. && git add .`);
execSync(`cd .. && git commit -m "Publishing mufasa ${newVersion}"`);
execSync(`cd .. && git push`);
} catch (err) {
console.error("Error reading, updating, or publishing the package.", err);
process.exit(1);
}