Skip to content

Commit

Permalink
Scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed Nov 8, 2023
1 parent b8fe25f commit 3916d5b
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 54 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- monorepo
paths-ignore:
- "**/*.md"
pull_request:
Expand Down Expand Up @@ -37,7 +38,7 @@ jobs:
if: steps.node_modules_cache.outputs.cache-hit != 'true'
run: npm ci
- name: Build
run: npm run build:changed
run: npm run build
- name: 'Upload Artifact'
uses: actions/upload-artifact@v3
with:
Expand Down Expand Up @@ -82,7 +83,7 @@ jobs:
- name: Rsync Artifacts
run: rsync -a artifact/ packages
- name: Test
run: npm run test:changed
run: npm run test

# Break the branch protection test into a seperate step, so we can manage the matrix more easily
test_and_contribute:
Expand All @@ -96,7 +97,7 @@ jobs:
runs-on: ubuntu-latest
name: Publish (NPM)
needs: ['build', 'test']
if: ${{ github.ref == 'refs/heads/main' || github.event_name == 'release' }}
if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/monorepo' || github.event_name == 'release' }}
permissions:
id-token: write
steps:
Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
"name": "root",
"private": true,
"scripts": {
"build": "lerna run build",
"build:changed": "lerna run build --since",
"test": "lerna run test --verbose --no-bail",
"test:changed": "lerna run test --since --no-bail",
"publish": "./publish.js",
"lint": "eslint packages/*",
"lint:fix": "eslint packages/* --fix"
"build": "./scripts/build.js",
"test": "./scripts/test.js",
"publish": "./scripts/publish.js",
"lint": "eslint packages/* scripts/*",
"lint:fix": "eslint packages/* scripts/* --fix"
},
"workspaces": [
"packages/*"
Expand Down
10 changes: 8 additions & 2 deletions packages/adapter-nextjs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ tempDir((err, cwd, cleanup) => {
if (err) throw err;
const projectDir = join(cwd, "e2e");
const root = dirname(fileURLToPath(import.meta.url));
execSync(`npx -y -p ${root} apphosting-adapter-nextjs-create ${projectDir}`, { cwd, stdio: "inherit" });
execSync(`npx -y -p ${root} apphosting-adapter-nextjs-build`, { cwd: projectDir, stdio: "inherit" });
execSync(`npx -y -p ${root} apphosting-adapter-nextjs-create ${projectDir}`, {
cwd,
stdio: "inherit",
});
execSync(`npx -y -p ${root} apphosting-adapter-nextjs-build`, {
cwd: projectDir,
stdio: "inherit",
});
if (!existsSync(join(projectDir, ".next"))) throw new Error(`next app wasn't build`);
if (!process.env.GITHUB_ACTION) {
execSync(`rm -rf e2e`, { cwd });
Expand Down
42 changes: 0 additions & 42 deletions publish.js

This file was deleted.

5 changes: 5 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /usr/bin/env node
const { spawn } = require("child_process");
const { lernaScopeArgs } = require("./github.js");

spawn("lerna", ["run", "build", ...lernaScopeArgs], { stdio: "inherit" });
36 changes: 36 additions & 0 deletions scripts/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#! /usr/bin/env node
const { execSync } = require("child_process");
const { basename } = require("path");

const [, packageFromRef, versionFromRef, , prerelease] =
/^refs\/tags\/(.+)-v(\d\d*\.\d\d*(\.\d\d*)?(-.+)?)$/.exec(process.env.GITHUB_REF ?? "") ?? [];

const lernaList = JSON.parse(
execSync(`lerna list --json ${packageFromRef ? "" : "--since"}`, {
stdio: ["ignore", "pipe", "ignore"],
}).toString(),
);

const ref = process.env.GITHUB_SHA ?? "HEAD";
const shortSHA = execSync(`git rev-parse --short ${ref}`).toString().trim();

const filteredLernaList = lernaList.filter((lerna) => {
if (lerna.private) return false;
if (packageFromRef && packageFromRef !== basename(lerna.location)) return false;
return true;
});

if (packageFromRef && filteredLernaList.length === 0) {
throw new Error(`Lerna didn't find ${packageFromRef} in this workspace`);
}

const lernaScopeArgs = filteredLernaList.map(({ name }) => ["--scope", name]).flat();

module.exports = {
packageFromRef,
versionFromRef,
prerelease: !packageFromRef || !!prerelease,
filteredLernaList,
shortSHA,
lernaScopeArgs,
};
36 changes: 36 additions & 0 deletions scripts/publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#! /usr/bin/env node
const { execSync } = require("child_process");
const { writeFileSync, readFileSync } = require("fs");
const { join } = require("path");
const { filteredLernaList, versionFromRef, shortSHA, prerelease } = require("./github.js");

const wombatDressingRoomTokens = new Map([
// Disabling this until I can get wombat access to this org
// ['firebase-frameworks', process.env.FIREBASE_FRAMEWORKS_NPM_TOKEN],
// ['@apphosting/adapter-nextjs', process.env.ADAPTER_NEXTJS_NPM_TOKEN],
]);

wombatDressingRoomTokens.forEach((token, pkg) => {
writeFileSync(".npmrc", `//wombat-dressing-room.appspot.com/${pkg}/:_authToken=${token}\n`, {
flag: "a+",
});
});

for (const lerna of filteredLernaList) {
if (versionFromRef && versionFromRef.split("-")[0] !== lerna.version) {
throw new Error(
`Cowardly refusing to publish ${lerna.name}@${versionFromRef} from ${lerna.version}, version needs to be bumped in source.`,
);
}
const version = versionFromRef || `${lerna.version}-canary.${shortSHA}`;
const cwd = lerna.location;
const tag = versionFromRef ? (prerelease ? "next" : "latest") : "canary";
const packageJsonPath = join(lerna.location, "package.json");
const packageJson = JSON.parse(readFileSync(packageJsonPath).toString());
packageJson.version = version;
writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, 2));
const registry = wombatDressingRoomTokens.get(lerna.name)
? `https://wombat-dressing-room.appspot.com/${lerna.name}/_ns`
: "https://registry.npmjs.org";
execSync(`npm publish --registry ${registry} --access public --tag ${tag} --provenance`, { cwd });
}
5 changes: 5 additions & 0 deletions scripts/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /usr/bin/env node
const { spawn } = require("child_process");
const { lernaScopeArgs } = require("./github.js");

spawn("lerna", ["run", "test", "--verbose", "--no-bail", ...lernaScopeArgs], { stdio: "inherit" });

0 comments on commit 3916d5b

Please sign in to comment.