11cb0ef41Sopenharmony_ci// called with all the options already set to their defaults 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst retrieveTag = require('./retrieve-tag.js') 41cb0ef41Sopenharmony_ciconst semver = require('semver') 51cb0ef41Sopenharmony_ciconst enforceClean = require('./enforce-clean.js') 61cb0ef41Sopenharmony_ciconst writeJson = require('./write-json.js') 71cb0ef41Sopenharmony_ciconst readJson = require('./read-json.js') 81cb0ef41Sopenharmony_ciconst git = require('@npmcli/git') 91cb0ef41Sopenharmony_ciconst commit = require('./commit.js') 101cb0ef41Sopenharmony_ciconst tag = require('./tag.js') 111cb0ef41Sopenharmony_ciconst log = require('proc-log') 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst runScript = require('@npmcli/run-script') 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_cimodule.exports = async (newversion, opts) => { 161cb0ef41Sopenharmony_ci const { 171cb0ef41Sopenharmony_ci path, 181cb0ef41Sopenharmony_ci allowSameVersion, 191cb0ef41Sopenharmony_ci gitTagVersion, 201cb0ef41Sopenharmony_ci ignoreScripts, 211cb0ef41Sopenharmony_ci preid, 221cb0ef41Sopenharmony_ci pkg, 231cb0ef41Sopenharmony_ci silent, 241cb0ef41Sopenharmony_ci } = opts 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci const { valid, clean, inc } = semver 271cb0ef41Sopenharmony_ci const current = pkg.version || '0.0.0' 281cb0ef41Sopenharmony_ci const currentClean = clean(current) 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci let newV 311cb0ef41Sopenharmony_ci if (valid(newversion, { loose: true })) { 321cb0ef41Sopenharmony_ci newV = clean(newversion, { loose: true }) 331cb0ef41Sopenharmony_ci } else if (newversion === 'from-git') { 341cb0ef41Sopenharmony_ci newV = await retrieveTag(opts) 351cb0ef41Sopenharmony_ci } else { 361cb0ef41Sopenharmony_ci newV = inc(currentClean, newversion, { loose: true }, preid) 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci if (!newV) { 401cb0ef41Sopenharmony_ci throw Object.assign(new Error('Invalid version: ' + newversion), { 411cb0ef41Sopenharmony_ci current, 421cb0ef41Sopenharmony_ci requested: newversion, 431cb0ef41Sopenharmony_ci }) 441cb0ef41Sopenharmony_ci } 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci if (newV === currentClean && !allowSameVersion) { 471cb0ef41Sopenharmony_ci throw Object.assign(new Error('Version not changed'), { 481cb0ef41Sopenharmony_ci current, 491cb0ef41Sopenharmony_ci requested: newversion, 501cb0ef41Sopenharmony_ci newVersion: newV, 511cb0ef41Sopenharmony_ci }) 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci const isGitDir = newversion === 'from-git' || await git.is(opts) 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci // ok! now we know the new version, and the old version is in pkg 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci // - check if git dir is clean 591cb0ef41Sopenharmony_ci // returns false if we should not keep doing git stuff 601cb0ef41Sopenharmony_ci const doGit = gitTagVersion && isGitDir && await enforceClean(opts) 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci if (!ignoreScripts) { 631cb0ef41Sopenharmony_ci await runScript({ 641cb0ef41Sopenharmony_ci ...opts, 651cb0ef41Sopenharmony_ci pkg, 661cb0ef41Sopenharmony_ci stdio: 'inherit', 671cb0ef41Sopenharmony_ci event: 'preversion', 681cb0ef41Sopenharmony_ci banner: !silent, 691cb0ef41Sopenharmony_ci env: { 701cb0ef41Sopenharmony_ci npm_old_version: current, 711cb0ef41Sopenharmony_ci npm_new_version: newV, 721cb0ef41Sopenharmony_ci }, 731cb0ef41Sopenharmony_ci }) 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci // - update the files 771cb0ef41Sopenharmony_ci pkg.version = newV 781cb0ef41Sopenharmony_ci delete pkg._id 791cb0ef41Sopenharmony_ci await writeJson(`${path}/package.json`, pkg) 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci // try to update shrinkwrap, but ok if this fails 821cb0ef41Sopenharmony_ci const locks = [`${path}/package-lock.json`, `${path}/npm-shrinkwrap.json`] 831cb0ef41Sopenharmony_ci const haveLocks = [] 841cb0ef41Sopenharmony_ci for (const lock of locks) { 851cb0ef41Sopenharmony_ci try { 861cb0ef41Sopenharmony_ci const sw = await readJson(lock) 871cb0ef41Sopenharmony_ci sw.version = newV 881cb0ef41Sopenharmony_ci if (sw.packages && sw.packages['']) { 891cb0ef41Sopenharmony_ci sw.packages[''].version = newV 901cb0ef41Sopenharmony_ci } 911cb0ef41Sopenharmony_ci await writeJson(lock, sw) 921cb0ef41Sopenharmony_ci haveLocks.push(lock) 931cb0ef41Sopenharmony_ci } catch { 941cb0ef41Sopenharmony_ci // ignore errors 951cb0ef41Sopenharmony_ci } 961cb0ef41Sopenharmony_ci } 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci if (!ignoreScripts) { 991cb0ef41Sopenharmony_ci await runScript({ 1001cb0ef41Sopenharmony_ci ...opts, 1011cb0ef41Sopenharmony_ci pkg, 1021cb0ef41Sopenharmony_ci stdio: 'inherit', 1031cb0ef41Sopenharmony_ci event: 'version', 1041cb0ef41Sopenharmony_ci banner: !silent, 1051cb0ef41Sopenharmony_ci env: { 1061cb0ef41Sopenharmony_ci npm_old_version: current, 1071cb0ef41Sopenharmony_ci npm_new_version: newV, 1081cb0ef41Sopenharmony_ci }, 1091cb0ef41Sopenharmony_ci }) 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci if (doGit) { 1131cb0ef41Sopenharmony_ci // - git add, git commit, git tag 1141cb0ef41Sopenharmony_ci await git.spawn(['add', `${path}/package.json`], opts) 1151cb0ef41Sopenharmony_ci // sometimes people .gitignore their lockfiles 1161cb0ef41Sopenharmony_ci for (const lock of haveLocks) { 1171cb0ef41Sopenharmony_ci await git.spawn(['add', lock], opts).catch(() => {}) 1181cb0ef41Sopenharmony_ci } 1191cb0ef41Sopenharmony_ci await commit(newV, opts) 1201cb0ef41Sopenharmony_ci await tag(newV, opts) 1211cb0ef41Sopenharmony_ci } else { 1221cb0ef41Sopenharmony_ci log.verbose('version', 'Not tagging: not in a git repo or no git cmd') 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci if (!ignoreScripts) { 1261cb0ef41Sopenharmony_ci await runScript({ 1271cb0ef41Sopenharmony_ci ...opts, 1281cb0ef41Sopenharmony_ci pkg, 1291cb0ef41Sopenharmony_ci stdio: 'inherit', 1301cb0ef41Sopenharmony_ci event: 'postversion', 1311cb0ef41Sopenharmony_ci banner: !silent, 1321cb0ef41Sopenharmony_ci env: { 1331cb0ef41Sopenharmony_ci npm_old_version: current, 1341cb0ef41Sopenharmony_ci npm_new_version: newV, 1351cb0ef41Sopenharmony_ci }, 1361cb0ef41Sopenharmony_ci }) 1371cb0ef41Sopenharmony_ci } 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci return newV 1401cb0ef41Sopenharmony_ci} 141