-
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
231 additions
and
165 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/src/utils/start-local-registry'; | ||
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 }); | ||
}; | ||
`; | ||
|
||
const stopLocalRegistryScript = ` | ||
/** | ||
* This script stops the local registry for e2e testing purposes. | ||
* It is meant to be called in jest's globalTeardown. | ||
*/ | ||
import storyLocalRegistry from '@nx/js/src/utils/stop-local-registry'; | ||
export default () => { | ||
storyLocalRegistry(); | ||
}; | ||
`; | ||
|
||
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,58 @@ | ||
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 | ||
*/ | ||
export default async function startLocalRegistry({ | ||
localRegistryTarget, | ||
storage, | ||
}: { | ||
localRegistryTarget: string; | ||
storage?: string; | ||
}) { | ||
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 (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?.pipe(process.stderr); | ||
childProcess.on('error', (err) => { | ||
console.log('local registry error', err); | ||
reject(err); | ||
}); | ||
childProcess.on('exit', (code) => { | ||
console.log('local registry exit', code); | ||
reject(code); | ||
}); | ||
}); | ||
} |
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,12 @@ | ||
import { execSync } from 'child_process'; | ||
|
||
export default function stopLocalRegistry() { | ||
if (global.localRegistryProcess) { | ||
global.localRegistryProcess.kill(); | ||
} | ||
if (global.localRegistryPort) { | ||
execSync( | ||
`npm config delete //localhost:${global.localRegistryPort}/:_authToken` | ||
); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
packages/plugin/src/generators/create-package/files/e2e/__cliName__.spec.ts__tmpl__
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,39 @@ | ||
import { getPackageManagerCommand } from '@nx/devkit'; | ||
import { | ||
checkFilesExist, | ||
removeTmpProject, | ||
tmpFolder, | ||
uniq, | ||
} from '@nx/plugin/testing'; | ||
import { execSync } from 'child_process'; | ||
import { join } from 'path'; | ||
|
||
describe('<%= cliName %> e2e', () => { | ||
let project: string; | ||
beforeAll(async () => { | ||
// publish plugin and cli | ||
const pm = getPackageManagerCommand(); | ||
execSync(`${pm.exec} nx publish <%= pluginName %> --ver=1.0.0 --tag=e2e`, { | ||
env: process.env, | ||
}); | ||
execSync(`${pm.exec} nx publish <%= cliName %> --ver=1.0.0 --tag=e2e`, { | ||
env: process.env, | ||
}); | ||
project = uniq('<%= cliName %>'); | ||
execSync(`npx <%= cliName %>@1.0.0 ${project}`, { | ||
cwd: tmpFolder(), | ||
env: process.env, | ||
}); | ||
}, 240_000); | ||
|
||
afterAll(() => { | ||
// Remove the generated project from the file system | ||
removeTmpProject(project); | ||
}); | ||
|
||
it('should create project using <%= cliName %>', () => { | ||
expect(() => | ||
checkFilesExist(join(tmpFolder(), project, 'package.json')) | ||
).not.toThrow(); | ||
}); | ||
}); |
Oops, something went wrong.