-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (108 loc) · 2.8 KB
/
index.js
File metadata and controls
117 lines (108 loc) · 2.8 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
#!/usr/bin/env node
/**
* nurjs
* Helps to create and version releases of NPM packages
*
* @author nurjs <https://github.com/nurjs>
*/
const init = require('./utils/init');
const cli = require('./utils/cli');
const log = require('./utils/log');
const path = require('path');
const fs = require('fs');
const { exec } = require('child_process');
const directoryPath = path.join(__dirname);
const input = cli.input;
const flags = cli.flags;
const { clear, debug } = flags;
(async () => {
init({ clear });
input.includes(`help`) && cli.showHelp(0);
debug && log(flags);
if (flags.minor) {
console.log('Updating minor version');
var versionUpdate = [0, 1, 0];
updatePackage(versionUpdate);
}
if (flags.major) {
console.log('Updating major version');
var versionUpdate = [1, 0, 0];
updatePackage(versionUpdate);
}
if (flags.patch) {
console.log('Updating patch version');
var versionUpdate = [0, 0, 1];
updatePackage(versionUpdate);
}
})();
function handleVersions(version, versionUpdate) {
var major = parseInt(versionUpdate[0]),
minor = parseInt(versionUpdate[1]),
patch = parseInt(versionUpdate[2]);
if (major != 0 || minor != 0) {
version[2] = '0';
}
if (major != 0) {
version[1] = '0';
version[0] = (parseInt(version[0]) + major).toString();
}
if (minor != 0) {
version[1] = (parseInt(version[1]) + minor).toString();
}
if (patch != 0) {
version[2] = (parseInt(version[2]) + patch).toString();
}
return version.join('.');
}
function updatePackage(versionUpdate) {
fs.readdir(directoryPath, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(async function (file) {
if (file === 'package.json') {
var content = fs.readFileSync(file, 'utf8');
var content = JSON.parse(content);
var oldVersion = content.version.split('.');
var newVersion = await handleVersions(
oldVersion,
versionUpdate
);
console.log(
`updating from version ${content.version} to ${newVersion}`
);
content.version = newVersion;
fs.writeFileSync(
file,
JSON.stringify(content, null, 4),
'utf8'
);
exec('npm install --save');
await publishToGit(newVersion);
}
});
});
}
async function publishToGit(version) {
if (flags.git) {
console.log('Publishing to git');
await sleep(2000);
await exec('git add .');
await sleep(1000);
await exec(`git commit -m "Release ${version}"`);
await sleep(1000);
await exec(`git push -u origin ${flags.branch}`);
console.log(`successfully pushed code, waiting for tag`);
await sleep(1000);
await exec(`git tag -a ${version} -m "Release ${version}"`);
sleep(2000);
await exec(`git push --tags`);
sleep(5000);
await exec(`git push --tags`);
}
}
function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}