Update versions now uses string replacement

This commit is contained in:
Rob Garrison 2018-07-21 12:16:58 -05:00
parent 07ee596b2f
commit e5fa1bee38

View File

@ -5,9 +5,6 @@ const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const root = path.join(__dirname, '..'); const root = path.join(__dirname, '..');
const manifest = require(`${root}/manifest.json`);
const pkg = require(`${root}/package.json`);
const good = '\x1b[32m%s\x1b[0m'; const good = '\x1b[32m%s\x1b[0m';
const warn = '\x1b[36m%s\x1b[0m'; const warn = '\x1b[36m%s\x1b[0m';
@ -42,21 +39,32 @@ function compare(v1, v2) {
} }
async function updateVersions() { async function updateVersions() {
const result = compare(manifest.version, pkg.version); const regexp = /"([v\d.]+)"/;
let file, obj; const manifest = await fs.readFile(`${root}/manifest.json`, 'utf8');
if (result === 0) { const pkg = await fs.readFile(`${root}/package.json`, 'utf8');
return console.log(good, 'Manifest & package versions match'); const manifestVersion = manifest.match(regexp);
} else if (result > 0) { const pkgVersion = pkg.match(regexp);
pkg.version = manifest.version; if (manifestVersion && pkgVersion) {
file = 'package.json'; const result = compare(manifestVersion[1], pkgVersion[1]);
obj = pkg; let match, version, file, str;
} else { if (result === 0) {
manifest.version = pkg.version; return console.log(good, 'Manifest & package versions match');
file = 'manifest.json'; } else if (result > 0) {
obj = manifest; match = pkgVersion;
version = manifestVersion[1];
file = 'package.json';
str = pkg;
} else {
match = manifestVersion;
version = pkgVersion[1];
file = 'manifest.json';
str = manifest;
}
console.log(warn, `Updating ${file} to ${version}`);
str = str.slice(0, match.index + 1) + version + str.slice(match.index + match[1].length + 1);
return fs.writeFile(`${root}/${file}`, str);
} }
console.log(warn, `Updating ${file} to ${pkg.version}`); throw Error(`Error reading ${manifestVersion ? '' : 'manifest.json'} ${pkgVersion ? '' : 'package.json'}`);
return fs.writeFile(`${root}/${file}`, JSON.stringify(obj, null, ' ') + '\n');
} }
updateVersions().catch(err => exit(err)); updateVersions().catch(err => exit(err));