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');
const pkg = await fs.readFile(`${root}/package.json`, 'utf8');
const manifestVersion = manifest.match(regexp);
const pkgVersion = pkg.match(regexp);
if (manifestVersion && pkgVersion) {
const result = compare(manifestVersion[1], pkgVersion[1]);
let match, version, file, str;
if (result === 0) { if (result === 0) {
return console.log(good, 'Manifest & package versions match'); return console.log(good, 'Manifest & package versions match');
} else if (result > 0) { } else if (result > 0) {
pkg.version = manifest.version; match = pkgVersion;
version = manifestVersion[1];
file = 'package.json'; file = 'package.json';
obj = pkg; str = pkg;
} else { } else {
manifest.version = pkg.version; match = manifestVersion;
version = pkgVersion[1];
file = 'manifest.json'; file = 'manifest.json';
obj = manifest; str = manifest;
} }
console.log(warn, `Updating ${file} to ${pkg.version}`); console.log(warn, `Updating ${file} to ${version}`);
return fs.writeFile(`${root}/${file}`, JSON.stringify(obj, null, ' ') + '\n'); str = str.slice(0, match.index + 1) + version + str.slice(match.index + match[1].length + 1);
return fs.writeFile(`${root}/${file}`, str);
}
throw Error(`Error reading ${manifestVersion ? '' : 'manifest.json'} ${pkgVersion ? '' : 'package.json'}`);
} }
updateVersions().catch(err => exit(err)); updateVersions().catch(err => exit(err));