#!/usr/bin/env node // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // A very simple script to verify that a package can be installed and imported. // Intended primarily as a basic smoke test after publishing. // TODO-RS: Work out how to run this against verdaccio for pre-release verification too! const { execSync, spawn } = require('child_process') const tmp = require('tmp') const fs = require('fs'); const pipeStdIo = { stdio: [process.stdin, process.stdout, process.stderr] } const localVerdaccioRegistry = 'http://localhost:4873/' const npmPublicRegistry = 'https://registry.npmjs.org/' const mode = process.argv[2] if (!['ci', 'public'].includes(mode)) throw new Error(`Unknown mode ${mode}`) const npmRegistry = process.argv[2] === 'ci' ? localVerdaccioRegistry : npmPublicRegistry const { name } = JSON.parse(fs.readFileSync("package.json", 'utf8')) // Create a temporary directory to act as a dummy consuming project. // It's important not to use the current directory because npm will // walk the file tree upwards and potentially find unrelated node_modules directories! const consumingPackageDir = tmp.dirSync(); // I am assuming that you used `local_verdaccio_publish` // This is to facilitate interacting with a local verdaccio server // So... I need a running server const verdaccio = spawn('npx', ['verdaccio', '-c', '../../verdaccio/config.yaml'], pipeStdIo) .on('error', e => { throw e }) // just in case process.on('exit', () => { if (!verdaccio.killed) verdaccio.kill() }) // Verify the version information const version = execSync(`npm info ${name} dist-tags.latest --registry ${npmRegistry}`).toString().trim() if (localVerdaccioRegistry === npmRegistry && !version.includes('ci')) { throw new Error('Latest version in Verdaccio is not a ci release.') } else if (localVerdaccioRegistry !== npmRegistry && version.includes('ci')) { throw new Error('CI release pushed publicly!') } console.log(`\nVerifying ${name}@${version} release...`) //Init dir in order to have package.json in dummy dir execSync(`npm init --yes`, {cwd: consumingPackageDir.name}) // Manually install the dependency execSync(`npm install ${name} --registry ${npmRegistry}`, {cwd: consumingPackageDir.name}) // Kill the background verdaccio server verdaccio.kill() // Verify that it can be imported. This also ensures that types are available correctly. const out = execSync(`node -e "console.log(require('${name}'))"`, {cwd: consumingPackageDir.name}).toString() console.log(`SUCCESS: Verified ${name} release: ${out}`)