Skip to content

Commit 70b602e

Browse files
committed
✨ Remote config file search
1 parent 6de9ac1 commit 70b602e

File tree

5 files changed

+61
-32
lines changed

5 files changed

+61
-32
lines changed

packages/cli/src/main.ts

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import semver from 'semver';
22
import chalk from 'chalk';
33
import path from 'path';
44
import { PluginManager } from 'live-plugin-manager';
5+
import merge from 'lodash/merge';
56
// @ts-ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398
67
import * as jscodeshift from 'jscodeshift/src/Runner';
78
import { isValidConfig } from '@codeshift/validator';
@@ -11,42 +12,59 @@ import { InvalidUserInputError } from './errors';
1112

1213
const packageManager = new PluginManager();
1314

14-
async function fetchPackageConfig(packageName: string) {
15-
// Attempt to find package from the community folder
16-
await packageManager.install(`@codeshift/mod-${packageName}`);
17-
const commPkg = packageManager.require(packageName);
18-
const commConfig = commPkg.default ? commPkg.default : commPkg;
15+
async function fetchCommunityPackageConfig(packageName: string) {
16+
const commPackageName = `@codeshift/mod-${packageName}`;
17+
await packageManager.install(commPackageName);
18+
const pkg = packageManager.require(packageName);
19+
const config = pkg.default ? pkg.default : pkg;
1920

20-
// if (!isValidConfig(commConfig)) {
21-
// }
21+
if (!isValidConfig(config)) {
22+
throw new Error(`Invalid config found in module ${commPackageName}`);
23+
}
2224

23-
// Attempt to find source package from npm
24-
await packageManager.install(packageName);
25-
// For source packages, fetching configs is a bit more elaborate
26-
let config;
25+
return config;
26+
}
2727

28-
// Attemp to fetch from the main entrypoint
29-
const info = packageManager.getInfo(packageName);
28+
async function fetchRemotePackageConfig(packageName: string) {
29+
await packageManager.install(packageName);
3030
const pkg = packageManager.require(packageName);
3131

32-
if (info || pkg) {
33-
config = pkg.default ? pkg.default : pkg;
32+
if (pkg) {
33+
const config = pkg.default ? pkg.default : pkg;
3434

35-
if (config && isValidConfig) {
35+
if (config && isValidConfig(config)) {
3636
// Found a config at the main entry-point
37+
return config;
3738
}
39+
}
3840

39-
config = require(path.join(info?.location, 'codeshift.config.js'));
40-
config = require(path.join(info?.location, 'src', 'codeshift.config.js'));
41-
config = require(path.join(
42-
info?.location,
43-
'codemods',
44-
'codeshift.config.js',
45-
));
41+
const info = packageManager.getInfo(packageName);
42+
43+
if (info) {
44+
let config: any;
45+
46+
[
47+
path.join(info?.location, 'codeshift.config.js'),
48+
path.join(info?.location, 'src', 'codeshift.config.js'),
49+
path.join(info?.location, 'codemods', 'codeshift.config.js'),
50+
].forEach(searchPath => {
51+
try {
52+
// eslint-disable-next-line @typescript-eslint/no-var-requires
53+
const pkg = require(searchPath);
54+
const searchConfig = pkg.default ? pkg.default : pkg;
55+
56+
if (isValidConfig(searchConfig)) {
57+
config = searchConfig;
58+
}
59+
} catch (e) {}
60+
});
61+
62+
if (config) return config;
4663
}
47-
// if ()
4864

49-
return config;
65+
throw new Error(
66+
`Unable to locate a valid codeshift.config in package ${packageName}`,
67+
);
5068
}
5169

5270
export default async function main(paths: string[], flags: Flags) {
@@ -77,7 +95,9 @@ export default async function main(paths: string[], flags: Flags) {
7795
.filter(str => !!str)[0]
7896
.replace('/', '__');
7997

80-
const config = await fetchPackageConfig(pkgName);
98+
const communityConfig = await fetchCommunityPackageConfig(pkgName);
99+
const remoteConfig = await fetchRemotePackageConfig(pkgName);
100+
const config = merge(communityConfig, remoteConfig);
81101

82102
const rawTransformIds = pkg.split(/(?=[@#])/).filter(str => !!str);
83103
rawTransformIds.shift();

packages/types/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codeshift/types",
3-
"version": "0.0.0",
3+
"version": "0.0.1",
44
"main": "dist/codeshift-types.cjs.js",
55
"module": "dist/codeshift-types.esm.js",
66
"types": "dist/codeshift-types.cjs.d.ts",

packages/validator/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
"dependencies": {
99
"@codeshift/types": "^0.0.1",
1010
"fs-extra": "^9.1.0",
11+
"lodash": "^4.17.21",
1112
"recast": "^0.20.4",
1213
"semver": "^7.3.5",
1314
"ts-node": "^9.1.1"
15+
},
16+
"devDependencies": {
17+
"@types/lodash": "^4.14.176"
1418
}
1519
}

packages/validator/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ export function isValidPackageName(dir: string) {
2929
return dir.match(/^(@[a-z0-9-~][a-z0-9-._~]*__)?[a-z0-9-~][a-z0-9-._~]*$/);
3030
}
3131

32-
export async function isValidConfig(config: CodeshiftConfig) {
32+
export function isValidConfig(config: CodeshiftConfig) {
3333
return (
3434
hasValidTransforms(config.transforms) || hasValidPresets(config.presets)
3535
);
3636
}
3737

38-
export async function isValidConfigAtPath(filePath: string) {
38+
export function isValidConfigAtPath(filePath: string) {
3939
const config = getConfigFromPath(filePath);
4040

4141
if (

yarn.lock

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,11 @@
17221722
resolved "https://registry.npmjs.org/@types/lockfile/-/lockfile-1.0.2.tgz#3f77e84171a2b7e3198bd5717c7547a54393baf8"
17231723
integrity sha512-jD5VbvhfMhaYN4M3qPJuhMVUg3Dfc4tvPvLEAXn6GXbs/ajDFtCQahX37GIE65ipTI3I+hEvNaXS3MYAn9Ce3Q==
17241724

1725+
"@types/lodash@^4.14.176":
1726+
version "4.14.176"
1727+
resolved "https://packages.atlassian.com/api/npm/npm-remote/@types/lodash/-/lodash-4.14.176.tgz#641150fc1cda36fbfa329de603bbb175d7ee20c0"
1728+
integrity sha1-ZBFQ/BzaNvv6Mp3mA7uxddfuIMA=
1729+
17251730
"@types/minimatch@*":
17261731
version "3.0.5"
17271732
resolved "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
@@ -5009,10 +5014,10 @@ [email protected]:
50095014
resolved "https://registry.npmjs.org/lodash.unescape/-/lodash.unescape-4.0.1.tgz#bf2249886ce514cda112fae9218cdc065211fc9c"
50105015
integrity sha1-vyJJiGzlFM2hEvrpIYzcBlIR/Jw=
50115016

5012-
[email protected], lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.14, lodash@^4.7.0:
5017+
[email protected], lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.14, lodash@^4.17.21, lodash@^4.7.0:
50135018
version "4.17.21"
5014-
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
5015-
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
5019+
resolved "https://packages.atlassian.com/api/npm/npm-remote/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
5020+
integrity sha1-Z5WRxWTDv/quhFTPCz3zcMPWkRw=
50165021

50175022
loud-rejection@^1.0.0:
50185023
version "1.6.0"

0 commit comments

Comments
 (0)