Skip to content

Commit 044cf6f

Browse files
committed
fixup! feat: allow alternative npm registry url
1 parent bbc8fb8 commit 044cf6f

File tree

7 files changed

+106
-7
lines changed

7 files changed

+106
-7
lines changed

.changeset/fair-waves-grin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@codeshift/cli': minor
33
---
44

5-
Allow for an alternate npm registry to be passed when calling the cli.
5+
Allow for an alternate npm registry and registryToken to be passed when calling the cli.

packages/cli/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ program
5757
'--registry <value>',
5858
'Define a registry where the package should be fetched from',
5959
)
60+
.option(
61+
'--registryToken <value>',
62+
'Define an authentication token to use as credentials for the registry',
63+
)
6064
.addOption(
6165
new Option(
6266
'--verbose <parser>',

packages/cli/src/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { PluginManager } from 'live-plugin-manager';
44
import { fetchPackageConfig } from './fetch-package';
55

66
export default async function list(packages: string[]) {
7-
const packageManager = new PluginManager({});
7+
const packageManager = new PluginManager();
88
const configs = [];
99

1010
for (const packageName of packages) {

packages/cli/src/main.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,4 +640,64 @@ describe('main', () => {
640640
);
641641
});
642642
});
643+
644+
describe('when using an alternative registry', () => {
645+
it('should use the passed registry url for the PluginManager', async () => {
646+
const spy = jest.fn();
647+
(PluginManager as jest.Mock).mockImplementation(
648+
spy.mockReturnValue({
649+
install: () => Promise.resolve(undefined),
650+
// @ts-ignore
651+
require: jest.fn().mockImplementationOnce((codemodName: string) => ({
652+
default: {
653+
transforms: {
654+
'18.0.0': `${codemodName}/path/to/18.js`,
655+
},
656+
},
657+
})),
658+
uninstallAll: () => Promise.resolve(),
659+
}),
660+
);
661+
662+
await main([mockPath], {
663+
packages: 'mylib@18.0.0',
664+
registry: 'https://localhost:4875',
665+
});
666+
667+
expect(spy).toHaveBeenCalledWith(
668+
expect.objectContaining({ npmRegistryUrl: 'https://localhost:4875' }),
669+
);
670+
});
671+
672+
it('should use the passed registryToken for the PluginManager', async () => {
673+
const spy = jest.fn();
674+
(PluginManager as jest.Mock).mockImplementation(
675+
spy.mockReturnValue({
676+
install: () => Promise.resolve(undefined),
677+
// @ts-ignore
678+
require: jest.fn().mockImplementationOnce((codemodName: string) => ({
679+
default: {
680+
transforms: {
681+
'18.0.0': `${codemodName}/path/to/18.js`,
682+
},
683+
},
684+
})),
685+
uninstallAll: () => Promise.resolve(),
686+
}),
687+
);
688+
689+
await main([mockPath], {
690+
packages: 'mylib@18.0.0',
691+
registryToken: '1234ABCD=',
692+
});
693+
694+
expect(spy).toHaveBeenCalledWith(
695+
expect.objectContaining({
696+
npmRegistryConfig: expect.objectContaining({
697+
auth: expect.objectContaining({ token: '1234ABCD=' }),
698+
}),
699+
}),
700+
);
701+
});
702+
});
643703
});

packages/cli/src/main.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import inquirer from 'inquirer';
77

88
import { CodeshiftConfig } from '@codeshift/types';
99
import { fetchConfigAtPath, fetchConfigs } from '@codeshift/fetcher';
10-
import { PluginManager } from 'live-plugin-manager';
10+
import { PluginManager, PluginManagerOptions } from 'live-plugin-manager';
1111
// @ts-ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398
1212
import * as jscodeshift from 'jscodeshift/src/Runner';
1313

@@ -23,10 +23,23 @@ export default async function main(paths: string[], flags: Flags) {
2323
);
2424
}
2525

26-
const packageManager = new PluginManager({
26+
const pluginManagerConfig: Partial<PluginManagerOptions> = {
2727
pluginsPath: path.join(__dirname, 'node_modules'),
28-
npmRegistryUrl: flags.registry,
29-
});
28+
};
29+
30+
// If a registry is provided in the CLI flags, use it for the pluginManagers configuration.
31+
if (flags.registry !== undefined) {
32+
pluginManagerConfig.npmRegistryUrl = flags.registry;
33+
}
34+
35+
// If a registryToken is provided in the CLI flags, use it as an authentication token for the pluginManager
36+
if (flags.registryToken !== undefined) {
37+
pluginManagerConfig.npmRegistryConfig = {
38+
auth: { token: flags.registryToken },
39+
};
40+
}
41+
42+
const packageManager = new PluginManager(pluginManagerConfig);
3043

3144
let transforms: string[] = [];
3245

packages/cli/src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export interface Flags {
3737
dry?: boolean;
3838
runInBand?: boolean;
3939

40-
/** Npm registry url that will be used to fetch the packages from. */
40+
/** Package registry url that will be used to fetch the packages from. */
4141
registry?: string;
42+
/** Authentication token that will be used to fetch packages from the registry. */
43+
registryToken?: string;
4244
verbose?: '0' | '1' | '2';
4345
}

website/docs/api/codeshift-cli.mdx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ Get current version number
173173
- `$ codeshift --version`
174174
- `$ codeshift -v`
175175

176+
### --registry
177+
178+
If an alternative registry url is provided, all packages will be fetched from this registry.
179+
180+
**default:**
181+
182+
`https://registry.npmjs.org/`
183+
184+
**example:**
185+
186+
- `$ codeshift --registry https://private-registry.npmjs.org/`
187+
188+
### --registryToken
189+
190+
If a registry token is provided, it will be used as an authentication token for the registry.
191+
192+
**example:**
193+
194+
- `$ codeshift --registryToken <ACCESS_TOKEN>`
195+
176196
### --help
177197

178198
Print all help text to the command line

0 commit comments

Comments
 (0)