From bbc8fb83a93ec1fbf324f4cbc3afc9fa929b9a37 Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Fri, 28 Apr 2023 12:06:59 +0200 Subject: [PATCH 1/2] feat: allow alternative npm registry url --- .changeset/fair-waves-grin.md | 5 +++++ packages/cli/src/index.ts | 4 ++++ packages/cli/src/list.ts | 2 +- packages/cli/src/main.ts | 1 + packages/cli/src/types.ts | 3 +++ 5 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changeset/fair-waves-grin.md diff --git a/.changeset/fair-waves-grin.md b/.changeset/fair-waves-grin.md new file mode 100644 index 000000000..878f497a3 --- /dev/null +++ b/.changeset/fair-waves-grin.md @@ -0,0 +1,5 @@ +--- +'@codeshift/cli': minor +--- + +Allow for an alternate npm registry to be passed when calling the cli. diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index f0a8c5567..b934c84b2 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -53,6 +53,10 @@ 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', + ) .addOption( new Option( '--verbose ', diff --git a/packages/cli/src/list.ts b/packages/cli/src/list.ts index 246c35057..9f7fe4b1e 100644 --- a/packages/cli/src/list.ts +++ b/packages/cli/src/list.ts @@ -4,7 +4,7 @@ import { PluginManager } from 'live-plugin-manager'; import { fetchPackageConfig } from './fetch-package'; export default async function list(packages: string[]) { - const packageManager = new PluginManager(); + const packageManager = new PluginManager({}); const configs = []; for (const packageName of packages) { diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.ts index fba8c0c51..e4832653e 100644 --- a/packages/cli/src/main.ts +++ b/packages/cli/src/main.ts @@ -25,6 +25,7 @@ export default async function main(paths: string[], flags: Flags) { const packageManager = new PluginManager({ pluginsPath: path.join(__dirname, 'node_modules'), + npmRegistryUrl: flags.registry, }); let transforms: string[] = []; diff --git a/packages/cli/src/types.ts b/packages/cli/src/types.ts index 0b764230b..43f1f74ba 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -36,5 +36,8 @@ export interface Flags { cpus?: number; dry?: boolean; runInBand?: boolean; + + /** Npm registry url that will be used to fetch the packages from. */ + registry?: string; verbose?: '0' | '1' | '2'; } From 044cf6f40b56c7a8a2095a1fac02bc6241f658bf Mon Sep 17 00:00:00 2001 From: Tom Heller Date: Tue, 2 May 2023 20:37:35 +0200 Subject: [PATCH 2/2] fixup! feat: allow alternative npm registry url --- .changeset/fair-waves-grin.md | 2 +- packages/cli/src/index.ts | 4 ++ packages/cli/src/list.ts | 2 +- packages/cli/src/main.spec.ts | 60 ++++++++++++++++++++++++++++++ packages/cli/src/main.ts | 21 +++++++++-- packages/cli/src/types.ts | 4 +- website/docs/api/codeshift-cli.mdx | 20 ++++++++++ 7 files changed, 106 insertions(+), 7 deletions(-) diff --git a/.changeset/fair-waves-grin.md b/.changeset/fair-waves-grin.md index 878f497a3..e3ac43156 100644 --- a/.changeset/fair-waves-grin.md +++ b/.changeset/fair-waves-grin.md @@ -2,4 +2,4 @@ '@codeshift/cli': minor --- -Allow for an alternate npm registry to be passed when calling the cli. +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 b934c84b2..7e93c2b53 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -57,6 +57,10 @@ program '--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/list.ts b/packages/cli/src/list.ts index 9f7fe4b1e..246c35057 100644 --- a/packages/cli/src/list.ts +++ b/packages/cli/src/list.ts @@ -4,7 +4,7 @@ import { PluginManager } from 'live-plugin-manager'; import { fetchPackageConfig } from './fetch-package'; export default async function list(packages: string[]) { - const packageManager = new PluginManager({}); + const packageManager = new PluginManager(); const configs = []; for (const packageName of packages) { 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 e4832653e..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,10 +23,23 @@ export default async function main(paths: string[], flags: Flags) { ); } - const packageManager = new PluginManager({ + const pluginManagerConfig: Partial = { pluginsPath: path.join(__dirname, 'node_modules'), - npmRegistryUrl: flags.registry, - }); + }; + + // 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 43f1f74ba..6da3fff57 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -37,7 +37,9 @@ export interface Flags { dry?: boolean; runInBand?: boolean; - /** Npm registry url that will be used to fetch the packages from. */ + /** 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