From ea4b2ef8fbc49413666af9a31003df37a049c8ae Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 15 Feb 2021 13:56:00 -0500 Subject: [PATCH] release: Factor out JSON read/write for later reuse --- src/bin/release.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/bin/release.js b/src/bin/release.js index aa3f5f1c2..144a078cd 100644 --- a/src/bin/release.js +++ b/src/bin/release.js @@ -25,10 +25,12 @@ if (!release) { throw new Error('No release type included'); } +const readJson = (filename) => JSON.parse(fs.readFileSync(filename, {encoding: 'utf8', flag: 'r'})); +const writeJson = (filename, obj) => fs.writeFileSync(filename, JSON.stringify(obj, null, 2)); + const changelog = fs.readFileSync('CHANGELOG.md', {encoding: 'utf8', flag: 'r'}); -let packageJson = fs.readFileSync('./src/package.json', {encoding: 'utf8', flag: 'r'}); -packageJson = JSON.parse(packageJson); -const currentVersion = packageJson.version; +const pkg = readJson('./src/package.json'); +const currentVersion = pkg.version; const newVersion = semver.inc(currentVersion, release); if (!newVersion) { @@ -44,9 +46,9 @@ if (!changelogIncludesVersion) { console.log('Okay looks good, lets create the package.json and package-lock.json'); -packageJson.version = newVersion; +pkg.version = newVersion; -fs.writeFileSync('src/package.json', JSON.stringify(packageJson, null, 2)); +writeJson('./src/package.json', pkg); // run npm version `release` where release is patch, minor or major childProcess.execSync('npm install --package-lock-only', {cwd: 'src/'});