11import path from 'path' ;
2- import fs from 'fs-extra' ;
32import semver from 'semver' ;
43import chalk from 'chalk' ;
54import findUp from 'find-up' ;
@@ -8,12 +7,12 @@ import { PluginManager, PluginManagerOptions } from 'live-plugin-manager';
87import { installPackage } from '@antfu/install-pkg' ;
98
109import * as core from '@hypermod/core' ;
11- import { Config } from '@hypermod/types' ;
12- import { fetchConfigAtPath , fetchConfigs } from '@hypermod/fetcher' ;
10+ import { fetchConfigAtPath } from '@hypermod/fetcher' ;
1311
1412import { InvalidUserInputError } from './errors' ;
1513import { fetchPackages } from './utils/fetch-package' ;
1614import { mergeConfigs } from './utils/merge-configs' ;
15+ import { fetchConfigsForWorkspaces , getPackageJson } from './utils/file-system' ;
1716import { getConfigPrompt , getMultiConfigPrompt } from './prompt' ;
1817
1918const ExperimentalModuleLoader = ( ) => ( {
@@ -65,27 +64,15 @@ export default async function main(
6564 ) ;
6665
6766 /**
68- * Attempt to locate a root package json with a workspaces config.
67+ * Attempt to locate a root package. json with a workspaces config.
6968 * If found, show a prompt with all available codemods
7069 */
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 ( ) ;
8871
72+ if ( localPackageJson && localPackageJson . workspaces ) {
73+ const configs = await fetchConfigsForWorkspaces (
74+ localPackageJson . workspaces ,
75+ ) ;
8976 const answers = await inquirer . prompt ( [ getMultiConfigPrompt ( configs ) ] ) ;
9077 const selectedConfig = configs . find (
9178 ( { filePath } ) => answers . codemod . filePath === filePath ,
0 commit comments