diff --git a/.changeset/fair-waves-grin.md b/.changeset/fair-waves-grin.md new file mode 100644 index 000000000..e3ac43156 --- /dev/null +++ b/.changeset/fair-waves-grin.md @@ -0,0 +1,5 @@ +--- +'@codeshift/cli': minor +--- + +Allow for an alternate npm registry and registryToken to be passed when calling the cli. diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index f0a8c5567..7e93c2b53 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -53,6 +53,14 @@ program ) .option('-d, --dry', 'dry run (no changes are made to files)') .option('--run-in-band', 'run serially in the current process') + .option( + '--registry ', + 'Define a registry where the package should be fetched from', + ) + .option( + '--registryToken ', + 'Define an authentication token to use as credentials for the registry', + ) .addOption( new Option( '--verbose ', diff --git a/packages/cli/src/main.spec.ts b/packages/cli/src/main.spec.ts index beb474aca..9b5d233be 100644 --- a/packages/cli/src/main.spec.ts +++ b/packages/cli/src/main.spec.ts @@ -640,4 +640,64 @@ describe('main', () => { ); }); }); + + describe('when using an alternative registry', () => { + it('should use the passed registry url for the PluginManager', async () => { + const spy = jest.fn(); + (PluginManager as jest.Mock).mockImplementation( + spy.mockReturnValue({ + install: () => Promise.resolve(undefined), + // @ts-ignore + require: jest.fn().mockImplementationOnce((codemodName: string) => ({ + default: { + transforms: { + '18.0.0': `${codemodName}/path/to/18.js`, + }, + }, + })), + uninstallAll: () => Promise.resolve(), + }), + ); + + await main([mockPath], { + packages: 'mylib@18.0.0', + registry: 'https://localhost:4875', + }); + + expect(spy).toHaveBeenCalledWith( + expect.objectContaining({ npmRegistryUrl: 'https://localhost:4875' }), + ); + }); + + it('should use the passed registryToken for the PluginManager', async () => { + const spy = jest.fn(); + (PluginManager as jest.Mock).mockImplementation( + spy.mockReturnValue({ + install: () => Promise.resolve(undefined), + // @ts-ignore + require: jest.fn().mockImplementationOnce((codemodName: string) => ({ + default: { + transforms: { + '18.0.0': `${codemodName}/path/to/18.js`, + }, + }, + })), + uninstallAll: () => Promise.resolve(), + }), + ); + + await main([mockPath], { + packages: 'mylib@18.0.0', + registryToken: '1234ABCD=', + }); + + expect(spy).toHaveBeenCalledWith( + expect.objectContaining({ + npmRegistryConfig: expect.objectContaining({ + auth: expect.objectContaining({ token: '1234ABCD=' }), + }), + }), + ); + }); + }); }); diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.ts index fba8c0c51..9887406a6 100644 --- a/packages/cli/src/main.ts +++ b/packages/cli/src/main.ts @@ -7,7 +7,7 @@ import inquirer from 'inquirer'; import { CodeshiftConfig } from '@codeshift/types'; import { fetchConfigAtPath, fetchConfigs } from '@codeshift/fetcher'; -import { PluginManager } from 'live-plugin-manager'; +import { PluginManager, PluginManagerOptions } from 'live-plugin-manager'; // @ts-ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398 import * as jscodeshift from 'jscodeshift/src/Runner'; @@ -23,9 +23,23 @@ export default async function main(paths: string[], flags: Flags) { ); } - const packageManager = new PluginManager({ + const pluginManagerConfig: Partial = { pluginsPath: path.join(__dirname, 'node_modules'), - }); + }; + + // If a registry is provided in the CLI flags, use it for the pluginManagers configuration. + if (flags.registry !== undefined) { + pluginManagerConfig.npmRegistryUrl = flags.registry; + } + + // If a registryToken is provided in the CLI flags, use it as an authentication token for the pluginManager + if (flags.registryToken !== undefined) { + pluginManagerConfig.npmRegistryConfig = { + auth: { token: flags.registryToken }, + }; + } + + const packageManager = new PluginManager(pluginManagerConfig); let transforms: string[] = []; diff --git a/packages/cli/src/types.ts b/packages/cli/src/types.ts index 0b764230b..6da3fff57 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -36,5 +36,10 @@ export interface Flags { cpus?: number; dry?: boolean; runInBand?: boolean; + + /** Package registry url that will be used to fetch the packages from. */ + registry?: string; + /** Authentication token that will be used to fetch packages from the registry. */ + registryToken?: string; verbose?: '0' | '1' | '2'; } diff --git a/website/docs/api/codeshift-cli.mdx b/website/docs/api/codeshift-cli.mdx index 09bad67e8..5402983a3 100644 --- a/website/docs/api/codeshift-cli.mdx +++ b/website/docs/api/codeshift-cli.mdx @@ -173,6 +173,26 @@ Get current version number - `$ codeshift --version` - `$ codeshift -v` +### --registry + +If an alternative registry url is provided, all packages will be fetched from this registry. + +**default:** + +`https://registry.npmjs.org/` + +**example:** + +- `$ codeshift --registry https://private-registry.npmjs.org/` + +### --registryToken + +If a registry token is provided, it will be used as an authentication token for the registry. + +**example:** + +- `$ codeshift --registryToken ` + ### --help Print all help text to the command line