|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +const assert = require('node:assert/strict'); |
| 5 | +const {execFile, execFileSync} = require('node:child_process'); |
| 6 | +const {once} = require('node:events'); |
| 7 | +const {promises: fs} = require('node:fs'); |
| 8 | +const http = require('node:http'); |
| 9 | +const os = require('node:os'); |
| 10 | +const path = require('node:path'); |
| 11 | +const {test} = require('node:test'); |
| 12 | +const {promisify} = require('node:util'); |
| 13 | + |
| 14 | +const run = promisify(execFile); |
| 15 | +const repoRoot = path.resolve(__dirname, '..', '..'); |
| 16 | + |
| 17 | +for (const name of ['@rnw-tests/registry-fixture', 'rnw-registry-fixture']) { |
| 18 | + test(`Windows CLI-init prunes published ${name} versions using the publish registry, not the default proxy`, async t => { |
| 19 | + if (process.platform !== 'win32') { |
| 20 | + t.skip('CLI-init uses the Windows Node/npm installation layout'); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const template = await fs.readFile( |
| 25 | + path.join(repoRoot, '.ado', 'templates', 'verdaccio-start.yml'), |
| 26 | + 'utf8', |
| 27 | + ); |
| 28 | + const command = template.match( |
| 29 | + /^\s*- script: node (\.ado\/scripts\/npmPack\.js[^\r\n]*--check-npm[^\r\n]*)$/m, |
| 30 | + ); |
| 31 | + assert.ok(command, 'CLI-init must check already published packages'); |
| 32 | + const [script, ...args] = command[1].split(/\s+/); |
| 33 | + const publishRegistry = template.match( |
| 34 | + /npm publish "[^"]+" --registry (\S+)/, |
| 35 | + )?.[1]; |
| 36 | + assert.ok(publishRegistry, 'CLI-init must specify its publish registry'); |
| 37 | + |
| 38 | + const root = await fs.mkdtemp( |
| 39 | + path.join(os.tmpdir(), 'rnw-verdaccio-pruning-'), |
| 40 | + ); |
| 41 | + t.after(() => fs.rm(root, {recursive: true, force: true})); |
| 42 | + const packages = path.join(root, 'packages'); |
| 43 | + const userConfig = path.join(root, '.npmrc'); |
| 44 | + const globalConfig = path.join(root, '.global-npmrc'); |
| 45 | + await Promise.all([ |
| 46 | + fs.mkdir(packages), |
| 47 | + fs.mkdir(path.join(root, 'package')), |
| 48 | + fs.writeFile(userConfig, ''), |
| 49 | + fs.writeFile(globalConfig, ''), |
| 50 | + ]); |
| 51 | + for (const version of ['1.0.0', '1.0.1']) { |
| 52 | + await fs.writeFile( |
| 53 | + path.join(root, 'package', 'package.json'), |
| 54 | + JSON.stringify({name, version}), |
| 55 | + ); |
| 56 | + execFileSync('tar', [ |
| 57 | + '-czf', |
| 58 | + path.join(packages, `${version}.tgz`), |
| 59 | + '-C', |
| 60 | + root, |
| 61 | + 'package', |
| 62 | + ]); |
| 63 | + } |
| 64 | + |
| 65 | + const requests = []; |
| 66 | + const server = http.createServer((request, response) => { |
| 67 | + const pathname = decodeURIComponent( |
| 68 | + new URL(request.url, 'http://localhost').pathname, |
| 69 | + ); |
| 70 | + requests.push(pathname); |
| 71 | + const isPublishRegistry = pathname === `/verdaccio/${name}`; |
| 72 | + response.writeHead(isPublishRegistry ? 200 : 404, { |
| 73 | + 'Content-Type': 'application/json', |
| 74 | + }); |
| 75 | + response.end( |
| 76 | + JSON.stringify( |
| 77 | + isPublishRegistry |
| 78 | + ? { |
| 79 | + name, |
| 80 | + 'dist-tags': {latest: '1.0.0'}, |
| 81 | + versions: {'1.0.0': {name, version: '1.0.0'}}, |
| 82 | + } |
| 83 | + : {error: 'Not found'}, |
| 84 | + ), |
| 85 | + ); |
| 86 | + }); |
| 87 | + server.listen(0, '127.0.0.1'); |
| 88 | + await once(server, 'listening'); |
| 89 | + t.after(() => promisify(server.close).call(server)); |
| 90 | + const address = server.address(); |
| 91 | + assert.ok(address && typeof address !== 'string'); |
| 92 | + const baseUrl = `http://127.0.0.1:${address.port}`; |
| 93 | + const registryIndex = args.indexOf('--registry'); |
| 94 | + if (registryIndex !== -1) { |
| 95 | + assert.equal(args[registryIndex + 1], publishRegistry); |
| 96 | + args[registryIndex + 1] = `${baseUrl}/verdaccio/`; |
| 97 | + } |
| 98 | + |
| 99 | + await run(process.execPath, [path.join(repoRoot, script), ...args, packages], { |
| 100 | + cwd: root, |
| 101 | + timeout: 60000, |
| 102 | + env: { |
| 103 | + ...process.env, |
| 104 | + NPM_CONFIG_REGISTRY: `${baseUrl}/proxy/`, |
| 105 | + NPM_CONFIG_USERCONFIG: userConfig, |
| 106 | + NPM_CONFIG_GLOBALCONFIG: globalConfig, |
| 107 | + NPM_CONFIG_CACHE: path.join(root, 'cache'), |
| 108 | + NPM_CONFIG_FETCH_RETRIES: '0', |
| 109 | + NPM_CONFIG_FETCH_TIMEOUT: '2000', |
| 110 | + }, |
| 111 | + }); |
| 112 | + |
| 113 | + assert.deepEqual(await fs.readdir(packages), ['1.0.1.tgz']); |
| 114 | + assert.ok(requests.includes(`/verdaccio/${name}`)); |
| 115 | + assert.ok(!requests.some(request => request.startsWith('/proxy/'))); |
| 116 | + }); |
| 117 | +} |
0 commit comments