Skip to content

Commit fb799fa

Browse files
committed
fixes main logic
1 parent 819a913 commit fb799fa

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

packages/cli/src/main.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import { InvalidUserInputError } from './errors';
1414
const packageManager = new PluginManager();
1515

1616
async function fetchCommunityPackageConfig(packageName: string) {
17-
const commPackageName = `@codeshift/mod-${packageName}`;
17+
const pkgName = packageName.replace('@', '').replace('/', '__');
18+
const commPackageName = `@codeshift/mod-${pkgName}`;
19+
1820
await packageManager.install(commPackageName);
19-
const pkg = packageManager.require(packageName);
21+
const pkg = packageManager.require(commPackageName);
2022
const config: CodeshiftConfig = pkg.default ? pkg.default : pkg;
2123

2224
if (!isValidConfig(config)) {
@@ -42,7 +44,7 @@ async function fetchRemotePackageConfig(packageName: string) {
4244
const info = packageManager.getInfo(packageName);
4345

4446
if (info) {
45-
let config: any;
47+
let config: CodeshiftConfig | undefined;
4648

4749
[
4850
path.join(info?.location, 'codeshift.config.js'),
@@ -94,14 +96,29 @@ export default async function main(paths: string[], flags: Flags) {
9496
const pkgs = flags.packages.split(',').filter(pkg => !!pkg);
9597

9698
for (const pkg of pkgs) {
97-
const pkgName = pkg
98-
.split(/[@#]/)
99-
.filter(str => !!str)[0]
100-
.replace('/', '__');
101-
102-
const communityConfig = await fetchCommunityPackageConfig(pkgName);
103-
const remoteConfig = await fetchRemotePackageConfig(pkgName);
104-
const config: CodeshiftConfig = merge(communityConfig, remoteConfig);
99+
const shouldPrependAtSymbol = pkg.startsWith('@') ? '@' : '';
100+
const pkgName =
101+
shouldPrependAtSymbol + pkg.split(/[@#]/).filter(str => !!str)[0];
102+
103+
let communityConfig;
104+
let remoteConfig;
105+
106+
try {
107+
communityConfig = await fetchCommunityPackageConfig(pkgName);
108+
} catch (error) {}
109+
110+
try {
111+
remoteConfig = await fetchRemotePackageConfig(pkgName);
112+
} catch (error) {}
113+
114+
if (!communityConfig && !remoteConfig) {
115+
throw new Error(
116+
`Unable to locate package from the codeshift-community packages or as a standalone NPM package.
117+
Make sure the package name ${pkgName} has been spelled correctly and exists before trying again.`,
118+
);
119+
}
120+
121+
const config: CodeshiftConfig = merge({}, communityConfig, remoteConfig);
105122

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

0 commit comments

Comments
 (0)