-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepublish.js
38 lines (31 loc) · 1.15 KB
/
prepublish.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
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
/* eslint no-console: 0 */
'use strict';
const exec = require('child_process').exec;
const fs = require('fs');
const removeBom = (text) => {
return text.replace(/^\uFEFF/, '');
};
exec('git tag --contains HEAD', function (err, stdout, stderr) {
if (err) {
console.error(err);
return;
}
if (stdout) {
console.log(stdout);
}
if (stderr) {
console.log(stderr);
}
let matchResult = /^v(.+)$/gm.exec(stdout);
if (matchResult === null) {
console.log('PrepareToPublishTask: no tags found, skip package.json update.');
return;
}
const version = matchResult[1];
console.log(`PrepareToPublishTask: There is a tag that matches the version pattern. Updating package.json with version ${version}...`);
let packageJson = JSON.parse(removeBom(fs.readFileSync(`${__dirname}/package.json`, 'utf8')));
packageJson.version = version;
fs.writeFileSync(`${__dirname}/package.json`, JSON.stringify(packageJson, null, ' '), 'utf8');
console.log('Updated.');
});