Skip to content

Commit

Permalink
fix(javascript): bun setup for javascript workflows (#3176)
Browse files Browse the repository at this point in the history
[Bun](https://bun.sh/) added as a package manager but the workflows are still using NodeJS. This is to fix this.


---
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
abdsahin committed Mar 25, 2024
1 parent bff36f7 commit d3ba33e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 27 deletions.
5 changes: 5 additions & 0 deletions src/cdk/jsii-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,11 @@ export class JsiiProject extends TypeScriptProject {
uses: "pnpm/action-setup@v3",
with: { version: this.package.pnpmVersion },
});
} else if (this.package.packageManager === NodePackageManager.BUN) {
prePublishSteps.push({
name: "Setup bun",
uses: "oven-sh/setup-bun@v1",
});
}

prePublishSteps.push(
Expand Down
61 changes: 34 additions & 27 deletions src/javascript/node-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,37 +1028,44 @@ export class NodeProject extends GitHubProject {
uses: "pnpm/action-setup@v3",
with: { version: this.package.pnpmVersion },
});
}

if (this.nodeVersion || this.workflowPackageCache) {
const cache =
this.package.packageManager === NodePackageManager.YARN
? "yarn"
: this.package.packageManager === NodePackageManager.YARN2
? "yarn"
: this.package.packageManager === NodePackageManager.YARN_CLASSIC
? "yarn"
: this.package.packageManager === NodePackageManager.YARN_BERRY
? "yarn"
: this.packageManager === NodePackageManager.BUN
? "bun"
: this.package.packageManager === NodePackageManager.PNPM
? "pnpm"
: "npm";
} else if (this.package.packageManager === NodePackageManager.BUN) {
install.push({
name: "Setup Node.js",
uses: "actions/setup-node@v4",
with: {
...(this.nodeVersion && {
"node-version": this.nodeVersion,
}),
...(this.workflowPackageCache && {
cache,
}),
},
name: "Setup bun",
uses: "oven-sh/setup-bun@v1",
});
}

if (this.package.packageManager !== NodePackageManager.BUN) {
if (this.nodeVersion || this.workflowPackageCache) {
const cache =
this.package.packageManager === NodePackageManager.YARN
? "yarn"
: this.package.packageManager === NodePackageManager.YARN2
? "yarn"
: this.package.packageManager === NodePackageManager.YARN_CLASSIC
? "yarn"
: this.package.packageManager === NodePackageManager.YARN_BERRY
? "yarn"
: this.packageManager === NodePackageManager.BUN
? "bun"
: this.package.packageManager === NodePackageManager.PNPM
? "pnpm"
: "npm";
install.push({
name: "Setup Node.js",
uses: "actions/setup-node@v4",
with: {
...(this.nodeVersion && {
"node-version": this.nodeVersion,
}),
...(this.workflowPackageCache && {
cache,
}),
},
});
}
}

const mutable = options.mutable ?? false;

if (this.package.scopedPackagesOptions) {
Expand Down
55 changes: 55 additions & 0 deletions test/javascript/node-project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,61 @@ test("workflowGitIdentity can be used to customize the git identity used in buil
});
});

describe("Setup bun", () => {
const setupBunIndex = (job: any) =>
job.steps.findIndex((step: any) => step.name === "Setup bun");
const setupNodeIndex = (job: any) =>
job.steps.findIndex((step: any) => step.name === "Setup Node.js");

test("Setup bun should not run without bun selected as the package manager", () => {
// WHEN
const options = {};

const project = new TestNodeProject(options);

// THEN
const output = synthSnapshot(project);
const buildWorkflow = yaml.parse(output[".github/workflows/build.yml"]);
expect(setupBunIndex(buildWorkflow.jobs.build)).toEqual(-1);
const releaseWorkflow = yaml.parse(output[".github/workflows/release.yml"]);
expect(setupBunIndex(releaseWorkflow.jobs.release)).toEqual(-1);
const upgradeWorkflow = yaml.parse(
output[".github/workflows/upgrade-main.yml"]
);
expect(setupBunIndex(upgradeWorkflow.jobs.upgrade)).toEqual(-1);
});

test("Setup Node.js should not be used if bun is selected as the package manager", () => {
// WHEN
const options = {
workflowPackageCache: true,
packageManager: NodePackageManager.BUN,
};

const project = new TestNodeProject(options);

// THEN
const output = synthSnapshot(project);
const buildWorkflow = yaml.parse(output[".github/workflows/build.yml"]);
expect(setupBunIndex(buildWorkflow.jobs.build)).toBeGreaterThanOrEqual(0);
expect(setupNodeIndex(buildWorkflow.jobs.build)).toEqual(-1);

const releaseWorkflow = yaml.parse(output[".github/workflows/release.yml"]);
expect(setupBunIndex(releaseWorkflow.jobs.release)).toBeGreaterThanOrEqual(
0
);
expect(setupNodeIndex(releaseWorkflow.jobs.release)).toEqual(-1);

const upgradeWorkflow = yaml.parse(
output[".github/workflows/upgrade-main.yml"]
);
expect(setupBunIndex(upgradeWorkflow.jobs.upgrade)).toBeGreaterThanOrEqual(
0
);
expect(setupNodeIndex(upgradeWorkflow.jobs.upgrade)).toEqual(-1);
});
});

describe("Setup pnpm", () => {
const setupPnpmIndex = (job: any) =>
job.steps.findIndex((step: any) => step.name === "Setup pnpm");
Expand Down

0 comments on commit d3ba33e

Please sign in to comment.