Skip to content

Commit 31a96cf

Browse files
committed
autopublish script wip
1 parent 2b1cc25 commit 31a96cf

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
build
33
out
4-
coverage
4+
coverage
5+
scripts/**.js

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
"build:cjs": "rollup -c",
2424
"check": "tsc",
2525
"run": "node ./build/@roguib/ng-vs-snippets.js",
26-
"test": "npm run build:cjs && jest ./tests/*"
26+
"test": "npm run build:cjs && jest ./tests/*",
27+
"auto-publish": "tsc scripts/auto-publish.ts && node scripts/auto-publish.js",
28+
"prettier": "prettier"
2729
},
2830
"dependencies": {
2931
"@types/jest": "^26.0.24",
32+
"prettier": "^2.3.2",
3033
"rollup": "^2.56.2",
3134
"rollup-plugin-add-shebang": "^0.3.1",
3235
"rollup-plugin-typescript2": "^0.30.0",

scripts/auto-publish.ts

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const fs = require("fs");
2+
const readline = require("readline");
3+
const rl = readline.createInterface({
4+
input: process.stdin,
5+
output: process.stdout,
6+
});
7+
const iterator = rl[Symbol.asyncIterator]();
8+
const prettier = require("prettier");
9+
10+
const autoPublish = async (): Promise<void> => {
11+
console.log(
12+
"Welcome to autopublish. This script walks you through the process of publishing the library to npm."
13+
);
14+
console.log("Choose which type of version you want to publish:");
15+
console.log("1- Major version (X.0.0) (M)");
16+
console.log("1- Minor version (0.X.0) (m)");
17+
console.log("1- Patch version (0.0.X) (p)");
18+
19+
let version: { value: string; done: boolean } = undefined;
20+
try {
21+
console.log("Choose the version (M/m/p)");
22+
version = await iterator.next();
23+
if (version.value != "M" && version.value != "m" && version.value != "p") {
24+
throw Error(
25+
"Published version should be Major(M) / Minor(m) / Patch(p). Aborting"
26+
);
27+
}
28+
} catch (error) {
29+
throw error;
30+
}
31+
32+
console.log("Updating package.json");
33+
let packageJson: any = undefined;
34+
try {
35+
packageJson = JSON.parse(
36+
fs.readFileSync("package.json", {
37+
encoding: "utf8",
38+
withFileTypes: true,
39+
})
40+
);
41+
} catch (error) {
42+
console.log(
43+
"An error has occured while trying to read package.json file. Aborting"
44+
);
45+
throw error;
46+
}
47+
48+
let vArr: Array<string> = packageJson.version.split("."),
49+
index = 0;
50+
switch (version.value) {
51+
case "M":
52+
index = 0;
53+
break;
54+
case "m":
55+
index = 1;
56+
break;
57+
default:
58+
index = 2;
59+
break;
60+
}
61+
vArr[index] = (+vArr[index] + 1).toString();
62+
packageJson.version = vArr.join(".");
63+
try {
64+
console.log("package.json updated. Running prettier formatter.");
65+
fs.writeFileSync(
66+
"package.json",
67+
prettier.format(JSON.stringify(packageJson), {
68+
semi: false,
69+
parser: "json",
70+
}),
71+
{
72+
encoding: "utf8",
73+
withFileTypes: true,
74+
}
75+
);
76+
} catch (error) {
77+
console.log(
78+
"An error has occured while trying to update package.json file. Aborting"
79+
);
80+
throw error;
81+
}
82+
// TODO
83+
console.log("Pushing the new version into GitHub");
84+
85+
// TODO
86+
console.log("Publishing the new version to npm");
87+
rl.close();
88+
return;
89+
};
90+
91+
autoPublish();

0 commit comments

Comments
 (0)