Skip to content

Commit

Permalink
Update src/dev/build/tasks/create_package_json_task.ts
Browse files Browse the repository at this point in the history
Signed-off-by: suzhou <suzhou@amazon.com>

Co-authored-by: Miki <amoo_miki@yahoo.com>
  • Loading branch information
SuZhou-Joe and AMoo-Miki authored Feb 23, 2023
1 parent 730d694 commit b09c3eb
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/dev/build/tasks/create_package_json_task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,38 @@ export const CreatePackageJson: Task = {
async run(config, log, build) {
const pkg = config.getOpenSearchDashboardsPkg();
/**
* OpenSearch server has a logic that if the pkg.branch is "main",
* set the docVersion to latest. So here we exclude main.
* OpenSearch server uses the `branch` property from `package.json` to
* build links to the documentation. If set to `main`, it would use `/latest`
* and if not, it would use the `version` to construct URLs.
*/
const semverResult = parse(config.getBuildVersion());
const shouldPatch = semverResult && pkg.branch !== 'main';
const branch = shouldPatch ? `${semverResult.major}.${semverResult.minor}` : pkg.branch;
if (shouldPatch) {
log.info(`Patch branch property: ${branch}`);
const buildVersion = config.getBuildVersion();
let branch;
if (pkg.branch === 'main') {
branch = pkg.branch;
} else {
const parsedBuildVersion = parse(buildVersion);
if (parsedBuildVersion) {
branch = `${parsedBuildVersion.major}.${parsedBuildVersion.minor}`;
log.info(`Updating package.branch to ${branch}`);
} else {
const validDocPathsPattern = /^\d+\.\d+$/;
if (validDocPathsPattern.test(pkg.branch)) {
branch = pkg.branch;
} else {
// package version was not parsable and branch is unusable
throw new Error(
`Failed to identify documentation path while generating package.json: encountered invalid build version (${buildVersion}) and branch property (${pkg.branch}).`
);
}
}
}

const newPkg = {
name: pkg.name,
private: true,
description: pkg.description,
keywords: pkg.keywords,
version: config.getBuildVersion(),
version: buildVersion,
branch,
build: {
number: config.getBuildNumber(),
Expand Down

0 comments on commit b09c3eb

Please sign in to comment.