Skip to content

Commit 7e3a6d9

Browse files
committed
Use globby for config fetching
1 parent ebf2a44 commit 7e3a6d9

File tree

9 files changed

+102
-79
lines changed

9 files changed

+102
-79
lines changed

.changeset/hungry-badgers-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codeshift/cli': patch
3+
---
4+
5+
Internal refactor, CLI now uses fetcher package

.changeset/selfish-wombats-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codeshift/fetcher': patch
3+
---
4+
5+
Initial release

packages/cli/src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ export default async function main(paths: string[], flags: Flags) {
5959
getCodeshiftPackageName(pkgName),
6060
packageManager,
6161
);
62+
} catch (error) {}
63+
64+
try {
6265
remoteConfig = await fetchRemotePackage(pkgName, packageManager);
6366
} catch (error) {}
6467

packages/fetcher/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@codeshift/types": "*",
1010
"chalk": "^4.1.0",
1111
"fs-extra": "^9.1.0",
12+
"globby": "^11.1.0",
1213
"live-plugin-manager": "^0.15.1",
1314
"ts-node": "^9.1.1"
1415
}

packages/fetcher/src/index.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import path from 'path';
2+
import globby from 'globby';
23
import { PluginManager } from 'live-plugin-manager';
34

45
import { CodeshiftConfig } from '@codeshift/types';
56

