1
1
import path from 'path' ;
2
- import fs from 'fs-extra' ;
3
2
import semver from 'semver' ;
4
3
import chalk from 'chalk' ;
5
4
import findUp from 'find-up' ;
@@ -8,12 +7,12 @@ import { PluginManager, PluginManagerOptions } from 'live-plugin-manager';
8
7
import { installPackage } from '@antfu/install-pkg' ;
9
8
10
9
import * as core from '@hypermod/core' ;
11
- import { Config } from '@hypermod/types' ;
12
- import { fetchConfigAtPath , fetchConfigs } from '@hypermod/fetcher' ;
10
+ import { fetchConfigAtPath } from '@hypermod/fetcher' ;
13
11
14
12
import { InvalidUserInputError } from './errors' ;
15
13
import { fetchPackages } from './utils/fetch-package' ;
16
14
import { mergeConfigs } from './utils/merge-configs' ;
15
+ import { fetchConfigsForWorkspaces , getPackageJson } from './utils/file-system' ;
17
16
import { getConfigPrompt , getMultiConfigPrompt } from './prompt' ;
18
17
19
18
const ExperimentalModuleLoader = ( ) => ( {
@@ -65,27 +64,15 @@ export default async function main(
65
64
) ;
66
65
67
66
/**
68
- * Attempt to locate a root package json with a workspaces config.
67
+ * Attempt to locate a root package. json with a workspaces config.
69
68
* If found, show a prompt with all available codemods
70
69
*/
71
- let rootPackageJson : any ;
72
- const packageJsonPath = await findUp ( 'package.json' ) ;
73
-
74
- if ( packageJsonPath ) {
75
- const packageJsonRaw = await fs . readFile ( packageJsonPath , 'utf8' ) ;
76
- rootPackageJson = JSON . parse ( packageJsonRaw ) ;
77
- }
78
-
79
- if ( rootPackageJson && rootPackageJson . workspaces ) {
80
- const configs = await ( rootPackageJson . workspaces as string [ ] ) . reduce <
81
- Promise < { filePath : string ; config : Config } [ ] >
82
- > ( async ( accum , filePath ) => {
83
- const configs = await fetchConfigs ( filePath ) ;
84
- if ( ! configs . length ) return accum ;
85
- const results = await accum ;
86
- return [ ...results , ...configs ] ;
87
- } , Promise . resolve ( [ ] ) ) ;
70
+ const localPackageJson = await getPackageJson ( ) ;
88
71
72
+ if ( localPackageJson && localPackageJson . workspaces ) {
73
+ const configs = await fetchConfigsForWorkspaces (
74
+ localPackageJson . workspaces ,
75
+ ) ;
89
76
const answers = await inquirer . prompt ( [ getMultiConfigPrompt ( configs ) ] ) ;
90
77
const selectedConfig = configs . find (
91
78
( { filePath } ) => answers . codemod . filePath === filePath ,
0 commit comments