Skip to content

Commit 13f5d31

Browse files
committed
refactor: extract virtual-fs into utils
1 parent 7ca8e29 commit 13f5d31

12 files changed

Lines changed: 426 additions & 292 deletions

File tree

packages/create-cli/src/lib/setup/ci.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { select } from '@inquirer/prompts';
22
import { vol } from 'memfs';
33
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
4+
import { createTree } from '@code-pushup/utils';
45
import { promptCiProvider, resolveCi } from './ci.js';
56
import type { ConfigContext } from './types.js';
6-
import { createTree } from './virtual-fs.js';
77

88
vi.mock('@inquirer/prompts', () => ({
99
select: vi.fn(),

packages/create-cli/src/lib/setup/gitignore.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { vol } from 'memfs';
22
import { readFile } from 'node:fs/promises';
33
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
4+
import { createTree } from '@code-pushup/utils';
45
import { resolveGitignore } from './gitignore.js';
5-
import { createTree } from './virtual-fs.js';
66

77
describe('resolveGitignore', () => {
88
it('should create .gitignore with comment when it does not exist', async () => {

packages/create-cli/src/lib/setup/monorepo.unit.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { select } from '@inquirer/prompts';
22
import { vol } from 'memfs';
33
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
4-
import { logger } from '@code-pushup/utils';
4+
import { createTree, logger } from '@code-pushup/utils';
55
import { addCodePushUpCommand, promptSetupMode } from './monorepo.js';
66
import type { WizardProject } from './types.js';
7-
import { createTree } from './virtual-fs.js';
87

98
vi.mock('@inquirer/prompts', () => ({
109
select: vi.fn(),

packages/create-cli/src/lib/setup/types.ts

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { PluginCodegenResult } from '@code-pushup/models';
2-
import type { MonorepoTool } from '@code-pushup/utils';
2+
import type { MonorepoTool, Tree } from '@code-pushup/utils';
33

44
export type {
55
CategoryCodegenConfig,
@@ -12,6 +12,8 @@ export type {
1212
PluginSetupTree,
1313
} from '@code-pushup/models';
1414

15+
export type { FileChange, FileSystemAdapter, Tree } from '@code-pushup/utils';
16+
1517
export const CI_PROVIDERS = ['github', 'gitlab', 'none'] as const;
1618
export type CiProvider = (typeof CI_PROVIDERS)[number];
1719

@@ -59,31 +61,3 @@ export type WriteContext = {
5961
configFilename: string;
6062
isEsm: boolean;
6163
};
62-
63-
/** A single file operation recorded by the virtual tree. */
64-
export type FileChange = {
65-
path: string;
66-
type: 'CREATE' | 'UPDATE';
67-
content: string;
68-
};
69-
70-
/** Virtual file system that buffers writes in memory until flushed to disk. */
71-
export type Tree = {
72-
root: string;
73-
exists: (filePath: string) => Promise<boolean>;
74-
read: (filePath: string) => Promise<string | null>;
75-
write: (filePath: string, content: string) => Promise<void>;
76-
listChanges: () => FileChange[];
77-
flush: () => Promise<void>;
78-
};
79-
80-
/** Abstraction over `node:fs` used by the virtual tree for disk I/O. */
81-
export type FileSystemAdapter = {
82-
readFile: (path: string, encoding: 'utf8') => Promise<string>;
83-
writeFile: (path: string, content: string) => Promise<void>;
84-
exists: (path: string) => Promise<boolean>;
85-
mkdir: (
86-
path: string,
87-
options: { recursive: true },
88-
) => Promise<string | undefined>;
89-
};

packages/create-cli/src/lib/setup/virtual-fs.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

packages/create-cli/src/lib/setup/virtual-fs.unit.test.ts

Lines changed: 0 additions & 191 deletions
This file was deleted.

packages/create-cli/src/lib/setup/wizard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path';
33
import {
44
type MonorepoTool,
55
asyncSequential,
6+
createTree,
67
formatAsciiTable,
78
getGitRoot,
89
logger,
@@ -35,7 +36,6 @@ import type {
3536
Tree,
3637
WriteContext,
3738
} from './types.js';
38-
import { createTree } from './virtual-fs.js';
3939

4040
/**
4141
* Runs the interactive setup wizard that generates a Code PushUp config file.

packages/utils/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ export {
204204
answerNonEmptyArray,
205205
answerString,
206206
} from './lib/plugin-answers.js';
207+
export {
208+
createTree,
209+
type FileChange,
210+
type FileSystemAdapter,
211+
type Tree,
212+
} from './lib/wizard/index.js';
207213
export {
208214
hasCodePushUpDependency,
209215
hasDependency,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type { FileChange, FileSystemAdapter, Tree } from './types.js';
2+
export { createTree } from './virtual-fs.js';

0 commit comments

Comments
 (0)