@storm-software/workspace-tools
Version:
Tools for managing a Storm workspace, including various Nx generators and executors for common development tasks.
268 lines (248 loc) • 11.6 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
var _chunkCUPARBOSjs = require('./chunk-CUPARBOS.js');
var _chunkJTAXCQX6js = require('./chunk-JTAXCQX6.js');
var _chunk3GQAWCBQjs = require('./chunk-3GQAWCBQ.js');
// src/executors/npm-publish/executor.ts
var _child_process = require('child_process');
var _promises = require('fs/promises');
var LARGE_BUFFER = 1024 * 1e6;
async function npmPublishExecutorFn(options, context) {
const isDryRun = process.env.NX_DRY_RUN === "true" || options.dryRun || false;
if (!context.projectName) {
throw new Error("The `npm-publish` executor requires a `projectName`.");
}
const projectConfig = _optionalChain([context, 'access', _ => _.projectsConfigurations, 'optionalAccess', _2 => _2.projects, 'optionalAccess', _3 => _3[context.projectName]]);
if (!projectConfig) {
throw new Error(`Could not find project configuration for \`${context.projectName}\``);
}
const packageRoot = _chunkJTAXCQX6js.joinPaths.call(void 0, context.root, options.packageRoot || _chunkJTAXCQX6js.joinPaths.call(void 0, "dist", projectConfig.root));
const packageJsonPath = _chunkJTAXCQX6js.joinPaths.call(void 0, packageRoot, "package.json");
const packageJsonFile = await _promises.readFile.call(void 0, packageJsonPath, "utf8");
if (!packageJsonFile) {
throw new Error(`Could not find \`package.json\` at ${packageJsonPath}`);
}
const packageJson = JSON.parse(packageJsonFile);
const packageName = packageJson.name;
console.info(`\u{1F680} Running Storm NPM Publish executor on the ${packageName} package`);
const packageTxt = packageName === context.projectName ? `package "${packageName}"` : `package "${packageName}" from project "${context.projectName}"`;
if (packageJson.private === true) {
console.warn(`Skipped ${packageTxt}, because it has \`"private": true\` in ${packageJsonPath}`);
return {
success: true
};
}
await _chunkCUPARBOSjs.pnpmCatalogUpdate.call(void 0, packageRoot, context.root);
const npmPublishCommandSegments = [
`npm publish --json`
];
const npmViewCommandSegments = [
`npm view ${packageName} versions dist-tags --json`
];
const registry = options.registry ? options.registry : _child_process.execSync.call(void 0, "npm config get registry", {
cwd: packageRoot,
env: {
...process.env,
FORCE_COLOR: "true"
},
maxBuffer: LARGE_BUFFER,
killSignal: "SIGTERM"
}).toString().trim();
if (registry) {
npmPublishCommandSegments.push(`--registry="${registry}" `);
npmViewCommandSegments.push(`--registry="${registry}" `);
}
if (options.otp) {
npmPublishCommandSegments.push(`--otp="${options.otp}" `);
}
if (isDryRun) {
npmPublishCommandSegments.push("--dry-run");
}
npmPublishCommandSegments.push("--provenance --access=public ");
const tag = options.tag || _child_process.execSync.call(void 0, "npm config get tag", {
cwd: packageRoot,
env: {
...process.env,
FORCE_COLOR: "true"
},
maxBuffer: LARGE_BUFFER,
killSignal: "SIGTERM"
}).toString().trim();
if (tag) {
npmPublishCommandSegments.push(`--tag="${tag}" `);
}
if (!isDryRun) {
const currentVersion = packageJson.version;
try {
try {
const result = _child_process.execSync.call(void 0, npmViewCommandSegments.join(" "), {
cwd: packageRoot,
env: {
...process.env,
FORCE_COLOR: "true"
},
maxBuffer: LARGE_BUFFER,
killSignal: "SIGTERM"
});
const resultJson = JSON.parse(result.toString());
const distTags = resultJson["dist-tags"] || {};
if (distTags[tag] === currentVersion) {
console.warn(`Skipped ${packageTxt} because v${currentVersion} already exists in ${registry} with tag "${tag}"`);
return {
success: true
};
}
} catch (err) {
console.warn("\n ********************** \n");
console.warn(`An error occurred while checking for existing dist-tags
${JSON.stringify(err)}
Note: If this is the first time this package has been published to NPM, this can be ignored.
`);
console.info("");
}
try {
if (!isDryRun) {
const command = `npm dist-tag add ${packageName}@${currentVersion} ${tag} --registry="${registry}" `;
console.info(`Adding the dist-tag ${tag} - preparing to run the following:
${command}
`);
const result = _child_process.execSync.call(void 0, command, {
cwd: packageRoot,
env: {
...process.env,
FORCE_COLOR: "true"
},
maxBuffer: LARGE_BUFFER,
killSignal: "SIGTERM"
});
console.info(`Added the dist-tag ${tag} to v${currentVersion} for registry "${registry}"
Execution response: ${result.toString()}
`);
} else {
console.info(`Would add the dist-tag ${tag} to v${currentVersion} for registry "${registry}", but [dry-run] was set.
`);
}
return {
success: true
};
} catch (err) {
try {
console.warn("\n ********************** \n");
let error = err;
if (Buffer.isBuffer(error)) {
error = error.toString();
}
console.warn(`An error occurred while adding dist-tags:
${error}
Note: If this is the first time this package has been published to NPM, this can be ignored.
`);
console.info("");
const stdoutData = JSON.parse(_optionalChain([err, 'access', _4 => _4.stdout, 'optionalAccess', _5 => _5.toString, 'call', _6 => _6()]) || "{}");
if (_optionalChain([stdoutData, 'optionalAccess', _7 => _7.error]) && !(_optionalChain([stdoutData, 'access', _8 => _8.error, 'optionalAccess', _9 => _9.code, 'optionalAccess', _10 => _10.includes, 'call', _11 => _11("E404")]) && _optionalChain([stdoutData, 'access', _12 => _12.error, 'optionalAccess', _13 => _13.summary, 'optionalAccess', _14 => _14.includes, 'call', _15 => _15("no such package available")])) && !(_optionalChain([err, 'access', _16 => _16.stderr, 'optionalAccess', _17 => _17.toString, 'call', _18 => _18(), 'access', _19 => _19.includes, 'call', _20 => _20("E404")]) && _optionalChain([err, 'access', _21 => _21.stderr, 'optionalAccess', _22 => _22.toString, 'call', _23 => _23(), 'access', _24 => _24.includes, 'call', _25 => _25("no such package available")]))) {
console.error("npm dist-tag add error please see below for more information:");
if (stdoutData.error.summary) {
console.error(_optionalChain([stdoutData, 'access', _26 => _26.error, 'optionalAccess', _27 => _27.summary]));
}
if (stdoutData.error.detail) {
console.error(_optionalChain([stdoutData, 'access', _28 => _28.error, 'optionalAccess', _29 => _29.detail]));
}
if (context.isVerbose) {
console.error(`npm dist-tag add stdout: ${JSON.stringify(stdoutData, null, 2)}`);
}
return {
success: false
};
}
} catch (err2) {
console.error(`Something unexpected went wrong when processing the npm dist-tag add output
${JSON.stringify(err2)}`);
return {
success: false
};
}
}
} catch (err) {
let error = err;
if (Buffer.isBuffer(error)) {
error = error.toString();
}
console.error("\n ********************** \n");
console.info("");
console.error("An error occured trying to run the npm dist-tag add command.");
console.error(error);
console.info("");
const stdoutData = JSON.parse(_optionalChain([err, 'access', _30 => _30.stdout, 'optionalAccess', _31 => _31.toString, 'call', _32 => _32()]) || "{}");
if (!(_optionalChain([stdoutData, 'access', _33 => _33.error, 'optionalAccess', _34 => _34.code, 'optionalAccess', _35 => _35.includes, 'call', _36 => _36("E404")]) && _optionalChain([stdoutData, 'access', _37 => _37.error, 'optionalAccess', _38 => _38.summary, 'optionalAccess', _39 => _39.toLowerCase, 'call', _40 => _40(), 'access', _41 => _41.includes, 'call', _42 => _42("not found")])) && !(_optionalChain([err, 'access', _43 => _43.stderr, 'optionalAccess', _44 => _44.toString, 'call', _45 => _45(), 'access', _46 => _46.includes, 'call', _47 => _47("E404")]) && _optionalChain([err, 'access', _48 => _48.stderr, 'optionalAccess', _49 => _49.toString, 'call', _50 => _50(), 'access', _51 => _51.toLowerCase, 'call', _52 => _52(), 'access', _53 => _53.includes, 'call', _54 => _54("not found")]))) {
console.error(`Something unexpected went wrong when checking for existing dist-tags.
Error: ${JSON.stringify(err)}
`);
return {
success: false
};
}
}
}
try {
const cwd = packageRoot;
const command = npmPublishCommandSegments.join(" ");
console.info(`Running publish command "${command}" in current working directory: "${cwd}" `);
const result = _child_process.execSync.call(void 0, command, {
cwd,
env: {
...process.env,
FORCE_COLOR: "true"
},
maxBuffer: LARGE_BUFFER,
killSignal: "SIGTERM"
});
if (isDryRun) {
console.info(`Would publish to ${registry} with tag "${tag}", but [dry-run] was set ${result ? `
Execution response: ${result.toString()}` : ""}
`);
} else {
console.info(`Published to ${registry} with tag "${tag}" ${result ? `
Execution response: ${result.toString()}` : ""}
`);
}
return {
success: true
};
} catch (err) {
try {
console.error("\n ********************** \n");
console.info("");
console.error("An error occured running pnpm publish.");
console.error("Please see below for more information:");
console.info("");
const stdoutData = JSON.parse(_optionalChain([err, 'access', _55 => _55.stdout, 'optionalAccess', _56 => _56.toString, 'call', _57 => _57()]) || "{}");
if (stdoutData.error.summary) {
console.error(stdoutData.error.summary);
console.error(stdoutData.error.summary);
}
if (stdoutData.error.detail) {
console.error(stdoutData.error.detail);
}
if (context.isVerbose) {
console.error(`npm publish stdout:
${JSON.stringify(stdoutData, null, 2)}`);
}
console.error("\n ********************** \n");
return {
success: false
};
} catch (err2) {
let error = err2;
if (Buffer.isBuffer(error)) {
error = error.toString();
}
console.error(`Something unexpected went wrong when processing the npm publish output
Error: ${JSON.stringify(error)}
`);
console.error("\n ********************** \n");
return {
success: false
};
}
}
}
_chunk3GQAWCBQjs.__name.call(void 0, npmPublishExecutorFn, "npmPublishExecutorFn");
exports.LARGE_BUFFER = LARGE_BUFFER; exports.npmPublishExecutorFn = npmPublishExecutorFn;