From fb08be37e27fe7cc790a22ae160cc845e7585575 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Thu, 1 Jul 2021 14:58:27 +0200 Subject: [PATCH] chore: edge release on each commit (#294) --- .github/workflows/test.yml | 6 ++ package.json | 1 + packages/nuxt3/package.json | 2 +- scripts/bump-edge.ts | 109 ++++++++++++++++++++++++++++++++++++ scripts/release-edge.sh | 26 +++++++++ yarn.lock | 1 + 6 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 scripts/bump-edge.ts create mode 100755 scripts/release-edge.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cc4962440d..ae4bb0a679 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -48,3 +48,9 @@ jobs: # - name: Coverage # uses: codecov/codecov-action@v1 + + - name: Release Edge + if: github.event_name == 'push' + run: ./scripts/release-edge.sh + env: + NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}} diff --git a/package.json b/package.json index e89abc35c4..acdcc68a77 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "esbuild": "^0.12.12", "eslint": "^7.29.0", "eslint-plugin-jsdoc": "^35.4.1", + "globby": "^11.0.4", "jest": "^27.0.6", "jiti": "^1.10.1", "lerna": "^4.0.0", diff --git a/packages/nuxt3/package.json b/packages/nuxt3/package.json index 6913d70b8c..00963d61a6 100644 --- a/packages/nuxt3/package.json +++ b/packages/nuxt3/package.json @@ -9,7 +9,7 @@ "dist" ], "scripts": { - "prepack": "unbuild" + "prepack": "unbuild || true" }, "bin": { "nu": "./bin/nuxt.js", diff --git a/scripts/bump-edge.ts b/scripts/bump-edge.ts new file mode 100644 index 0000000000..3344452b0d --- /dev/null +++ b/scripts/bump-edge.ts @@ -0,0 +1,109 @@ +import { promises as fsp } from 'fs' +import { resolve } from 'path' +import { execSync } from 'child_process' +import globby from 'globby' + +async function loadPackage (dir: string) { + const pkgPath = resolve(dir, 'package.json') + const data = JSON.parse(await fsp.readFile(pkgPath, 'utf-8').catch(() => '{}')) + const save = () => fsp.writeFile(pkgPath, JSON.stringify(data, null, 2) + '\n') + + const updateDeps = (reviver: Function) => { + for (const type of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) { + if (!data[type]) { continue } + for (const e of Object.entries(data[type])) { + const dep = { name: e[0], range: e[1], type } + delete data[type][dep.name] + const updated = reviver(dep) || dep + data[updated.type] = data[updated.type] || {} + data[updated.type][updated.name] = updated.range + } + } + } + + return { + dir, + data, + save, + updateDeps + } +} + +type ThenArg = T extends PromiseLike ? U : T +type Package = ThenArg> + +async function loadWorkspace (dir: string) { + const workspacePkg = await loadPackage(dir) + const pkgDirs = await globby(workspacePkg.data.workspaces || [], { onlyDirectories: true }) + + const packages: Package[] = [] + + for (const pkgDir of pkgDirs) { + const pkg = await loadPackage(pkgDir) + if (!pkg.data.name) { continue } + packages.push(pkg) + } + + const find = (name: string) => { + const pkg = packages.find(pkg => pkg.data.name === name) + if (!pkg) { + throw new Error('Workspace package not found: ' + name) + } + return pkg + } + + const rename = (from: string, to: string) => { + find(from).data.name = to + for (const pkg of packages) { + pkg.updateDeps((dep) => { + if (dep.name === from && !dep.range.startsWith('npm:')) { + dep.range = 'npm:' + to + '@' + dep.range + } + }) + } + } + + const setVersion = (name: string, newVersion: string) => { + find(name).data.version = newVersion + for (const pkg of packages) { + pkg.updateDeps((dep) => { + if (dep.name === name) { + dep.range = newVersion + } + }) + } + } + + const save = () => Promise.all(packages.map(pkg => pkg.save())) + + return { + dir, + workspacePkg, + packages, + save, + find, + rename, + setVersion + } +} + +async function main () { + const workspace = await loadWorkspace(process.cwd()) + + const commit = execSync('git rev-parse --short HEAD').toString('utf-8').trim() + const date = Math.round(Date.now() / (1000 * 60)) + + for (const pkg of workspace.packages.filter(p => !p.data.private)) { + workspace.setVersion(pkg.data.name, `${pkg.data.version}-${date}.${commit}`) + if (pkg.data.name !== 'nuxt3' /* TODO: Hardcoded! */) { + workspace.rename(pkg.data.name, pkg.data.name + '-edge') + } + } + + await workspace.save() +} + +main().catch((err) => { + console.error(err) + process.exit(1) +}) diff --git a/scripts/release-edge.sh b/scripts/release-edge.sh new file mode 100755 index 0000000000..d2f280fb72 --- /dev/null +++ b/scripts/release-edge.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Restore all git changes +git restore -s@ -SW -- packages examples + +# Bump versions to edge +yarn jiti ./scripts/bump-edge + +# Resolve yarn +yarn + +# Update token +if [[ ! -z ${NODE_AUTH_TOKEN} ]] ; then + echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" >> ~/.npmrc + echo "registry=https://registry.npmjs.org/" >> ~/.npmrc + echo "always-auth=true" >> ~/.npmrc + npm whoami +fi + +# Release packages +for p in packages/* ; do + pushd $p + echo "Publishing $p" + npm publish -q --access public # --dry-run + popd +done diff --git a/yarn.lock b/yarn.lock index 8ff77374e8..28fb3e8415 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9797,6 +9797,7 @@ __metadata: esbuild: ^0.12.12 eslint: ^7.29.0 eslint-plugin-jsdoc: ^35.4.1 + globby: ^11.0.4 jest: ^27.0.6 jiti: ^1.10.1 lerna: ^4.0.0