Skip to content

Commit

Permalink
feat: use npm pkg registry for meta fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jun 28, 2022
1 parent 869e3d3 commit ee771cb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const run = async ({cwd = process.cwd(), env = process.env, flags = {}} =
for (let name of queue) {
const pkg = packages[name]
pkg.config = await getConfig(pkg.absPath, root.absPath)
pkg.latest = await getLatest(cwd, name)
pkg.latest = await getLatest(pkg)

const semanticChanges = await getSemanticChanges(pkg.absPath, pkg.latest.tag?.ref)
const depsChanges = await updateDeps(pkg, packages)
Expand Down
36 changes: 28 additions & 8 deletions src/main/js/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ import {$, ctx, fs, path, tempy} from 'zx-extra'
import ini from 'ini'
import {copy} from 'git-glob-cp'

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

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

pkg.fetched = true
Expand All @@ -24,7 +21,21 @@ export const fetchPkg = async (pkg) => {
}
}

export const fetchManifest = async (pkg) => {}
export const fetchManifest = async (pkg, {nothrow, env = $.env} = {}) => {
const {npmRegistry, npmToken, npmConfig} = parseEnv(env)
const bearerToken = getBearerToken(npmRegistry, npmToken, npmConfig)
const url = getManifestUrl(npmRegistry, pkg.name, pkg.version)

try {
const res = await fetch(url, {authorization: bearerToken})
if (!res.ok) throw res

return res.json() // NOTE .json() is async too
} catch (e) {
if (nothrow) return null
throw e
}
}

export const npmPublish = (pkg) => ctx(async ($) => {
const {absPath: cwd, name, version} = pkg
Expand All @@ -45,3 +56,12 @@ export const getAuthToken = (registry, npmrc) =>

// $`npm view ${name}@${version} dist.tarball`
export const getTarballUrl = (registry, name, version) => `${registry}/${name}/-/${name.replace(/^.+(%2f|\/)/,'')}-${version}.tgz`

export const getManifestUrl = (registry, name, version) => `${registry}/${name}/${version}`

export const getBearerToken = async (npmRegistry, npmToken, npmConfig) => {
const token = npmConfig
? getAuthToken(npmRegistry, ini.parse(await fs.readFile(npmConfig, 'utf8')))
: npmToken
return `Bearer ${token}`
}
7 changes: 4 additions & 3 deletions src/main/js/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {formatTag, getLatestTag} from './tag.js'
import {ctx, fs, path, $} from 'zx-extra'
import {push, fetch, parseRepo} from './repo.js'
import {parseEnv} from './config.js'
import {npmPublish} from './npm.js'
import {fetchManifest, npmPublish} from './npm.js'

export const publish = async (pkg) => {
await pushTag(pkg)
Expand Down Expand Up @@ -142,9 +142,10 @@ export const getLatestMeta = async (cwd, tag) => {
return null
}

export const getLatest = async (cwd, name) => {
export const getLatest = async (pkg) => {
const {absPath: cwd, name} = pkg
const tag = await getLatestTag(cwd, name)
const meta = await getLatestMeta(cwd, tag?.ref)
const meta = await getLatestMeta(cwd, tag?.ref) || await fetchManifest(pkg, {nothrow: true})

return {
tag,
Expand Down
2 changes: 1 addition & 1 deletion src/main/js/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const fetch = async ({cwd: _cwd, branch, origin: _origin}) => ctx(async (
try {
await $`git clone --single-branch --branch ${branch} --depth 1 ${origin} .`
} catch (e) {
console.warn(`ref ${branch} does not exist in ${origin}`)
console.warn(`ref '${branch}' does not exist in ${origin}`)
await $`git init .`
await $`git remote add origin ${origin}`
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/js/npm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {suite} from 'uvu'
import * as assert from 'uvu/assert'
import {fetchManifest} from '../../main/js/npm.js'

const test = suite('npm')

test('fetchManifest()', async () => {
const env = {NPM_REGISTRY: 'https://registry.npmjs.org'}
const manifest = await fetchManifest({
name: 'ggcp',
version: '1.5.0'
}, {env})
assert.ok(manifest.name === 'ggcp')

const notfound = await fetchManifest({
name: '@qiwi/sf2332f-32ds2213-dfs23',
version: '999.15.0'
}, {nothrow: true, env})
assert.is(notfound, null)
})

test.run()
3 changes: 1 addition & 2 deletions src/test/js/tag.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {suite} from 'uvu'
import * as assert from 'uvu/assert'

import {parseTag, formatTag, getTags} from '../../main/js/tag.js'
import {parseTag, formatTag, getTags, getLatestTaggedVersion} from '../../main/js/tag.js'
import {createFakeRepo} from './test-utils.js'
import {semver} from 'zx-extra'
import {getLatestTaggedVersion} from "../../main/js/tag.js";

const test = suite('tag')

Expand Down

0 comments on commit ee771cb

Please sign in to comment.