6-
export function fetchConfig(filePath: string): CodeshiftConfig | undefined {
7+
export async function fetchConfig(
8+
filePath: string,
9+
): Promise<CodeshiftConfig | undefined> {
710
let config: CodeshiftConfig | undefined;
811

9-
[
10-
path.join(filePath, 'codeshift.config.js'),
11-
path.join(filePath, 'codeshift.config.ts'),
12-
path.join(filePath, 'src', 'codeshift.config.js'),
13-
path.join(filePath, 'src', 'codeshift.config.ts'),
14-
path.join(filePath, 'codemods', 'codeshift.config.js'),
15-
path.join(filePath, 'codemods', 'codeshift.config.ts'),
16-
].forEach(searchPath => {
12+
const matchedPaths = await globby([
13+
path.join(filePath, 'codeshift.config.(js|ts|tsx)'),
14+
path.join(filePath, 'src', 'codeshift.config.(js|ts|tsx)'),
15+
path.join(filePath, 'codemods', 'codeshift.config.(js|ts|tsx)'),
16+
]);
17+
18+
matchedPaths.forEach(matchedPath => {
1719
try {
1820
// eslint-disable-next-line @typescript-eslint/no-var-requires
19-
const pkg = require(searchPath);
21+
const pkg = require(matchedPath);
2022
const searchConfig = pkg.default ? pkg.default : pkg;
2123
config = searchConfig;
2224
} catch (e) {}
@@ -39,13 +41,11 @@ export async function fetchRemotePackage(
3941
packageName: string,
4042
packageManager: PluginManager,
4143
): Promise<CodeshiftConfig | undefined> {
42-
let config = await fetchPackage(packageName, packageManager);
43-
44+
const config = await fetchPackage(packageName, packageManager);
4445
if (config) return config;
4546

4647
const info = packageManager.getInfo(packageName);
47-
4848
if (!info) return undefined;
4949

50-
return fetchConfig(info.location);
50+
return await fetchConfig(info.location);
5151
}

packages/validator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"license": "MIT",
77
"repository": "https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/master/packages/validator",
88
"dependencies": {
9+
"@codeshift/fetcher": "^0.0.1",
910
"@codeshift/types": "^0.0.5",
1011
"fs-extra": "^9.1.0",
1112
"lodash": "^4.17.21",

packages/validator/src/index.spec.ts

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
jest.mock('fs-extra');
2+
jest.mock('@codeshift/fetcher');
23

34
import fs from 'fs-extra';
4-
import path from 'path';
5+
6+
import { fetchConfig } from '@codeshift/fetcher';
57

68
import {
79
isValidPackageName,
@@ -112,58 +114,38 @@ describe('validator', () => {
112114
jest.resetModules();
113115
});
114116

115-
it('should validate config', () => {
116-
jest.mock(
117-
path.join(__dirname, 'path', 'to', 'codeshift.config.js'),
118-
() => ({
119-
__esModule: true,
120-
default: {
121-
transforms: {
122-
'10.0.0': 'path/to/transform.js',
123-
},
124-
},
125-
}),
126-
{ virtual: true },
127-
);
117+
it('should validate config', async () => {
118+
(fetchConfig as jest.Mock).mockResolvedValue({
119+
transforms: {
120+
'10.0.0': 'path/to/transform.js',
121+
},
122+
});
128123

129-
expect(isValidConfigAtPath('path/to/')).toEqual(true);
124+
const result = await isValidConfigAtPath('path/to/');
125+
expect(result).toEqual(true);
130126
});
131127

132-
it('should error if config contains invalid transforms', () => {
133-
jest.mock(
134-
path.join(__dirname, 'path', 'to', 'codeshift.config.js'),
135-
() => ({
136-
__esModule: true,
137-
default: {
138-
transforms: {
139-
hello: '',
140-
},
141-
},
142-
}),
143-
{ virtual: true },
144-
);
128+
it('should error if config contains invalid transforms', async () => {
129+
(fetchConfig as jest.Mock).mockResolvedValue({
130+
transforms: {
131+
hello: '',
132+
},
133+
});
145134

146-
expect(() => isValidConfigAtPath('path/to/')).toThrowError(
135+
await expect(isValidConfigAtPath('path/to/')).rejects.toThrowError(
147136
`Invalid transform ids found for config at "path/to/".
148137
Please make sure all transforms are identified by a valid semver version. ie 10.0.0`,
149138
);
150139
});
151140

152-
it('should error if config contains invalid presets', () => {
153-
jest.mock(
154-
path.join(__dirname, 'path', 'to', 'codeshift.config.js'),
155-
() => ({
156-
__esModule: true,
157-
default: {
158-
presets: {
159-
'foo bar': '',
160-
},
161-
},
162-
}),
163-
{ virtual: true },
164-
);
141+
it('should error if config contains invalid presets', async () => {
142+
(fetchConfig as jest.Mock).mockResolvedValue({
143+
presets: {
144+
'foo bar': '',
145+
},
146+
});
165147

166-
expect(() => isValidConfigAtPath('path/to/')).toThrowError(
148+
await expect(isValidConfigAtPath('path/to/')).rejects.toThrowError(
167149
`Invalid preset ids found for config at "path/to/".
168150
Please make sure all presets are kebab case and contain no spaces or special characters. ie sort-imports-by-scope`,
169151
);
@@ -196,6 +178,7 @@ Please make sure all presets are kebab case and contain no spaces or special cha
196178
try {
197179
await isValidPackageJson('path/to/');
198180
} catch (error) {
181+
// @ts-ignore
199182
expect(error.message).toMatch(
200183
'No main entrypoint provided in package.json',
201184
);
@@ -210,6 +193,7 @@ Please make sure all presets are kebab case and contain no spaces or special cha
210193
try {
211194
await isValidPackageJson('path/to/');
212195
} catch (error) {
196+
// @ts-ignore
213197
expect(error.message).toMatch(
214198
'No package name provided in package.json',
215199
);

packages/validator/src/index.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
import fs from 'fs-extra';
22
import semver from 'semver';
33
import path from 'path';
4-
import { CodeshiftConfig } from '@codeshift/types';
5-
6-
function getConfigFromPath(filePath: string): CodeshiftConfig {
7-
const configPath = path.join(__dirname, filePath, 'codeshift.config.js');
8-
// eslint-disable-next-line @typescript-eslint/no-var-requires
9-
const config = require(configPath);
104

11-
return !!config.default ? config.default : config;
12-
}
5+
import { CodeshiftConfig } from '@codeshift/types';
6+
import { fetchConfig } from '@codeshift/fetcher';
137

14-
function hasValidTransforms(transforms?: Record<string, string>) {
15-
if (!transforms) return true;
8+
function hasValidTransforms(config: CodeshiftConfig) {
9+
if (!config.transforms) return true;
1610

17-
return Object.entries(transforms).every(([key]) => semver.valid(key));
11+
return Object.entries(config.transforms).every(([key]) => semver.valid(key));
1812
}
1913

20-
function hasValidPresets(presets?: Record<string, string>): boolean {
21-
if (!presets) return true;
14+
function hasValidPresets(config: CodeshiftConfig): boolean {
15+
if (!config.presets) return true;
2216

23-
return Object.entries(presets).every(([key]) =>
17+
return Object.entries(config.presets).every(([key]) =>
2418
key.match(/^[0-9a-zA-Z\-]+$/),
2519
);
2620
}
@@ -30,20 +24,22 @@ export function isValidPackageName(dir: string): boolean {
3024
}
3125

3226
export function isValidConfig(config: CodeshiftConfig) {
33-
return (
34-
hasValidTransforms(config.transforms) && hasValidPresets(config.presets)
35-
);
27+
return hasValidTransforms(config) && hasValidPresets(config);
3628
}
3729

38-
export function isValidConfigAtPath(filePath: string) {
39-
const config = getConfigFromPath(filePath);
30+
export async function isValidConfigAtPath(filePath: string) {
31+
const config = await fetchConfig(filePath);
32+
33+
if (!config) {
34+
throw new Error('Unable to locate config file');
35+
}
4036

41-
if (!hasValidTransforms(config.transforms)) {
37+
if (!hasValidTransforms(config)) {
4238
throw new Error(`Invalid transform ids found for config at "${filePath}".
4339
Please make sure all transforms are identified by a valid semver version. ie 10.0.0`);
4440
}
4541

46-
if (!hasValidPresets(config.presets)) {
42+
if (!hasValidPresets(config)) {
4743
throw new Error(`Invalid preset ids found for config at "${filePath}".
4844
Please make sure all presets are kebab case and contain no spaces or special characters. ie sort-imports-by-scope`);
4945
}

yarn.lock

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3329,6 +3329,17 @@ fast-glob@^3.1.1, fast-glob@^3.2.4:
33293329
merge2 "^1.3.0"
33303330
micromatch "^4.0.4"
33313331

3332+
fast-glob@^3.2.9:
3333+
version "3.2.10"
3334+
resolved "https://packages.atlassian.com/api/npm/npm-remote/fast-glob/-/fast-glob-3.2.10.tgz#2734f83baa7f43b7fd41e13bc34438f4ffe284ee"
3335+
integrity sha1-JzT4O6p/Q7f9QeE7w0Q49P/ihO4=
3336+
dependencies:
3337+
"@nodelib/fs.stat" "^2.0.2"
3338+
"@nodelib/fs.walk" "^1.2.3"
3339+
glob-parent "^5.1.2"
3340+
merge2 "^1.3.0"
3341+
micromatch "^4.0.4"
3342+
33323343
fast-json-stable-stringify@2.x, fast-json-stable-stringify@^2.0.0:
33333344
version "2.1.0"
33343345
resolved "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
@@ -3707,6 +3718,18 @@ globby@^11.0.0:
37073718
merge2 "^1.3.0"
37083719
slash "^3.0.0"
37093720

3721+
globby@^11.1.0:
3722+
version "11.1.0"
3723+
resolved "https://packages.atlassian.com/api/npm/npm-remote/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b"
3724+
integrity sha1-vUvpi7BC+D15b344EZkfvoKg00s=
3725+
dependencies:
3726+
array-union "^2.1.0"
3727+
dir-glob "^3.0.1"
3728+
fast-glob "^3.2.9"
3729+
ignore "^5.2.0"
3730+
merge2 "^1.4.1"
3731+
slash "^3.0.0"
3732+
37103733
globby@^9.2.0:
37113734
version "9.2.0"
37123735
resolved "https://registry.npmjs.org/globby/-/globby-9.2.0.tgz#fd029a706c703d29bdd170f4b6db3a3f7a7cb63d"
@@ -3901,6 +3924,11 @@ ignore@^5.1.4:
39013924
resolved "https://registry.npmjs.org/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57"
39023925
integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==
39033926

3927+
ignore@^5.2.0:
3928+
version "5.2.0"
3929+
resolved "https://packages.atlassian.com/api/npm/npm-remote/ignore/-/ignore-5.2.0.tgz#6d3bac8fa7fe0d45d9f9be7bac2fc279577e345a"
3930+
integrity sha1-bTusj6f+DUXZ+b57rC/CeVd+NFo=
3931+
39043932
import-fresh@^3.0.0:
39053933
version "3.3.0"
39063934
resolved "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"
@@ -5165,7 +5193,7 @@ merge-stream@^2.0.0:
51655193
resolved "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
51665194
integrity sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==
51675195

5168-
merge2@^1.2.3, merge2@^1.3.0:
5196+
merge2@^1.2.3, merge2@^1.3.0, merge2@^1.4.1:
51695197
version "1.4.1"
51705198
resolved "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae"
51715199
integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==

0 commit comments

Comments
 (0)