Skip to content

Commit

Permalink
feat: add lazy builder
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jun 25, 2022
1 parent 22954ad commit 4c59a0e
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 26 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
"bin": "./src/main/js/cli.js",
"scripts": {
"test": "NPM_REGISTRY='http://localhost:4873' NPM_TOKEN='mRv6eIuiaggXGb9ZDFCtBA==' c8 uvu ./src/test -i fixtures -i utils && c8 report -r lcov",
"test:it": "NPM_REGISTRY='http://localhost:4873' NPM_TOKEN='mRv6eIuiaggXGb9ZDFCtBA==' node ./src/test/js/integration.test.js",
"docs": "mkdir -p docs && cp ./README.md ./docs/README.md"
},
"dependencies": {
"@semrel-extra/topo": "^1.4.1",
"git-glob-cp": "^1.6.2",
"git-glob-cp": "^1.7.1",
"ini": "^3.0.0",
"zx-extra": "^1.7.0"
},
"devDependencies": {
Expand Down
42 changes: 38 additions & 4 deletions src/main/js/build.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
import {ctx} from 'zx-extra'
import {ctx, tempy, fs} from 'zx-extra'
import {copy} from 'git-glob-cp'
import ini from 'ini'
import {traverseDeps} from './deps.js'
import {parseEnv} from './publish.js'

export const build = (pkg, packages) => ctx(async ($) => {
if (pkg.built) return true

await traverseDeps(pkg, packages, async (_, {pkg}) => build(pkg, packages))

if (pkg.manifest.scripts?.build && pkg.manifest?.release?.build) {
$.cwd = pkg.absPath
await $`npm run build`
console.log(`built '${pkg.name}'`)
if (pkg.changes?.length === 0 && pkg.manifest?.release?.fetch) await fetchPkg(pkg)

if (!pkg.fetched) {
$.cwd = pkg.absPath
await $`npm run build`
console.log(`built '${pkg.name}'`)
}
}

pkg.built = true

return true
})

const fetchPkg = (pkg) => ctx(async ($) => {
try {
const cwd = pkg.absPath
const {npmRegistry} = parseEnv($.env)
const temp = tempy.temporaryDirectory()
const npmrc = ini.parse(await fs.readFile(`${cwd}/.npmrc`, 'utf8'))
const token = getAuthToken(npmRegistry, npmrc)
const auth = `Authorization: Bearer ${token}`
const tarball = getTarballUrl(npmRegistry, pkg.name, pkg.version)

await $`wget --header=${auth} -qO- ${tarball} | tar xvz -C ${temp}`
await copy({from: ['**/*', '!package.json'], to: cwd, cwd: `${temp}/package`})

pkg.fetched = true
console.log(`fetched '${pkg.name}@${pkg.version}'`)
} catch (e) {
console.log(`fetching '${pkg.name}@${pkg.version}' failed`, e)
}
})

// NOTE registry-auth-token does not work with localhost:4873
const getAuthToken = (registry, npmrc) =>
(Object.entries(npmrc).find(([reg]) => reg.startsWith(registry.replace(/^https?/, ''))) || [])[1]

// $`npm view ${name}@${version} dist.tarball`
const getTarballUrl = (registry, name, version) => `${registry}/${name}/-/${name.replace(/^.+(%2f|\/)/,'')}-${version}.tgz`
1 change: 1 addition & 0 deletions src/main/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {publish, getLatest} from './publish.js'
import {build} from './build.js'

export const run = async ({cwd = process.cwd(), env = process.env, flags = {}} = {}) => {
console.log('Run zx bulk release')
const {packages, queue} = await topo({cwd})
const dryRun = flags['dry-run'] || flags.dryRun

Expand Down
44 changes: 41 additions & 3 deletions src/test/js/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as assert from 'uvu/assert'
import {ctx, tempy, fs} from 'zx-extra'
import {run} from '../../main/js/index.js'
import {formatTag} from '../../main/js/tag.js'
import {createFakeRepo, createNpmRegistry} from './test-utils.js'
import {addCommits, createFakeRepo, createNpmRegistry} from './test-utils.js'

const test = suite('integration')

Expand Down Expand Up @@ -37,7 +37,8 @@ const cwd = await createFakeRepo({
build: 'echo "building a"'
},
release: {
build: true
build: true,
fetch: true
}
}
}
Expand Down Expand Up @@ -80,6 +81,10 @@ const cwd = await createFakeRepo({
name: 'b',
dependencies: {
a: '1.0.0'
},
release: {
build: true,
fetch: true
}
}
}
Expand Down Expand Up @@ -116,7 +121,6 @@ test('run()', async () => {

assert.is(digestA['dist-tags'].latest, '1.0.1')
assert.is(digestA.dist.tarball, 'http://localhost:4873/a/-/a-1.0.1.tgz')

assert.is(digestB['dist-tags'].latest, '1.0.0')
assert.is(digestB.dist.tarball, 'http://localhost:4873/b/-/b-1.0.0.tgz')

Expand All @@ -130,6 +134,40 @@ test('run()', async () => {
assert.is((await fs.readJson(`${meta}/${tag.toLowerCase().replace(/[^a-z0-9-]/g, '-')}.json`)).version, '1.0.1')
})

await addCommits({cwd, commits: [
{
msg: 'feat(b): add feat',
files: [
{
relpath: './packages/b/feat.txt',
contents: 'new feat'
}
]
}
]})

await run({cwd})

await ctx(async ($) => {
$.cwd = cwd
const digestA = JSON.parse((await $`npm view a --registry=${registry.address} --json`).toString())
const digestB = JSON.parse((await $`npm view b --registry=${registry.address} --json`).toString())

assert.is(digestA['dist-tags'].latest, '1.0.1')
assert.is(digestA.dist.tarball, 'http://localhost:4873/a/-/a-1.0.1.tgz')
assert.is(digestB['dist-tags'].latest, '1.1.0')
assert.is(digestB.dist.tarball, 'http://localhost:4873/b/-/b-1.1.0.tgz')

const tag = formatTag({name: 'b', version: '1.1.0'})
assert.is((await $`git for-each-ref refs/tags/${tag} --format='%(contents)'`).toString().trim(), tag)

const origin = (await $`git remote get-url origin`).toString().trim()
const meta = tempy.temporaryDirectory()

await $`git clone --single-branch --branch meta --depth 1 ${origin} ${meta}`
assert.is((await fs.readJson(`${meta}/${tag.toLowerCase().replace(/[^a-z0-9-]/g, '-')}.json`)).version, '1.1.0')
})

await registry.stop()
})

Expand Down
37 changes: 23 additions & 14 deletions src/test/js/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,33 @@ export const createFakeRepo = async ({cwd = tempy.temporaryDirectory(), commits
$.cwd = cwd
await $`git init`

for (let {msg, files, name = 'Semrel-extra Bot', email = '[email protected]', tags = []} of commits) {
await $`git config user.name ${name}`
await $`git config user.email ${email}`
for (let {relpath, contents} of files) {
const _contents = typeof contents === 'string' ? contents : JSON.stringify(contents, null, 2)
await fs.outputFile(path.resolve(cwd, relpath), _contents)
}
await $`git add .`
await $`git commit -m ${msg}`

for (let tag of tags) {
await $`git tag ${tag} -m ${tag}`
}
}
await addCommits({cwd, commits})

const bare = tempy.temporaryDirectory()
await $`git init --bare ${bare}`
await $`git remote add origin ${bare}`

return cwd
})

export const addCommits = async ({cwd, commits = []}) => ctx(async ($) => {
$.cwd = cwd

for (let {msg, files, name = 'Semrel-extra Bot', email = '[email protected]', tags = []} of commits) {
await $`git config user.name ${name}`
await $`git config user.email ${email}`
for (let {relpath, contents} of files) {
const _contents = typeof contents === 'string' ? contents : JSON.stringify(contents, null, 2)
const file = path.resolve(cwd, relpath)
await fs.outputFile(file, _contents)

await $`git add ${file}`
}

await $`git commit -m ${msg}`

for (let tag of tags) {
await $`git tag ${tag} -m ${tag}`
}
}
})
13 changes: 9 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1174,10 +1174,10 @@ getpass@^0.1.1:
dependencies:
assert-plus "^1.0.0"

git-glob-cp@^1.6.2:
version "1.6.2"
resolved "https://registry.yarnpkg.com/git-glob-cp/-/git-glob-cp-1.6.2.tgz#1cf9831b2377cdca21007fa0ea8183896ac231b1"
integrity sha512-XUgdmBDHC6v5m3mt9LB5prldOYLMZG5tPRacxIN256xhFh6O3ltGebWmpad9Julm7LAawI+sTa/FEUte7Hq0sg==
git-glob-cp@^1.7.1:
version "1.7.1"
resolved "https://registry.yarnpkg.com/git-glob-cp/-/git-glob-cp-1.7.1.tgz#9019b574e8e5908bed5af46e7efcf07b0a3dfd3b"
integrity sha512-UmcMdJMOzGa/mV7zdU2wHjZ3Yz0SWYyAmEZ+Z3K4mA+DHFWd+rBgOid4vGQ/jbSjHKodLx8BMxOHawdUrlYMCw==
dependencies:
tar "^6.1.11"
zx-extra "^1.7.0"
Expand Down Expand Up @@ -1380,6 +1380,11 @@ inherits@2, [email protected]:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==

ini@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ini/-/ini-3.0.0.tgz#2f6de95006923aa75feed8894f5686165adc08f1"
integrity sha512-TxYQaeNW/N8ymDvwAxPyRbhMBtnEwuvaTYpOQkFx1nSeusgezHniEc/l35Vo4iCq/mMiTJbpD7oYxN98hFlfmw==

ip-regex@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-2.1.0.tgz#fa78bf5d2e6913c911ce9f819ee5146bb6d844e9"
Expand Down

0 comments on commit 4c59a0e

Please sign in to comment.