Skip to content

Commit 10cf791

Browse files
author
Daniel Del Core
committed
moves some functions into utils
1 parent 121a627 commit 10cf791

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

.changeset/kind-flies-smile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hypermod/cli': patch
3+
---
4+
5+
Minor internal refactor.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"predocs:start": "ts-node scripts/docs",
1111
"docs:start": "cd website && yarn start",
12-
"postinstall": "yarn monorepo:check",
12+
"postinstall": "yarn monorepo:fix && yarn monorepo:check",
1313
"build": "yarn build:pkgs && yarn build:community",
1414
"build:pkgs": "tsc --build tsconfig.packages.json",
1515
"build:pkgs:watch": "tsc --build --watch tsconfig.packages.json",

packages/cli/src/main.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import path from 'path';
2-
import fs from 'fs-extra';
32
import semver from 'semver';
43
import chalk from 'chalk';
54
import findUp from 'find-up';
@@ -8,12 +7,12 @@ import { PluginManager, PluginManagerOptions } from 'live-plugin-manager';
87
import { installPackage } from '@antfu/install-pkg';
98

109
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';
1311

1412
import { InvalidUserInputError } from './errors';
1513
import { fetchPackages } from './utils/fetch-package';
1614
import { mergeConfigs } from './utils/merge-configs';
15+
import { fetchConfigsForWorkspaces, getPackageJson } from './utils/file-system';
1716
import { getConfigPrompt, getMultiConfigPrompt } from './prompt';
1817

1918
const 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,

packages/cli/src/utils/file-system.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fs from 'fs-extra';
2+
import findUp from 'find-up';
3+
4+
import { fetchConfigs } from '@hypermod/fetcher';
5+
import { Config } from '@hypermod/types';
6+
7+
export async function getPackageJson() {
8+
let rootPackageJson: any;
9+
const packageJsonPath = await findUp('package.json');
10+
11+
if (packageJsonPath) {
12+
const packageJsonRaw = await fs.readFile(packageJsonPath, 'utf8');
13+
rootPackageJson = JSON.parse(packageJsonRaw);
14+
}
15+
16+
return rootPackageJson;
17+
}
18+
19+
export async function fetchConfigsForWorkspaces(workspaces: string[]) {
20+
return await workspaces.reduce<
21+
Promise<{ filePath: string; config: Config }[]>
22+
>(async (accum, filePath) => {
23+
const configs = await fetchConfigs(filePath);
24+
if (!configs.length) return accum;
25+
const results = await accum;
26+
return [...results, ...configs];
27+
}, Promise.resolve([]));
28+
}

0 commit comments

Comments
 (0)