-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-plugin): add verdaccio to create package e2e
- Loading branch information
Showing
17 changed files
with
247 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ProjectConfiguration, readJson, type Tree } from '@nx/devkit'; | ||
|
||
const startLocalRegistryScript = (localRegistryTarget: string) => ` | ||
/** | ||
* This script starts a local registry for e2e testing purposes. | ||
* It is meant to be called in jest's globalSetup. | ||
*/ | ||
import { startLocalRegistry } from '@nx/js'; | ||
export default async () => { | ||
// local registry target to run | ||
const localRegistryTarget = '${localRegistryTarget}'; | ||
// storage folder for the local registry | ||
const storage = './tmp/local-registry/storage'; | ||
await startLocalRegistry({ localRegistryTarget, storage, verbose: false }); | ||
}; | ||
`; | ||
|
||
const stopLocalRegistryScript = ` | ||
/** | ||
* This script stops the local registry for e2e testing purposes. | ||
* It is meant to be called in jest's globalTeardown. | ||
*/ | ||
import { stopLocalRegistry } from '@nx/js'; | ||
export default () => { | ||
stopLocalRegistry(); | ||
}; | ||
`; | ||
|
||
export function addLocalRegistryScripts(tree: Tree) { | ||
const startLocalRegistryPath = 'tools/scripts/start-local-registry.ts'; | ||
const stopLocalRegistryPath = 'tools/scripts/stop-local-registry.ts'; | ||
|
||
const projectConfiguration: ProjectConfiguration = readJson( | ||
tree, | ||
'project.json' | ||
); | ||
const localRegistryTarget = `${projectConfiguration.name}:local-registry`; | ||
if (!tree.exists(startLocalRegistryPath)) { | ||
tree.write( | ||
startLocalRegistryPath, | ||
startLocalRegistryScript(localRegistryTarget) | ||
); | ||
} | ||
if (!tree.exists(stopLocalRegistryPath)) { | ||
tree.write(stopLocalRegistryPath, stopLocalRegistryScript); | ||
} | ||
|
||
return { startLocalRegistryPath, stopLocalRegistryPath }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { execSync, fork } from 'child_process'; | ||
|
||
/** | ||
* This function is used to start a local registry for testing purposes. | ||
* @param localRegistryTarget the target to run to start the local registry e.g. workspace:local-registry | ||
* @param storage the storage location for the local registry | ||
* @param verbose whether to log verbose output | ||
*/ | ||
export async function startLocalRegistry({ | ||
localRegistryTarget, | ||
storage, | ||
verbose, | ||
}: { | ||
localRegistryTarget: string; | ||
storage?: string; | ||
verbose?: boolean; | ||
}) { | ||
if (!localRegistryTarget) { | ||
throw new Error(`localRegistryTarget is required`); | ||
} | ||
global.localRegistryProcess = await new Promise((resolve, reject) => { | ||
const childProcess = fork( | ||
require.resolve('nx'), | ||
[ | ||
...`run ${localRegistryTarget} --location none --clear true`.split(' '), | ||
...(storage ? [`--storage`, storage] : []), | ||
], | ||
{ stdio: 'pipe' } | ||
); | ||
|
||
const listener = (data) => { | ||
if (verbose) { | ||
process.stdout.write(data); | ||
} | ||
if (data.toString().includes('http://localhost:')) { | ||
global.port = parseInt( | ||
data.toString().match(/localhost:(?<port>\d+)/)?.groups?.port | ||
); | ||
console.log('Local registry started on port ' + global.port); | ||
|
||
const registry = `http://localhost:${global.port}`; | ||
process.env.npm_config_registry = registry; | ||
process.env.YARN_REGISTRY = registry; | ||
execSync( | ||
`npm config set //localhost:${global.port}/:_authToken "secretVerdaccioToken"` | ||
); | ||
console.log('Set npm and yarn config registry to ' + registry); | ||
|
||
resolve(childProcess); | ||
childProcess?.stdout?.off('data', listener); | ||
} | ||
}; | ||
childProcess?.stdout?.on('data', listener); | ||
childProcess?.stderr?.on('data', (data) => { | ||
process.stderr.write(data); | ||
}); | ||
childProcess.on('error', (err) => { | ||
console.log('local registry error', err); | ||
reject(err); | ||
}); | ||
childProcess.on('exit', (code) => { | ||
console.log('local registry exit', code); | ||
reject(code); | ||
}); | ||
}); | ||
} | ||
|
||
export default startLocalRegistry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { execSync } from 'child_process'; | ||
|
||
export function stopLocalRegistry() { | ||
if (global.localRegistryProcess) { | ||
global.localRegistryProcess.kill(); | ||
} | ||
if (global.localRegistryPort) { | ||
execSync( | ||
`npm config delete //localhost:${global.localRegistryPort}/:_authToken` | ||
); | ||
} | ||
} | ||
|
||
export default stopLocalRegistry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.