From 3916d5baf303a0ad386bb0a47a2c83aad32677d5 Mon Sep 17 00:00:00 2001 From: James Daniels Date: Wed, 8 Nov 2023 03:57:00 -0500 Subject: [PATCH] Scripts --- .github/workflows/test.yml | 7 +++--- package.json | 12 ++++------ packages/adapter-nextjs/test.ts | 10 ++++++-- publish.js | 42 --------------------------------- scripts/build.js | 5 ++++ scripts/github.js | 36 ++++++++++++++++++++++++++++ scripts/publish.js | 36 ++++++++++++++++++++++++++++ scripts/test.js | 5 ++++ 8 files changed, 99 insertions(+), 54 deletions(-) delete mode 100755 publish.js create mode 100755 scripts/build.js create mode 100644 scripts/github.js create mode 100755 scripts/publish.js create mode 100755 scripts/test.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7ab39cc5..23bf1e82 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,6 +4,7 @@ on: push: branches: - main + - monorepo paths-ignore: - "**/*.md" pull_request: @@ -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: @@ -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: @@ -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: diff --git a/package.json b/package.json index 2b1a0a62..aebd1956 100644 --- a/package.json +++ b/package.json @@ -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/*" diff --git a/packages/adapter-nextjs/test.ts b/packages/adapter-nextjs/test.ts index 049fb8af..c17e4e33 100644 --- a/packages/adapter-nextjs/test.ts +++ b/packages/adapter-nextjs/test.ts @@ -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 }); diff --git a/publish.js b/publish.js deleted file mode 100755 index bdcf2737..00000000 --- a/publish.js +++ /dev/null @@ -1,42 +0,0 @@ -#! /usr/bin/env node -const { execSync } = require("child_process"); -const { writeFileSync, readFileSync } = require("fs"); -const { join, basename } = require("path"); - -const [, packageFromRef, versionFromRef,, prerelease] = /^refs\/tags\/(.+)-v(\d\d*\.\d\d*(\.\d\d*)?(-.+)?)$/.exec(process.env.GITHUB_REF ?? "") ?? []; -const ref = process.env.GITHUB_SHA ?? "HEAD"; -const shortSHA = execSync(`git rev-parse --short ${ref}`).toString().trim(); - -const lernaList= JSON.parse(execSync(`lerna list --json ${packageFromRef ? '' : '--since'}`).toString()); -if (packageFromRef && !lernaList.find(it => basename(it.location) === packageFromRef)) { - throw `Lerna didn't find ${packageFromRef} in this workspace`; -} - -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 lernaList) { - if (lerna.private) continue; - if (packageFromRef && packageFromRef !== basename(lerna.location)) continue; - if (versionFromRef && versionFromRef.split('-')[0] !== lerna.version) { - throw `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 }); -} diff --git a/scripts/build.js b/scripts/build.js new file mode 100755 index 00000000..9d9b186f --- /dev/null +++ b/scripts/build.js @@ -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" }); diff --git a/scripts/github.js b/scripts/github.js new file mode 100644 index 00000000..7ca59672 --- /dev/null +++ b/scripts/github.js @@ -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, +}; diff --git a/scripts/publish.js b/scripts/publish.js new file mode 100755 index 00000000..f7b19766 --- /dev/null +++ b/scripts/publish.js @@ -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 }); +} diff --git a/scripts/test.js b/scripts/test.js new file mode 100755 index 00000000..bb5b34bb --- /dev/null +++ b/scripts/test.js @@ -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" });