'use strict'; var node_child_process = require('node:child_process'); var fs = require('node:fs'); var node_os = require('node:os'); var path = require('node:path'); var core = require('@pkgr/core'); var semver = require('semver'); var __defProp = Object.defineProperty; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; const PREVERSION_TAGS = ["alpha", "beta"]; const getPreversionTag = (version) => PREVERSION_TAGS.find( (tag) => version === tag || new RegExp(`[._-]${tag}[._-]?\\d*$`).test(version) ); const pkgPath = core.findUp(process.cwd()); const pkgContent = fs.readFileSync(pkgPath, "utf8"); const pkg = JSON.parse(pkgContent); const jsonRegex = new RegExp('[[{](?:[\\d\\t\\n\\r +,.:[\\]aeflnr-u{}-]|".*?")+[\\]}]', "gis"); const createStdioError = (error) => { var _a, _b; return new Error( ((_a = error.stderr) == null ? void 0 : _a.toString()) || ((_b = error.stdout) == null ? void 0 : _b.toString()) || error.message, { cause: error } ); }; const getPreversion = (tag, version = tag) => { if (version.startsWith("v")) { version = version.slice(1); } else if (version === tag) { let distTags; try { distTags = JSON.parse( node_child_process.execSync(`npm info ${pkg.name} dist-tags --json`).toString() ); } catch (err) { const error = err; if (error.stdout) { const content = error.stdout.toString(); const matched = content.match(jsonRegex); if (matched == null ? void 0 : matched[0]) { const { error: { code } = {} } = JSON.parse(matched[0]); if (code === "E404") { distTags = {}; } } } if (!distTags) { throw createStdioError(error); } } const { [tag]: preTag, latest } = distTags; if (preTag != null && latest != null && semver.gt(preTag, latest)) { version = preTag.endsWith(`-${tag}`) ? preTag + ".0" : preTag.replace( new RegExp(`(-${tag}\\.)(\\d+)$`), (_, $0, $1) => $0 + (+$1 + 1) ); } else { version = (latest == null ? "0.0.0" : semver.minVersion(`>${latest}`).format()) + `-${tag}`; } } return version; }; const preversion = ({ branch, message, preversion: preversion2, tag }) => { var _a; tag != null ? tag : tag = preversion2 && getPreversionTag(preversion2) || PREVERSION_TAGS[1]; if (tag === "latest") { console.error("Publish `latest` tag via this script is not permitted."); process.exitCode = 1; return; } const { HOME = node_os.homedir(), NPM_TOKEN } = process.env; const npmrcPath = path.resolve(HOME, ".npmrc"); if (fs.existsSync(npmrcPath)) { console.log("Found existing `.npmrc` file"); } else if (NPM_TOKEN) { console.log("No `.npmrc` file found, creating one"); fs.writeFileSync(npmrcPath, `//registry.npmjs.org/:_authToken=${NPM_TOKEN}`); } else { console.error( "No `.npmrc` found nor `NPM_TOKEN` provided, unable to publish packages" ); process.exitCode = 1; return; } const newPkg = __spreadValues({}, pkg); newPkg.version = preversion2 = getPreversion(tag, preversion2); fs.writeFileSync(pkgPath, JSON.stringify(newPkg, null, 2)); try { node_child_process.execSync("npx clean-pkg-json"); node_child_process.execSync( `npm publish ${((_a = pkg.publishConfig) == null ? void 0 : _a.directory) ? `./${pkg.publishConfig.directory}` : ""} --tag ${tag}` ); } catch (err) { const error = err; fs.writeFileSync(pkgPath, pkgContent); throw createStdioError(error); } if (!branch) { fs.writeFileSync(pkgPath, pkgContent); return; } const commitMessage = message.replaceAll(/{{\s*version\s*}}/g, preversion2); node_child_process.execSync(`git commit -am "${commitMessage}"`); node_child_process.execSync(`git push --follow-tags origin "${branch}"`); }; exports.getPreversion = getPreversion; exports.getPreversionTag = getPreversionTag; exports.pkg = pkg; exports.preversion = preversion;