Skip to content

Commit

Permalink
chore: start edge releases
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 13, 2022
1 parent bf62e47 commit bbbbeb4
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"cross-env": "^7.0.3",
"eslint": "^8.13.0",
"eslint-plugin-jsdoc": "^39.2.0",
"jiti": "^1.13.0",
"nuxt": "^2",
"unbuild": "0.7.3",
"vitest": "^0.9.3",
Expand Down
110 changes: 110 additions & 0 deletions scripts/bump-edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { promises as fsp } from 'fs'
import { execSync } from 'child_process'
import { resolve } from 'pathe'
import { globby } from 'globby'

// Temporary forked from nuxt/framework

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> = T extends PromiseLike<infer U> ? U : T
type Package = ThenArg<ReturnType<typeof loadPackage>>

async function loadWorkspace (dir: string) {
const workspacePkg = await loadPackage(dir)
const pkgDirs = await globby(workspacePkg.data.workspaces || [], { onlyDirectories: true })

const packages: Package[] = [workspacePkg]

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}`)
workspace.rename(pkg.data.name, pkg.data.name + '-edge')
}

await workspace.save()
}

main().catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
})
27 changes: 27 additions & 0 deletions scripts/release-edge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# Temporary forked from nuxt/framework

set -xe

# Restore all git changes
git restore -s@ -SW -- .

# Bump versions to edge
yarn jiti ./scripts/bump-edge

# Resolve yarn
# YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install

# 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
echo "npmAuthToken: ${NODE_AUTH_TOKEN}" >> ~/.yarnrc.yml
npm whoami
fi

# Release packages
echo "Publishing package..."
yarn npm publish --access public --tolerate-republish

0 comments on commit bbbbeb4

Please sign in to comment.