Skip to content

Commit c4a39a3

Browse files
Merge pull request #158 from tomheller/main
feat: allow alternative npm registry url
2 parents 3a16fe6 + 044cf6f commit c4a39a3

File tree

6 files changed

+115
-3
lines changed

6 files changed

+115
-3
lines changed

.changeset/fair-waves-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codeshift/cli': minor
3+
---
4+
5+
Allow for an alternate npm registry and registryToken to be passed when calling the cli.

packages/cli/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ program
5353
)
5454
.option('-d, --dry', 'dry run (no changes are made to files)')
5555
.option('--run-in-band', 'run serially in the current process')
56+
.option(
57+
'--registry <value>',
58+
'Define a registry where the package should be fetched from',
59+
)
60+
.option(
61+
'--registryToken <value>',
62+
'Define an authentication token to use as credentials for the registry',
63+
)
5664
.addOption(
5765
new Option(
5866
'--verbose <parser>',

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 & 3 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,9 +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-
});
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);
2943

3044
let transforms: string[] = [];
3145

packages/cli/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,10 @@ export interface Flags {
3636
cpus?: number;
3737
dry?: boolean;
3838
runInBand?: boolean;
39+
40+
/** Package registry url that will be used to fetch the packages from. */
41+
registry?: string;
42+
/** Authentication token that will be used to fetch packages from the registry. */
43+
registryToken?: string;
3944
verbose?: '0' | '1' | '2';
4045
}

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)