This repository was archived by the owner on May 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
165 lines (150 loc) · 4.98 KB
/
app.js
File metadata and controls
165 lines (150 loc) · 4.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
const fs = require("fs");
const prependFile = require("prepend-file");
const npmRun = require("npm-run");
const git = require("simple-git/promise");
const exec = require("child_process");
const prompts = require("prompts");
let bundlePath = "app/assets/javascripts/easy_vue/bundle.js";
const versionPath = "lib/easy_vue/version.rb";
const bundleTextToAppend = "/*! no_asset_compression */\n";
const getCurrentVersion = () => {
const isVersion = fs.existsSync(versionPath);
if (!isVersion) return null;
const version = fs.readFileSync(versionPath, "utf8");
const versionParsedArr = version.match(/(\d+).(\d+).(\d+)/);
const [versionString, major, minor, patch] = versionParsedArr;
return {
versionString,
major,
minor,
patch
};
};
const resetAndCommit = async ({ value: confirmed, aborted }) => {
if (!confirmed || aborted) {
console.log("\n You have to push and commit all data first");
process.exit(0);
}
git().reset("hard");
};
const getIncreasedVersion = (computedVersions, versionType) => {
const attrName = `increased${versionType
.charAt(0)
.toUpperCase()}${versionType.slice(1)}`;
return computedVersions[attrName];
};
const getBranchChoices = async (options, versionType) => {
const { currentBranch, computedVersions } = options;
const choices = [
{ title: `${currentBranch}`, value: `${currentBranch}` },
{ title: `Own name`, value: "own" }
];
if (versionType) {
const increasedVersion = getIncreasedVersion(computedVersions, versionType);
choices.unshift({
title: `release_${increasedVersion}`,
value: `release_${increasedVersion}`
});
}
return choices;
};
const processResponses = async (answers, computedVersions) => {
const { bundlePath, commitMessage, branchName, versionType } = answers;
const { current: currentBranch } = await git().branch();
// if users branch !== current branch, checkout to new specified branch
if (branchName !== currentBranch) {
await git().checkoutLocalBranch(branchName);
}
// Run npm build
console.log("Started npm run build, please wait...");
npmRun.execSync("npm run build");
console.log("Successfully built bundle.js");
const bundle = fs.existsSync(bundlePath);
if (!bundle) {
console.log("Bundle not found!!");
process.exit(0);
}
// append text to bundle.js
prependFile.sync(bundlePath, bundleTextToAppend);
// modify version.rb if version change is chosen
if (versionType) {
const versionFile = fs.readFileSync(versionPath, "utf8");
const versionRegxp = /(\d+).(\d+).(\d+)/;
const increasedVersion = getIncreasedVersion(computedVersions, versionType);
const newVersionFile = versionFile.replace(versionRegxp, increasedVersion);
fs.writeFileSync(versionPath, newVersionFile, "utf8");
console.log("Successfully changed version in version.rb");
}
// add all changes to git
await git().add("*");
await git().commit(commitMessage);
console.log("Committed with message: ", commitMessage);
await git().push("origin", branchName);
console.log("Pushed to branch: ", branchName);
console.log("Finished!");
};
const computeVersions = currVersion => {
const { major, minor, patch } = currVersion;
const increasedMajor = `${+major + 1}.${minor}.${patch}`;
const increasedMinor = `${major}.${+minor + 1}.${patch}`;
const increasedPatch = `${major}.${minor}.${+patch + 1}`;
return {
increasedMajor,
increasedMinor,
increasedPatch
};
};
const init = async () => {
const currVersion = getCurrentVersion();
const { current: currentBranch } = await git().branch();
const computedVersions = computeVersions(currVersion);
const { increasedMajor, increasedMinor, increasedPatch } = computedVersions;
const answers = await prompts([
{
type: "confirm",
name: "pushedAndCommited",
message: "Have you commited and pushed all files?",
initial: true,
onState: resetAndCommit
},
{
type: "select",
name: "versionType",
message: `Pick a version you want to increase. Current version is: ${currVersion.versionString}`,
choices: [
{ title: `Patch (version will be: ${increasedPatch})`, value: "patch" },
{ title: `Minor (version will be: ${increasedMinor})`, value: "minor" },
{ title: `Major (version will be: ${increasedMajor})`, value: "major" },
{ title: `None of them`, value: null }
]
},
{
type: "select",
name: "branchName",
message: `Select branch name`,
choices: getBranchChoices.bind(null, {
currentBranch,
computedVersions
})
},
{
type: prev => (prev === "own" ? "text" : false),
name: "branchName",
message: `Type branch name`
},
{
type: "text",
name: "commitMessage",
message: `Commit message`,
initial: prev => `New ${prev}`
},
{
type: "text",
name: "bundlePath",
message: `Bundle path`,
initial: `${bundlePath}`
}
]);
await processResponses(answers, computedVersions);
};
init();