-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·164 lines (138 loc) · 4.4 KB
/
index.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
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
#!/usr/bin/env node
var path = require('path');
const fs = require('fs');
const date = require('date-and-time');
var commandExists = require('command-exists');
const chalk = require('chalk');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const argv = yargs(hideBin(process.argv))
.version(false)
.option('v', {
alias: 'version',
type: 'string',
demandOption: true,
description: 'Version of the module. [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease]'
})
.option('incCommit', {
alias: 'c',
type: 'boolean',
default: false,
description: 'Make the commit.'
})
.help()
.argv;
const semver = require('semver');
const { exec } = require("child_process");
// Create new text.
let package_path = process.cwd() + "/package.json";
var pjson = require(package_path);
const project = pjson.name;
const now = new Date();
let current_version = pjson.version;
let new_version = "";
switch (argv['v']) {
case 'major':
case 'minor':
case 'patch':
case 'premajor':
case 'preminor':
case 'prepatch':
case 'prerelease':
new_version = semver.inc(current_version, argv['v']);
break;
default:
new_version = argv['v'];
}
if (!semver.valid(new_version)) {
console.error(chalk.red("Version is not semantically correct: " + new_version));
process.exit(1);
}
console.log(chalk.yellow("Setting new Version: " + new_version));
let bldMsg = "# Information added by packaging script on " + date.format(now, 'YYYY-MM-DD') + "\r\n";
bldMsg += "version: " + new_version + "\r\n";
bldMsg += "project: " + project + "\r\n";
bldMsg += "datestamp: " + Date.now();
let infoFiles = [];
fromDir('./', /\.info.yml$/, function(filename) {
infoFiles.push(filename);
});
let writes = [];
infoFiles.forEach((filename) => {
let p = fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
const index = data.indexOf("# Information added by packaging script on ");
if (index == -1) {
data += "\r\n\r\n" + bldMsg;
}
else {
data = data.substring(0, index) + bldMsg;
}
console.log(chalk.yellow("Updating " + filename + " ..."));
fs.writeFile(filename, data, { flag: 'w+' }, err => {
if (err) console.log(err);
});
});
writes.push(p);
});
Promise.all(writes).then(() => {
console.log(chalk.yellow("Updating package.json ..."));
exec("npm version --allow-same-version --no-commit-hooks --no-git-tag-version " + new_version, (error, stdout, stderr) => {
if (error) {
console.log(chalk.red("Error updating package.json"));
console.log(`\r\nerror:\r\n${error.message}`);
return;
}
if (stderr) {
console.log(chalk.red("StError updating package.json"));
console.log(`\r\nstderr:\r\n${stderr}`);
return;
}
console.log(`\r\nstdout: \r\n${stdout}`);
console.log(chalk.blue("Finished updating package.json"));
if (argv['c']) {
console.log(chalk.yellow("Attempting to commit changes..."));
commandExists('git', function (err, commandExists) {
if (commandExists) {
// git tag new_version
exec("git add . && git commit -m \"Changing version to " + new_version + "\" && git tag " + new_version, (error, stdout, stderr) => {
if (error) {
console.log(`\r\nerror:\r\n${error.message}`);
return;
}
if (stderr) {
console.log(`\r\nstderr:\r\n${stderr}`);
return;
}
console.log(`\r\nstdout:\r\n${stdout}`);
console.log(chalk.blue("Finished commiting changes"));
console.log(chalk.green("\r\nSUCCESS, enjoy your new version!"));
});
}
if (err) {
console.error(chalk.red(err));
}
});
}
else {
console.log(chalk.green("\r\nSUCCESS, enjoy your new version!"));
}
});
});
function fromDir(startPath, filter, callback) {
if (!fs.existsSync(startPath)) {
console.log("no dir ", startPath);
return;
}
var files = fs.readdirSync(startPath);
for (var i = 0; i < files.length; i++) {
var filename = path.join(startPath, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
fromDir(filename, filter, callback); //recurse
} else if (filter.test(filename)) callback(filename);
}
}