Skip to content

Commit

Permalink
chore: fix api
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Mar 9, 2023
1 parent fd463b8 commit 260f92f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
"tomlify-j0.4": "^3.0.0",
"tree-kill": "^1.2.2",
"typescript": "^4.4.4",
"verdaccio": "^5.10.2",
"verdaccio": "^5.22.1",
"vite": "^4.0.0",
"vitest": "^0.29.0"
},
Expand Down
53 changes: 33 additions & 20 deletions tools/e2e/setup.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import { appendFile, mkdtemp, readFile, rm, writeFile } from 'fs/promises'
import { tmpdir } from 'os'
import { join, sep } from 'path'
import { join, normalize, sep } from 'path'
import { cwd, env } from 'process'
import { fileURLToPath } from 'url'

import execa from 'execa'
import getPort from 'get-port'
import verdaccio from 'verdaccio'
import pTimeout from 'p-timeout'
import { runServer } from 'verdaccio'

import { fileExistsAsync } from '../../src/lib/fs.mjs'

const dir = fileURLToPath(import.meta.url)

const VERDACCIO_TIMEOUT_MILLISECONDS = 60 * 1000
const START_PORT_RANGE = 5000
const END_PORT_RANGE = 5000

/**
* Gets the verdaccio configuration
* @param {string} storage The location where the artifacts are stored
*/
const getVerdaccioConfig = (storage) => ({
storage,
const getVerdaccioConfig = () => ({
// workaround
// on v5 the `self_path` still exists and will be removed in v6 of verdaccio
self_path: dir,
storage: normalize(join(dir, '../../.verdaccio-storage')),
web: { title: 'Test Registry' },
max_body_size: '128mb',
// Disable creation of users this is only meant for integration testing
Expand Down Expand Up @@ -51,31 +56,39 @@ const getVerdaccioConfig = (storage) => ({
},
})

/**
* Start verdaccio server
* @returns {Promise<{ url: URL; storage: string; }>}
*/
const runVerdaccio = async (config, port) => {
const app = await runServer(config)

return new Promise((resolve, reject) => {
app.listen(port, 'localhost', () => {
resolve({ url: new URL(`http://localhost:${port}/`), storage: config.storage })
})
app.on('error', (error) => {
reject(error)
})
})
}

/**
* Start verdaccio registry and store artifacts in a new temporary folder on the os
* @returns {Promise<{ url: URL; storage: string; }>}
*/
export const startRegistry = async () => {
const config = getVerdaccioConfig()

// Remove netlify-cli from the verdaccio storage because we are going to publish it in a second
await rm(join(config.storage, 'netlify-cli'), { force: true, recursive: true })

// generate a random starting port to avoid race condition inside the promise when running a large
// number in parallel
const startPort = Math.floor(Math.random() * END_PORT_RANGE) + START_PORT_RANGE
const freePort = await getPort({ host: 'localhost', port: startPort })
const storage = fileURLToPath(new URL('../../.verdaccio-storage', import.meta.url))

// Remove netlify-cli from the verdaccio storage because we are going to publish it in a second
await rm(join(storage, 'netlify-cli'), { force: true, recursive: true })

return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Starting Verdaccio Timed out'))
}, VERDACCIO_TIMEOUT_MILLISECONDS)

verdaccio.default(getVerdaccioConfig(storage), freePort, storage, '1.0.0', 'verdaccio', (webServer, { port }) => {
webServer.listen(port, 'localhost', () => {
resolve({ url: new URL(`http://localhost:${port}/`), storage })
})
})
})
return await pTimeout(runVerdaccio(config, freePort), VERDACCIO_TIMEOUT_MILLISECONDS, 'Starting Verdaccio timed out')
}

/**
Expand Down

0 comments on commit 260f92f

Please sign in to comment.