Skip to content

Remote config fetching #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 23, 2021
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/grumpy-swans-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@codeshift/validator': minor
---

Fundamentally simplifies and improves on how validation works.
5 changes: 5 additions & 0 deletions .changeset/silly-icons-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@codeshift/types': patch
---

Initial release
5 changes: 5 additions & 0 deletions .changeset/slimy-foxes-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@codeshift/cli': minor
---

Codemods can now be sourced from standalone npm packages such as react as long as they provide a codeshift.config.js. This allows for greater flexibility for where codemods may be distributed
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"fs-extra": "^9.1.0",
"jscodeshift": "^0.12.0",
"live-plugin-manager": "^0.15.1",
"lodash": "^4.17.21",
"semver": "^7.3.5",
"ts-node": "^9.1.1"
}
Expand Down
116 changes: 96 additions & 20 deletions packages/cli/src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,12 @@ jest.mock('jscodeshift/src/Runner', () => ({
// @ts-ignore
import * as jscodeshift from 'jscodeshift/src/Runner';
import { PluginManager } from 'live-plugin-manager';

import main from './main';

const mockPath = 'src/pages/home-page/';

describe('main', () => {
beforeEach(() => {
(PluginManager as jest.Mock).mockReturnValue({
install: () => Promise.resolve(undefined),
require: (codemodName: string) => ({
transforms: {
'18.0.0': `${codemodName}/path/to/18.js`,
'19.0.0': `${codemodName}/path/to/19.js`,
'20.0.0': `${codemodName}/path/to/20.js`,
},
presets: {
'update-formatting': `${codemodName}/path/to/update-formatting.js`,
'update-imports': `${codemodName}/path/to/update-imports.js`,
},
}),
uninstallAll: () => Promise.resolve(),
});
});

afterEach(() => {
jest.resetAllMocks();
});
Expand Down Expand Up @@ -134,6 +117,26 @@ describe('main', () => {
});

describe('when running transforms with the -p flag', () => {
beforeEach(() => {
(PluginManager as jest.Mock).mockImplementation(() => ({
install: jest.fn().mockResolvedValue(undefined),
require: jest.fn().mockImplementation((codemodName: string) => {
if (!codemodName.startsWith('@codeshift')) {
throw new Error('Attempted to fetch codemod from npm');
}

return {
transforms: {
'18.0.0': `${codemodName}/path/to/18.js`,
'19.0.0': `${codemodName}/path/to/19.js`,
'20.0.0': `${codemodName}/path/to/20.js`,
},
};
}),
uninstallAll: jest.fn().mockResolvedValue(undefined),
}));
});

it('should run package transform for single version', async () => {
await main([mockPath], {
packages: 'mylib@18.0.0',
Expand Down Expand Up @@ -234,6 +237,7 @@ describe('main', () => {
expect.any(Object),
);
});

it('should run multiple transforms of the same package', async () => {
await main([mockPath], {
packages: '@myscope/mylib@20.0.0@19.0.0',
Expand Down Expand Up @@ -374,6 +378,25 @@ describe('main', () => {
});

describe('when running presets with the -p flag', () => {
beforeEach(() => {
(PluginManager as jest.Mock).mockImplementation(() => ({
install: jest.fn().mockResolvedValue(undefined),
require: jest.fn().mockImplementation((codemodName: string) => {
if (!codemodName.startsWith('@codeshift')) {
throw new Error('Attempted to fetch codemod from npm');
}

return {
presets: {
'update-formatting': `${codemodName}/path/to/update-formatting.js`,
'update-imports': `${codemodName}/path/to/update-imports.js`,
},
};
}),
uninstallAll: jest.fn().mockResolvedValue(undefined),
}));
});

it('should run single preset', async () => {
await main([mockPath], {
packages: 'mylib#update-formatting',
Expand Down Expand Up @@ -508,18 +531,71 @@ describe('main', () => {
});
});

describe('when running transforms from NPM with the -p flag', () => {
beforeEach(() => {
(PluginManager as jest.Mock).mockImplementation(() => ({
install: jest.fn().mockResolvedValue(undefined),
require: jest.fn().mockImplementation((codemodName: string) => {
if (codemodName.startsWith('@codeshift')) {
throw new Error('Attempted to fetch codemod from community folder');
}

return {
transforms: {
'18.0.0': `${codemodName}/path/to/18.js`,
},
presets: {
'update-formatting': `${codemodName}/path/to/update-formatting.js`,
},
};
}),
uninstallAll: jest.fn().mockResolvedValue(undefined),
}));
});

it('should run package transform for single version', async () => {
await main([mockPath], {
packages: 'mylib@18.0.0',
parser: 'babel',
extensions: 'js',
});

expect(jscodeshift.run).toHaveBeenCalledTimes(1);
expect(jscodeshift.run).toHaveBeenCalledWith(
'mylib/path/to/18.js',
expect.arrayContaining([mockPath]),
expect.anything(),
);
});

it('should run single preset', async () => {
await main([mockPath], {
packages: 'mylib#update-formatting',
parser: 'babel',
extensions: 'js',
});

expect(jscodeshift.run).toHaveBeenCalledTimes(1);
expect(jscodeshift.run).toHaveBeenCalledWith(
'mylib/path/to/update-formatting.js',
expect.arrayContaining([mockPath]),
expect.anything(),
);
});
});

describe('when reading configs using non-cjs exports', () => {
it('should read configs exported with export default', async () => {
(PluginManager as jest.Mock).mockReturnValue({
install: () => Promise.resolve(undefined),
// @ts-ignore
require: (codemodName: string) => ({
require: jest.fn().mockImplementationOnce((codemodName: string) => ({
default: {
transforms: {
'18.0.0': `${codemodName}/path/to/18.js`,
},
},
}),
})),
uninstallAll: () => Promise.resolve(),
});

Expand Down
111 changes: 97 additions & 14 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,84 @@
import semver from 'semver';
import chalk from 'chalk';
import path from 'path';
import { PluginManager } from 'live-plugin-manager';
import merge from 'lodash/merge';
// @ts-ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398
import * as jscodeshift from 'jscodeshift/src/Runner';
import { isValidConfig } from '@codeshift/validator';
import { CodeshiftConfig } from '@codeshift/types';

import { Flags } from './types';
import { InvalidUserInputError } from './errors';

async function fetchCommunityPackageConfig(
packageName: string,
packageManager: PluginManager,
) {
const pkgName = packageName.replace('@', '').replace('/', '__');
const commPackageName = `@codeshift/mod-${pkgName}`;

await packageManager.install(commPackageName);
const pkg = packageManager.require(commPackageName);
const config: CodeshiftConfig = pkg.default ? pkg.default : pkg;

if (!isValidConfig(config)) {
throw new Error(`Invalid config found in module ${commPackageName}`);
}

return config;
}

async function fetchRemotePackageConfig(
packageName: string,
packageManager: PluginManager,
) {
await packageManager.install(packageName);
const pkg = packageManager.require(packageName);

if (pkg) {
const config: CodeshiftConfig = pkg.default ? pkg.default : pkg;

if (config && isValidConfig(config)) {
// Found a config at the main entry-point
return config;
}
}

const info = packageManager.getInfo(packageName);

if (info) {
let config: CodeshiftConfig | undefined;

[
path.join(info?.location, 'codeshift.config.js'),
path.join(info?.location, 'codeshift.config.ts'),
path.join(info?.location, 'src', 'codeshift.config.js'),
path.join(info?.location, 'src', 'codeshift.config.ts'),
path.join(info?.location, 'codemods', 'codeshift.config.js'),
path.join(info?.location, 'codemods', 'codeshift.config.ts'),
].forEach(searchPath => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require(searchPath);
const searchConfig: CodeshiftConfig = pkg.default ? pkg.default : pkg;

if (isValidConfig(searchConfig)) {
config = searchConfig;
}
} catch (e) {}
});

if (config) return config;
}

throw new Error(
`Unable to locate a valid codeshift.config in package ${packageName}`,
);
}

export default async function main(paths: string[], flags: Flags) {
const packageManager = new PluginManager();
let transforms: string[] = [];

if (!flags.transform && !flags.packages) {
Expand All @@ -26,24 +97,36 @@ export default async function main(paths: string[], flags: Flags) {
transforms.push(flags.transform);
}

const packageManager = new PluginManager();

if (flags.packages) {
const pkgs = flags.packages.split(',').filter(pkg => !!pkg);

for (const pkg of pkgs) {
const pkgName = pkg
.split(/[@#]/)
.filter(str => !!str)[0]
.replace('/', '__');
const packageName = `@codeshift/mod-${pkgName}`;

await packageManager.install(packageName);
const codeshiftPackage = packageManager.require(packageName);

const config = codeshiftPackage.default
? codeshiftPackage.default
: codeshiftPackage;
const shouldPrependAtSymbol = pkg.startsWith('@') ? '@' : '';
const pkgName =
shouldPrependAtSymbol + pkg.split(/[@#]/).filter(str => !!str)[0];

let communityConfig;
let remoteConfig;

try {
communityConfig = await fetchCommunityPackageConfig(
pkgName,
packageManager,
);
} catch (error) {}

try {
remoteConfig = await fetchRemotePackageConfig(pkgName, packageManager);
} catch (error) {}

if (!communityConfig && !remoteConfig) {
throw new Error(
`Unable to locate package from the codeshift-community packages or as a standalone NPM package.
Make sure the package name ${pkgName} has been spelled correctly and exists before trying again.`,
);
}

const config: CodeshiftConfig = merge({}, communityConfig, remoteConfig);

const rawTransformIds = pkg.split(/(?=[@#])/).filter(str => !!str);
rawTransformIds.shift();
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isValidConfig, isValidPackageJson } from '@codeshift/validator';
import { isValidConfigAtPath, isValidPackageJson } from '@codeshift/validator';

export default async function validate(targetPath: string = '.') {
try {
await isValidConfig(targetPath);
await isValidConfigAtPath(targetPath);
await isValidPackageJson(targetPath);
} catch (error) {
console.warn(error);
Expand Down
1 change: 1 addition & 0 deletions packages/types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @codeshift/types
9 changes: 9 additions & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@codeshift/types",
"version": "0.0.1",
"main": "dist/codeshift-types.cjs.js",
"module": "dist/codeshift-types.esm.js",
"types": "dist/codeshift-types.cjs.d.ts",
"license": "MIT",
"repository": "https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/master/packages/types"
}
7 changes: 7 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface CodeshiftConfig {
target?: string[];
maintainers?: string[];
description?: string;
transforms?: Record<string, string>;
presets?: Record<string, string>;
}
5 changes: 5 additions & 0 deletions packages/validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
"license": "MIT",
"repository": "https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/master/packages/validator",
"dependencies": {
"@codeshift/types": "^0.0.1",
"fs-extra": "^9.1.0",
"lodash": "^4.17.21",
"recast": "^0.20.4",
"semver": "^7.3.5",
"ts-node": "^9.1.1"
},
"devDependencies": {
"@types/lodash": "^4.14.176"
}
}
Loading