|
1 | 1 | import path from 'path'; |
2 | 2 | import semver from 'semver'; |
3 | 3 | import chalk from 'chalk'; |
4 | | -import ora from 'ora'; |
5 | 4 | import { PluginManager } from 'live-plugin-manager'; |
6 | | -import merge from 'lodash/merge'; |
| 5 | + |
7 | 6 | // @ts-ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398 |
8 | 7 | import * as jscodeshift from 'jscodeshift/src/Runner'; |
9 | 8 |
|
10 | | -import { fetchPackage, fetchRemotePackage } from '@codeshift/fetcher'; |
11 | | -import { isValidConfig } from '@codeshift/validator'; |
12 | | -import { CodeshiftConfig } from '@codeshift/types'; |
13 | | - |
14 | 9 | import { Flags } from './types'; |
15 | 10 | import { InvalidUserInputError } from './errors'; |
16 | | - |
17 | | -function getCodeshiftPackageName(packageName: string) { |
18 | | - return `@codeshift/mod-${packageName.replace('@', '').replace('/', '__')}`; |
19 | | -} |
| 11 | +import { fetchPackageConfig } from './fetch-package'; |
20 | 12 |
|
21 | 13 | export default async function main(paths: string[], flags: Flags) { |
22 | 14 | const packageManager = new PluginManager({ |
@@ -53,56 +45,7 @@ export default async function main(paths: string[], flags: Flags) { |
53 | 45 | const pkgName = |
54 | 46 | shouldPrependAtSymbol + pkg.split(/[@#]/).filter(str => !!str)[0]; |
55 | 47 |
|
56 | | - let codeshiftConfig: CodeshiftConfig | undefined; |
57 | | - let remoteConfig: CodeshiftConfig | undefined; |
58 | | - |
59 | | - const spinner = ora( |
60 | | - `${chalk.green('Attempting to download package:')}, ${pkgName}`, |
61 | | - ).start(); |
62 | | - |
63 | | - try { |
64 | | - codeshiftConfig = await fetchPackage( |
65 | | - getCodeshiftPackageName(pkgName), |
66 | | - packageManager, |
67 | | - ); |
68 | | - spinner.succeed( |
69 | | - `${chalk.green( |
70 | | - `Found CodeshiftCommunity package: `, |
71 | | - )} ${getCodeshiftPackageName(pkgName)}`, |
72 | | - ); |
73 | | - } catch (error) { |
74 | | - spinner.warn( |
75 | | - `${chalk.yellow( |
76 | | - `Unable to locate CodeshiftCommunity package: `, |
77 | | - )} ${getCodeshiftPackageName(pkgName)}`, |
78 | | - ); |
79 | | - } |
80 | | - |
81 | | - try { |
82 | | - remoteConfig = await fetchRemotePackage(pkgName, packageManager); |
83 | | - spinner.succeed( |
84 | | - `${chalk.green(`Found codeshift package: `)} ${pkgName}`, |
85 | | - ); |
86 | | - } catch (error) { |
87 | | - spinner.warn( |
88 | | - `${chalk.yellow(`Unable to locate codeshift package: `)} ${pkgName}`, |
89 | | - ); |
90 | | - } |
91 | | - |
92 | | - if (!codeshiftConfig && !remoteConfig) { |
93 | | - throw new Error( |
94 | | - `Unable to locate package from codeshift-community or NPM. |
95 | | -Make sure the package name "${pkgName}" is correct and try again.`, |
96 | | - ); |
97 | | - } |
98 | | - |
99 | | - const config: CodeshiftConfig = merge({}, remoteConfig, codeshiftConfig); |
100 | | - |
101 | | - if (!isValidConfig(config)) { |
102 | | - throw new Error( |
103 | | - `Unable to locate a valid codeshift.config in package ${pkgName}`, |
104 | | - ); |
105 | | - } |
| 48 | + const config = await fetchPackageConfig(pkgName, packageManager); |
106 | 49 |
|
107 | 50 | const rawTransformIds = pkg.split(/(?=[@#])/).filter(str => !!str); |
108 | 51 | rawTransformIds.shift(); |
|
0 commit comments