Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-waves-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@codeshift/cli': minor
---

Allow for an alternate npm registry and registryToken to be passed when calling the cli.
8 changes: 8 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <value>',
'Define a registry where the package should be fetched from',
)
.option(
'--registryToken <value>',
'Define an authentication token to use as credentials for the registry',
)
.addOption(
new Option(
'--verbose <parser>',
Expand Down
60 changes: 60 additions & 0 deletions packages/cli/src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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=' }),
}),
}),
);
});
});
});
20 changes: 17 additions & 3 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -23,9 +23,23 @@ export default async function main(paths: string[], flags: Flags) {
);
}

const packageManager = new PluginManager({
const pluginManagerConfig: Partial<PluginManagerOptions> = {
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[] = [];

Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
20 changes: 20 additions & 0 deletions website/docs/api/codeshift-cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ACCESS_TOKEN>`

### --help

Print all help text to the command line
Expand Down