2023-01-04 20:03:47 +01:00
|
|
|
/**
|
|
|
|
* Utility functions for the compatibility tests
|
|
|
|
*/
|
|
|
|
|
2023-08-06 09:53:32 +02:00
|
|
|
import * as prettier from 'prettier'
|
|
|
|
import * as path from 'path'
|
|
|
|
import {globby, argv, YAML, $, fs, cd, os} from '@cspotcode/zx'
|
2023-01-04 20:03:47 +01:00
|
|
|
import onCleanup from 'node-cleanup'
|
2023-08-06 09:53:32 +02:00
|
|
|
import startVerdaccioServer from 'verdaccio'
|
|
|
|
import {defer} from '../utils/testUtils'
|
2023-01-04 20:03:47 +01:00
|
|
|
|
2023-01-22 18:51:55 +01:00
|
|
|
/**
|
|
|
|
* @param {string} pkg
|
|
|
|
* @returns boolean
|
|
|
|
*/
|
|
|
|
const isTheatreDependency = (pkg) =>
|
|
|
|
pkg.startsWith('@theatre/') || pkg === 'theatric'
|
|
|
|
|
2023-01-12 13:43:44 +01:00
|
|
|
const verbose = !!argv['verbose']
|
|
|
|
|
|
|
|
if (!verbose) {
|
2023-01-04 20:03:47 +01:00
|
|
|
$.verbose = false
|
|
|
|
console.log(
|
|
|
|
'Running in quiet mode. Add --verbose to see the output of all commands.',
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
VERDACCIO_PORT: 4823,
|
|
|
|
VERDACCIO_HOST: `localhost`,
|
|
|
|
get VERDACCIO_URL() {
|
|
|
|
return `http://${config.VERDACCIO_HOST}:${config.VERDACCIO_PORT}/`
|
|
|
|
},
|
|
|
|
PATH_TO_COMPAT_TESTS_ROOT: path.join(__dirname, '..'),
|
|
|
|
MONOREPO_ROOT: path.join(__dirname, '../..'),
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set environment variables so that yarn and npm use verdaccio as the registry.
|
|
|
|
* These are only set for the current process.
|
|
|
|
*/
|
|
|
|
process.env.YARN_NPM_PUBLISH_REGISTRY = config.VERDACCIO_URL
|
|
|
|
process.env.YARN_UNSAFE_HTTP_WHITELIST = config.VERDACCIO_HOST
|
|
|
|
process.env.YARN_NPM_AUTH_IDENT = 'test:test'
|
|
|
|
process.env.NPM_CONFIG_REGISTRY = config.VERDACCIO_URL
|
|
|
|
|
|
|
|
const tempVersion =
|
|
|
|
'0.0.1-COMPAT.' +
|
2023-08-01 13:55:25 +02:00
|
|
|
(typeof argv['version'] === 'number'
|
|
|
|
? argv['version'].toString()
|
|
|
|
: // a random integer between 1 and 50000
|
|
|
|
(Math.floor(Math.random() * 50000) + 1).toString())
|
2023-01-04 20:03:47 +01:00
|
|
|
|
2023-08-01 15:07:19 +02:00
|
|
|
const keepAlive = !!argv['keep-alive']
|
|
|
|
|
2023-01-04 20:03:47 +01:00
|
|
|
/**
|
|
|
|
* This script starts verdaccio and publishes all the packages in the monorepo to it, then
|
|
|
|
* it runs `npm install` on all the test packages, and finally it closes verdaccio.
|
|
|
|
*/
|
2023-08-06 09:53:32 +02:00
|
|
|
export async function installFixtures(): Promise<void> {
|
2023-01-04 20:03:47 +01:00
|
|
|
onCleanup((exitCode, signal) => {
|
|
|
|
onCleanup.uninstall()
|
|
|
|
restoreTestPackageJsons()
|
|
|
|
process.kill(process.pid, signal)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2023-08-01 13:55:25 +02:00
|
|
|
console.log(
|
|
|
|
`Using temporary version: ${tempVersion} . Use --version=[NUMBER] to change.`,
|
|
|
|
)
|
2023-01-04 20:03:47 +01:00
|
|
|
console.log('Patching package.json files in ./test-*')
|
|
|
|
const restoreTestPackageJsons = await patchTestPackageJsons()
|
|
|
|
|
|
|
|
console.log('Starting verdaccio')
|
|
|
|
const verdaccioServer = await startVerdaccio(config.VERDACCIO_PORT)
|
|
|
|
console.log(`Verdaccio is running on ${config.VERDACCIO_URL}`)
|
|
|
|
|
|
|
|
console.log('Releasing @theatre/* packages to verdaccio')
|
|
|
|
await releaseToVerdaccio()
|
|
|
|
|
|
|
|
console.log('Running `$ npm install` on test packages')
|
|
|
|
await runNpmInstallOnTestPackages()
|
2023-08-06 08:43:45 +02:00
|
|
|
|
|
|
|
restoreTestPackageJsons()
|
|
|
|
|
2023-08-01 15:07:19 +02:00
|
|
|
if (keepAlive) {
|
|
|
|
console.log('Keeping verdaccio alive. Press Ctrl+C to exit.')
|
|
|
|
// wait for ctrl+c
|
|
|
|
await new Promise((resolve) => {})
|
|
|
|
} else {
|
|
|
|
console.log('Closing verdaccio. Use --keep-alive to keep it running.')
|
|
|
|
await verdaccioServer.close()
|
|
|
|
}
|
2023-01-04 20:03:47 +01:00
|
|
|
console.log('Done')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runNpmInstallOnTestPackages() {
|
|
|
|
const packagePaths = await getCompatibilityTestSetups()
|
|
|
|
|
2023-08-06 08:43:45 +02:00
|
|
|
const promises = packagePaths.map(async (pathToPackageDir) => {
|
2023-08-01 15:07:19 +02:00
|
|
|
await fs.remove(path.join(pathToPackageDir, 'node_modules'))
|
|
|
|
await fs.remove(path.join(pathToPackageDir, 'package-lock.json'))
|
|
|
|
cd(path.join(pathToPackageDir, '../'))
|
|
|
|
const tempPath = fs.mkdtempSync(
|
|
|
|
path.join(os.tmpdir(), 'theatre-compat-test-'),
|
|
|
|
)
|
|
|
|
await fs.copy(pathToPackageDir, tempPath)
|
|
|
|
|
|
|
|
cd(path.join(tempPath))
|
2023-01-04 20:03:47 +01:00
|
|
|
try {
|
|
|
|
console.log('Running npm install on ' + pathToPackageDir + '...')
|
2023-01-12 13:43:44 +01:00
|
|
|
await $`npm install --registry ${config.VERDACCIO_URL} --loglevel ${
|
|
|
|
verbose ? 'warn' : 'error'
|
|
|
|
} --fund false`
|
2023-08-01 15:07:19 +02:00
|
|
|
|
|
|
|
console.log('npm install finished successfully in' + tempPath)
|
|
|
|
|
|
|
|
await fs.move(
|
|
|
|
path.join(tempPath, 'node_modules'),
|
|
|
|
path.join(pathToPackageDir, 'node_modules'),
|
|
|
|
)
|
|
|
|
await fs.move(
|
|
|
|
path.join(tempPath, 'package-lock.json'),
|
|
|
|
path.join(pathToPackageDir, 'package-lock.json'),
|
|
|
|
)
|
2023-01-04 20:03:47 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error(`Failed to install dependencies for ${pathToPackageDir}
|
|
|
|
Try running \`npm install\` in that directory manually via:
|
|
|
|
cd ${pathToPackageDir}
|
|
|
|
npm install --registry ${config.VERDACCIO_URL}
|
|
|
|
Original error: ${error}`)
|
2023-08-06 08:43:45 +02:00
|
|
|
throw new Error(`Failed to install dependencies for ${pathToPackageDir}`)
|
2023-08-01 15:07:19 +02:00
|
|
|
} finally {
|
|
|
|
await fs.remove(tempPath)
|
2023-01-04 20:03:47 +01:00
|
|
|
}
|
2023-08-06 08:43:45 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const result = await Promise.allSettled(promises)
|
|
|
|
const failed = result.filter((x) => x.status === 'rejected')
|
|
|
|
if (failed.length > 0) {
|
|
|
|
console.error(
|
|
|
|
`Failed to install dependencies for the following packages:`,
|
|
|
|
result
|
|
|
|
.map((result, i) => [packagePaths[i], result])
|
|
|
|
.filter(
|
|
|
|
([p, result]) =>
|
|
|
|
// @ts-ignore
|
|
|
|
result.status === 'rejected',
|
|
|
|
)
|
|
|
|
.map(([path]) => path),
|
|
|
|
)
|
2023-01-04 20:03:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes an absolute path to a package.json file and replaces all of its
|
|
|
|
* dependencies on `@theatre/*` packatges to `version`.
|
|
|
|
*
|
|
|
|
* @param {string} pathToPackageJson absolute path to the package.json file
|
|
|
|
* @param {string} version The version to set all `@theatre/*` dependencies to
|
|
|
|
*/
|
|
|
|
async function patchTheatreDependencies(pathToPackageJson, version) {
|
|
|
|
const originalFileContent = fs.readFileSync(pathToPackageJson, {
|
|
|
|
encoding: 'utf-8',
|
|
|
|
})
|
|
|
|
// get the package.json file's content
|
|
|
|
const packageJson = JSON.parse(originalFileContent)
|
|
|
|
|
|
|
|
// find all dependencies on '@theatre/*' packages and replace them with the local version
|
|
|
|
for (const dependencyType of [
|
|
|
|
'dependencies',
|
|
|
|
'devDependencies',
|
|
|
|
'peerDependencies',
|
|
|
|
]) {
|
|
|
|
const dependencies = packageJson[dependencyType]
|
|
|
|
if (dependencies) {
|
|
|
|
for (const dependencyName of Object.keys(dependencies)) {
|
2023-01-22 18:51:55 +01:00
|
|
|
if (isTheatreDependency(dependencyName)) {
|
2023-01-04 20:03:47 +01:00
|
|
|
dependencies[dependencyName] = version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// run the json through prettier
|
|
|
|
const jsonStringPrettified = prettier.format(
|
|
|
|
JSON.stringify(packageJson, null, 2),
|
|
|
|
{
|
|
|
|
parser: 'json',
|
|
|
|
filepath: pathToPackageJson,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// write the modified package.json file
|
|
|
|
fs.writeFileSync(pathToPackageJson, jsonStringPrettified, {encoding: 'utf-8'})
|
|
|
|
}
|
|
|
|
|
2023-08-06 09:53:32 +02:00
|
|
|
async function patchTestPackageJsons(): Promise<() => void> {
|
2023-01-04 20:03:47 +01:00
|
|
|
const packagePaths = (await getCompatibilityTestSetups()).map(
|
|
|
|
(pathToPackageDir) => path.join(pathToPackageDir, 'package.json'),
|
|
|
|
)
|
|
|
|
|
|
|
|
// replace all dependencies on @theatre/* packages with the local version
|
|
|
|
for (const pathToPackageJson of packagePaths) {
|
|
|
|
patchTheatreDependencies(pathToPackageJson, tempVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
// replace all dependencies on @theatre/* packages with the 0.0.1-COMPAT.1
|
|
|
|
for (const pathToPackageJson of packagePaths) {
|
|
|
|
patchTheatreDependencies(pathToPackageJson, '0.0.1-COMPAT.1')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the verdaccio server and returns a promise that resolves when the serve is up and ready
|
|
|
|
*
|
|
|
|
* Credit: https://github.com/storybookjs/storybook/blob/92b23c080d03433765cbc7a60553d036a612a501/scripts/run-registry.ts
|
|
|
|
*/
|
2023-08-06 09:53:32 +02:00
|
|
|
async function startVerdaccio(port: number): Promise<{close: () => void}> {
|
2023-01-04 20:03:47 +01:00
|
|
|
let resolved = false
|
|
|
|
|
2023-08-06 09:53:32 +02:00
|
|
|
const deferred = defer<{close: () => void}>()
|
2023-01-12 13:43:44 +01:00
|
|
|
|
2023-08-06 09:53:32 +02:00
|
|
|
const config = {
|
|
|
|
...YAML.parse(
|
|
|
|
fs.readFileSync(path.join(__dirname, '../verdaccio.yml'), 'utf8'),
|
|
|
|
),
|
|
|
|
}
|
2023-08-01 15:07:19 +02:00
|
|
|
|
2023-08-06 09:53:32 +02:00
|
|
|
if (verbose) {
|
|
|
|
config.logs.level = 'warn'
|
|
|
|
}
|
2023-08-01 15:07:19 +02:00
|
|
|
|
2023-08-06 09:53:32 +02:00
|
|
|
const cache = path.join(__dirname, '../.verdaccio-cache')
|
|
|
|
|
|
|
|
config.self_path = cache
|
|
|
|
|
|
|
|
startVerdaccioServer(
|
|
|
|
config,
|
|
|
|
'6000',
|
|
|
|
cache,
|
|
|
|
'1.0.0',
|
|
|
|
'verdaccio',
|
|
|
|
(webServer) => {
|
|
|
|
webServer.listen(port, () => {
|
|
|
|
resolved = true
|
|
|
|
deferred.resolve(webServer)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
2023-01-04 20:03:47 +01:00
|
|
|
|
2023-08-06 09:53:32 +02:00
|
|
|
await Promise.race([
|
|
|
|
deferred.promise,
|
2023-01-04 20:03:47 +01:00
|
|
|
new Promise((_, rej) => {
|
|
|
|
setTimeout(() => {
|
2023-08-06 09:53:32 +02:00
|
|
|
rej(new Error(`TIMEOUT - verdaccio didn't start within 10s`))
|
2023-01-04 20:03:47 +01:00
|
|
|
}, 10000)
|
|
|
|
}),
|
|
|
|
])
|
2023-08-06 09:53:32 +02:00
|
|
|
|
|
|
|
return deferred.promise
|
2023-01-04 20:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const packagesToPublish = [
|
|
|
|
'@theatre/core',
|
|
|
|
'@theatre/studio',
|
|
|
|
'@theatre/dataverse',
|
|
|
|
'@theatre/react',
|
|
|
|
'@theatre/browser-bundles',
|
|
|
|
'@theatre/r3f',
|
2023-01-22 18:51:55 +01:00
|
|
|
'theatric',
|
2023-01-04 20:03:47 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns a new version to each of @theatre/* packages. If there a package depends on another package in this monorepo,
|
|
|
|
* this function makes sure the dependency version is fixed at "version"
|
|
|
|
*
|
2023-08-06 09:53:32 +02:00
|
|
|
* @param workspacesListObjects - An Array of objects containing information about the workspaces
|
|
|
|
* @param version - Version of the latest commit (or any other string)
|
|
|
|
* @returns - An async function that restores the package.json files to their original version
|
2023-01-04 20:03:47 +01:00
|
|
|
*/
|
2023-08-06 09:53:32 +02:00
|
|
|
async function writeVersionsToPackageJSONs(
|
|
|
|
workspacesListObjects: Array<{name: string; location: string}>,
|
|
|
|
version: string,
|
|
|
|
): Promise<() => void> {
|
2023-01-04 20:03:47 +01:00
|
|
|
/**
|
|
|
|
* An array of functions each of which restores a certain package.json to its original state
|
|
|
|
* @type {Array<() => void>}
|
|
|
|
*/
|
|
|
|
const restores = []
|
|
|
|
for (const workspaceData of workspacesListObjects) {
|
|
|
|
const pathToPackage = path.resolve(
|
|
|
|
config.MONOREPO_ROOT,
|
|
|
|
workspaceData.location,
|
|
|
|
'./package.json',
|
|
|
|
)
|
|
|
|
|
|
|
|
const originalFileContent = fs.readFileSync(pathToPackage, {
|
|
|
|
encoding: 'utf-8',
|
|
|
|
})
|
|
|
|
const originalJson = JSON.parse(originalFileContent)
|
|
|
|
|
|
|
|
restores.push(() => {
|
|
|
|
fs.writeFileSync(pathToPackage, originalFileContent, {encoding: 'utf-8'})
|
|
|
|
})
|
|
|
|
|
|
|
|
let {dependencies, peerDependencies, devDependencies} = originalJson
|
|
|
|
|
|
|
|
// Normally we don't have to override the package versions in dependencies because yarn would already convert
|
|
|
|
// all the "workspace:*" versions to a fixed version before publishing. However, packages like @theatre/studio
|
|
|
|
// have a peerDependency on @theatre/core set to "*" (meaning they would work with any version of @theatre/core).
|
|
|
|
// This is not the desired behavior in pre-release versions, so here, we'll fix those "*" versions to the set version.
|
|
|
|
for (const deps of [dependencies, peerDependencies, devDependencies]) {
|
|
|
|
if (!deps) continue
|
|
|
|
for (const wpObject of workspacesListObjects) {
|
|
|
|
if (deps[wpObject.name]) {
|
|
|
|
deps[wpObject.name] = version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const newJson = {
|
|
|
|
...originalJson,
|
|
|
|
version,
|
|
|
|
dependencies,
|
|
|
|
peerDependencies,
|
|
|
|
devDependencies,
|
|
|
|
}
|
|
|
|
fs.writeFileSync(pathToPackage, JSON.stringify(newJson, undefined, 2), {
|
|
|
|
encoding: 'utf-8',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return () =>
|
|
|
|
restores.forEach((fn) => {
|
|
|
|
fn()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds all the @theatre/* packages with version number 0.0.1-COMPAT.1 and publishes
|
|
|
|
* them all to the verdaccio registry
|
|
|
|
*/
|
|
|
|
async function releaseToVerdaccio() {
|
|
|
|
cd(config.MONOREPO_ROOT)
|
|
|
|
|
|
|
|
// @ts-ignore ignore
|
|
|
|
process.env.THEATRE_IS_PUBLISHING = true
|
|
|
|
|
|
|
|
const workspacesListString = await $`yarn workspaces list --json`
|
|
|
|
const workspacesListObjects = workspacesListString.stdout
|
|
|
|
.split(os.EOL)
|
|
|
|
// strip out empty lines
|
|
|
|
.filter(Boolean)
|
|
|
|
.map((x) => JSON.parse(x))
|
|
|
|
|
|
|
|
const restorePackages = await writeVersionsToPackageJSONs(
|
|
|
|
workspacesListObjects,
|
|
|
|
tempVersion,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Restore the package.json files to their original state when the process is killed
|
|
|
|
process.on('SIGINT', async function cleanup(a) {
|
|
|
|
restorePackages()
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
2023-08-08 20:17:05 +02:00
|
|
|
await $`yarn cli build clean`
|
2023-08-08 20:06:06 +02:00
|
|
|
await $`yarn cli build`
|
2023-01-04 20:03:47 +01:00
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
packagesToPublish.map(async (workspaceName) => {
|
|
|
|
const npmTag = 'compat'
|
|
|
|
await $`yarn workspace ${workspaceName} npm publish --access public --tag ${npmTag}`
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
} finally {
|
|
|
|
restorePackages()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-28 19:49:52 +02:00
|
|
|
* Get all the setups from `./compat-tests/`
|
2023-01-04 20:03:47 +01:00
|
|
|
*
|
2023-08-06 09:53:32 +02:00
|
|
|
* @returns An array containing the absolute paths to the compatibility test setups
|
2023-01-04 20:03:47 +01:00
|
|
|
*/
|
2023-08-06 09:53:32 +02:00
|
|
|
export async function getCompatibilityTestSetups(): Promise<Array<string>> {
|
2023-01-04 20:03:47 +01:00
|
|
|
const fixturePackageJsonFiles = await globby(
|
|
|
|
'./fixtures/*/package/package.json',
|
|
|
|
{
|
|
|
|
cwd: config.PATH_TO_COMPAT_TESTS_ROOT,
|
|
|
|
gitignore: false,
|
|
|
|
onlyFiles: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return fixturePackageJsonFiles.map((entry) => {
|
|
|
|
return path.join(config.PATH_TO_COMPAT_TESTS_ROOT, entry, '../')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes ../test-*\/(node_modules|package-lock.json|yarn.lock)
|
|
|
|
*/
|
|
|
|
export async function clean() {
|
|
|
|
const toDelete = await globby(
|
2023-08-01 13:55:25 +02:00
|
|
|
'./fixtures/*/package/(node_modules|yarn.lock|package-lock.json|.parcel-cache)',
|
2023-01-04 20:03:47 +01:00
|
|
|
{
|
|
|
|
cwd: config.PATH_TO_COMPAT_TESTS_ROOT,
|
|
|
|
// node_modules et al are gitignored, but we still want to clean them
|
|
|
|
gitignore: false,
|
|
|
|
// include directories too
|
|
|
|
onlyFiles: false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return await Promise.all(
|
|
|
|
toDelete.map((fileOrDir) => {
|
|
|
|
console.log('deleting', fileOrDir)
|
|
|
|
return fs.remove(path.join(config.PATH_TO_COMPAT_TESTS_ROOT, fileOrDir))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|