Skip to content

Commit

Permalink
fix(core): programmatic release API should throw on error (#20448)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry authored Nov 28, 2023
1 parent 8c14244 commit adefe63
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 393 deletions.
168 changes: 89 additions & 79 deletions packages/js/src/executors/release-publish/release-publish.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,96 +76,106 @@ export default async function runExecutor(
const registry =
options.registry ?? execSync(`npm config get registry`).toString().trim();
const tag = options.tag ?? execSync(`npm config get tag`).toString().trim();
const currentVersion = projectPackageJson.version;

try {
const result = execSync(npmViewCommandSegments.join(' '), {
env: processEnv(true),
cwd: packageRoot,
stdio: ['ignore', 'pipe', 'pipe'],
});

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,
};
}

if (resultJson.versions.includes(currentVersion)) {
try {
if (!options.dryRun) {
execSync(
`npm dist-tag add ${packageName}@${currentVersion} ${tag} --registry=${registry}`,
{
env: processEnv(true),
cwd: packageRoot,
stdio: 'ignore',
}
);
console.log(
`Added the dist-tag ${tag} to v${currentVersion} for registry ${registry}.\n`
);
} else {
console.log(
`Would add the dist-tag ${tag} to v${currentVersion} for registry ${registry}, but ${chalk.keyword(
'orange'
)('[dry-run]')} was set.\n`
);
}
/**
* In a dry-run scenario, it is most likely that all commands are being run with dry-run, therefore
* the most up to date/relevant version might not exist on disk for us to read and make the npm view
* request with.
*
* Therefore, so as to not produce misleading output in dry around dist-tags being altered, we do not
* perform the npm view step, and just show npm publish's dry-run output.
*/
if (!options.dryRun) {
const currentVersion = projectPackageJson.version;
try {
const result = execSync(npmViewCommandSegments.join(' '), {
env: processEnv(true),
cwd: packageRoot,
stdio: ['ignore', 'pipe', 'pipe'],
});

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) {
try {
const stdoutData = JSON.parse(err.stdout?.toString() || '{}');

console.error('npm dist-tag add error:');
if (stdoutData.error.summary) {
console.error(stdoutData.error.summary);
}
if (stdoutData.error.detail) {
console.error(stdoutData.error.detail);
}
}

if (context.isVerbose) {
console.error('npm dist-tag add stdout:');
console.error(JSON.stringify(stdoutData, null, 2));
if (resultJson.versions.includes(currentVersion)) {
try {
if (!options.dryRun) {
execSync(
`npm dist-tag add ${packageName}@${currentVersion} ${tag} --registry=${registry}`,
{
env: processEnv(true),
cwd: packageRoot,
stdio: 'ignore',
}
);
console.log(
`Added the dist-tag ${tag} to v${currentVersion} for registry ${registry}.\n`
);
} else {
console.log(
`Would add the dist-tag ${tag} to v${currentVersion} for registry ${registry}, but ${chalk.keyword(
'orange'
)('[dry-run]')} was set.\n`
);
}
return {
success: false,
success: true,
};
} catch (err) {
console.error(
'Something unexpected went wrong when processing the npm dist-tag add output\n',
err
);
return {
success: false,
};
try {
const stdoutData = JSON.parse(err.stdout?.toString() || '{}');

console.error('npm dist-tag add error:');
if (stdoutData.error.summary) {
console.error(stdoutData.error.summary);
}
if (stdoutData.error.detail) {
console.error(stdoutData.error.detail);
}

if (context.isVerbose) {
console.error('npm dist-tag add stdout:');
console.error(JSON.stringify(stdoutData, null, 2));
}
return {
success: false,
};
} catch (err) {
console.error(
'Something unexpected went wrong when processing the npm dist-tag add output\n',
err
);
return {
success: false,
};
}
}
}
}
} catch (err) {
const stdoutData = JSON.parse(err.stdout?.toString() || '{}');
// If the error is that the package doesn't exist, then we can ignore it because we will be publishing it for the first time in the next step
if (
!(
stdoutData.error?.code?.includes('E404') &&
stdoutData.error?.summary?.includes('no such package available')
)
) {
console.error(
`Something unexpected went wrong when checking for existing dist-tags.\n`,
err
);
return {
success: false,
};
} catch (err) {
const stdoutData = JSON.parse(err.stdout?.toString() || '{}');
// If the error is that the package doesn't exist, then we can ignore it because we will be publishing it for the first time in the next step
if (
!(
stdoutData.error?.code?.includes('E404') &&
stdoutData.error?.summary?.includes('no such package available')
)
) {
console.error(
`Something unexpected went wrong when checking for existing dist-tags.\n`,
err
);
return {
success: false,
};
}
}
}

Expand Down
Loading

0 comments on commit adefe63

Please sign in to comment.