Skip to content

Commit

Permalink
refactor: clean up commands
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 31, 2024
1 parent 7c516e0 commit 8448ff8
Show file tree
Hide file tree
Showing 153 changed files with 1,446 additions and 1,240 deletions.
1 change: 1 addition & 0 deletions apps/app-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"inquirer": "^9.1.4",
"interpret": "^3.1.1",
"liftoff": "^5.0.0",
"radash": "^12.1.0",
"semver": "^7.5.4",
"supports-color": "^9.3.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ exports[`exports > exports from index.ts 1`] = `
"PdkPlatformName",
"addPlatformToContext",
"copyFile",
"createDirectories",
"defineConfig",
"deleteDirectory",
"deleteFile",
"executeCommand",
"executePerPlatform",
"executePromises",
"exists",
"getFileContents",
Expand Down
4 changes: 2 additions & 2 deletions apps/app-builder/src/__tests__/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'path';
import path from 'node:path';

const dirname = new URL('.', import.meta.url).pathname;

export const MOCK_ROOT_DIR = path.resolve(dirname, '../..', '.test');

export const DEFAULT_FILE_SYSTEM = Object.freeze({
export const DEFAULT_FILE_SYSTEM: Readonly<Record<string, unknown>> = Object.freeze({
config: {
'pdk.php': '<?php return [];',
},
Expand Down
5 changes: 2 additions & 3 deletions apps/app-builder/src/__tests__/createTestContext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {type RecursivePartial} from '@myparcel/ts-utils';
import {mergeDefaultConfig} from '../utils/mergeDefaultConfig';
import {createDebugger} from '../utils/command/createDebugger';
import {type PdkBuilderConfig} from '../types/config';
import {type CommandArgs, type PdkBuilderContext} from '../types/command';
import {type PdkBuilderConfig} from '../types/config.types';
import {type CommandArgs, type PdkBuilderContext} from '../types/command.types';
import {PdkPlatformName} from '../constants';
import {MOCK_ROOT_DIR} from './constants';

Expand All @@ -15,7 +15,6 @@ export const createTestContext = <Context extends PdkBuilderContext = PdkBuilder
parallel: false,
quiet: true,
verbose: 0,
version: '1.0.0',
...context?.args,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {type RecursivePartial} from '@myparcel/ts-utils';
import {type PdkBuilderContext} from '../types/command';
import {type PdkBuilderContext} from '../types/command.types';
import {createTestContext} from './createTestContext';

export const createTestContextWithMockedFs = <Context extends PdkBuilderContext = PdkBuilderContext>(
Expand Down
8 changes: 8 additions & 0 deletions apps/app-builder/src/__tests__/expectNoFileChanges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {expect} from 'vitest';
import {fsModifyingMethods} from './spies/fs';

export const expectNoFileChanges = (): void => {
fsModifyingMethods.forEach((spy) => {
expect(spy).not.toHaveBeenCalled();
});
};
14 changes: 8 additions & 6 deletions apps/app-builder/src/__tests__/mockFileSystem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import fs from 'fs';
import path from 'node:path';
import fs from 'node:fs';
import {type TaskContext, vi} from 'vitest';
import {isObject, merge} from 'lodash-unified';
import {assign, isObject} from 'radash';
import {exists} from '../utils/fs/exists';
import {DEFAULT_FILE_SYSTEM, MOCK_ROOT_DIR} from './constants';

Expand All @@ -27,11 +27,13 @@ const recursiveCreate = async (entries: Record<string, unknown>, rootDir: string
};

export const mockFileSystem = async (ctx: TaskContext, fileSystem?: Record<string, unknown>): Promise<string> => {
const rootDir = path.resolve(MOCK_ROOT_DIR, ctx.task.id);
const rootDir = path.resolve(MOCK_ROOT_DIR, `${ctx.task.name}-${ctx.task.id}`.replace(/\s/g, '-'));
const mergedFileSystem = assign(DEFAULT_FILE_SYSTEM, fileSystem ?? {});

await recursiveCreate(merge({}, DEFAULT_FILE_SYSTEM, fileSystem), rootDir);
await recursiveCreate(mergedFileSystem, rootDir);

vi.restoreAllMocks();
// Clear the calls to the filesystem methods before continuing
vi.clearAllMocks();

return rootDir;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type TaskContext} from 'vitest';
import {type RecursivePartial} from '@myparcel/ts-utils';
import {type PdkBuilderContext} from '../types/command';
import {type PdkBuilderContext} from '../types/command.types';
import {mockFileSystem} from './mockFileSystem';
import {createTestContextWithMockedFs} from './createTestContextWithMockedFs';

Expand Down
6 changes: 0 additions & 6 deletions apps/app-builder/src/__tests__/restoreFileSystem.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import fs from 'fs';
import {MOCK_ROOT_DIR} from './constants';
import fs from 'node:fs';
import {MOCK_ROOT_DIR} from '../constants';

const restoreFileSystem = async () => {
await fs.promises.rm(MOCK_ROOT_DIR, {recursive: true, force: true});
};

// noinspection JSUnusedGlobalSymbols
export const teardown = async (): Promise<void> => {
await restoreFileSystem();
};

// noinspection JSUnusedGlobalSymbols
export const setup = async (): Promise<void> => {
await restoreFileSystem();
};
17 changes: 17 additions & 0 deletions apps/app-builder/src/__tests__/setup/spyOnFs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fs from 'node:fs';
import {vi} from 'vitest';

vi.spyOn(fs, 'appendFile');
vi.spyOn(fs, 'copyFile');
vi.spyOn(fs, 'mkdir');
vi.spyOn(fs, 'rm');
vi.spyOn(fs, 'rmdir');
vi.spyOn(fs, 'unlink');
vi.spyOn(fs, 'writeFile');
vi.spyOn(fs.promises, 'appendFile');
vi.spyOn(fs.promises, 'copyFile');
vi.spyOn(fs.promises, 'mkdir');
vi.spyOn(fs.promises, 'rm');
vi.spyOn(fs.promises, 'rmdir');
vi.spyOn(fs.promises, 'unlink');
vi.spyOn(fs.promises, 'writeFile');
50 changes: 16 additions & 34 deletions apps/app-builder/src/__tests__/spies/fs.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
// make a spy for all methods on fs.promises that modify the file system
import fs from 'fs';
import {vi} from 'vitest';
import fs from 'node:fs';

const fsAppendFile = vi.spyOn(fs, 'appendFile');
const fsCopyFile = vi.spyOn(fs, 'copyFile');
const fsMkdir = vi.spyOn(fs, 'mkdir');
const fsRm = vi.spyOn(fs, 'rm');
const fsRmdir = vi.spyOn(fs, 'rmdir');
const fsUnlink = vi.spyOn(fs, 'unlink');
const fsWriteFile = vi.spyOn(fs, 'writeFile');

const fsPromisesAppendFile = vi.spyOn(fs.promises, 'appendFile');
const fsPromisesCopyFile = vi.spyOn(fs.promises, 'copyFile');
const fsPromisesMkdir = vi.spyOn(fs.promises, 'mkdir');
const fsPromisesRm = vi.spyOn(fs.promises, 'rm');
const fsPromisesRmdir = vi.spyOn(fs.promises, 'rmdir');
const fsPromisesUnlink = vi.spyOn(fs.promises, 'unlink');
const fsPromisesWriteFile = vi.spyOn(fs.promises, 'writeFile');

export const fsModifyingMethodSpies = [
fsAppendFile,
fsCopyFile,
fsMkdir,
fsRm,
fsRmdir,
fsUnlink,
fsWriteFile,
fsPromisesAppendFile,
fsPromisesCopyFile,
fsPromisesMkdir,
fsPromisesRm,
fsPromisesRmdir,
fsPromisesUnlink,
fsPromisesWriteFile,
export const fsModifyingMethods = [
fs.appendFile,
fs.copyFile,
fs.mkdir,
fs.rm,
fs.rmdir,
fs.unlink,
fs.writeFile,
fs.promises.appendFile,
fs.promises.copyFile,
fs.promises.mkdir,
fs.promises.rm,
fs.promises.rmdir,
fs.promises.unlink,
fs.promises.writeFile,
] as const;
15 changes: 6 additions & 9 deletions apps/app-builder/src/commands/clean.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
import {describe, expect, it} from 'vitest';
import {exists} from '../utils/fs/exists';
import {fsModifyingMethodSpies} from '../__tests__/spies/fs';
import {fsModifyingMethods} from '../__tests__/spies/fs';
import {mockFileSystemAndCreateContext} from '../__tests__/mockFileSystemAndCreateContext';
import {expectNoFileChanges} from '../__tests__/expectNoFileChanges';
import clean from './clean';

describe('command: clean', () => {
it('does nothing when dry run is passed', async (ctx) => {
expect.assertions(fsModifyingMethodSpies.length);
expect.assertions(fsModifyingMethods.length);

const context = await mockFileSystemAndCreateContext(ctx, {dist: {'text.txt': ''}}, {args: {dryRun: true}});

await clean(context);

fsModifyingMethodSpies.forEach((spy) => {
expect(spy).not.toHaveBeenCalled();
});
expectNoFileChanges();
});

it('does nothing when outDir does not exist', async (ctx) => {
expect.assertions(fsModifyingMethodSpies.length);
expect.assertions(fsModifyingMethods.length);

const context = await mockFileSystemAndCreateContext(ctx);

await clean(context);

fsModifyingMethodSpies.forEach((spy) => {
expect(spy).not.toHaveBeenCalled();
});
expectNoFileChanges();
});

it('cleans dir', async (ctx) => {
Expand Down
10 changes: 5 additions & 5 deletions apps/app-builder/src/commands/clean.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {rmDir} from '../utils/fs/rmDir';
import {type PdkBuilderCommand} from '../types/command';
import {deleteDirectory} from '../utils/fs/deleteDirectory';
import {type PdkBuilderCommand} from '../types/command.types';

const clean: PdkBuilderCommand = async (context) => {
await rmDir(context.config.outDir, context);
};
const clean = (async (context) => {
await deleteDirectory(context, context.config.outDir);
}) satisfies PdkBuilderCommand;

export default clean;
53 changes: 0 additions & 53 deletions apps/app-builder/src/commands/copy.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import path from 'path';
import fs from 'fs';
import copy from './index';
import path from 'node:path';
import fs from 'node:fs';
import {describe, expect, it} from 'vitest';
import glob from 'fast-glob';
import {PdkPlatformName} from '../constants';
import {fsModifyingMethodSpies} from '../__tests__/spies/fs';
import {mockFileSystemAndCreateContext} from '../__tests__/mockFileSystemAndCreateContext';
import copy from './copy';
import {PdkPlatformName} from '../../constants';
import {fsModifyingMethods} from '../../__tests__/spies/fs';
import {mockFileSystemAndCreateContext} from '../../__tests__/mockFileSystemAndCreateContext';
import {expectNoFileChanges} from '../../__tests__/expectNoFileChanges';

describe('command: copy', () => {
it('does nothing when dry run is passed', async (ctx) => {
expect.assertions(fsModifyingMethodSpies.length);
expect.assertions(fsModifyingMethods.length);

const context = await mockFileSystemAndCreateContext(ctx, {dist: {'text.txt': ''}}, {args: {dryRun: true}});

await copy(context);

fsModifyingMethodSpies.forEach((spy) => {
expect(spy).not.toHaveBeenCalled();
});
expectNoFileChanges();
});

it('copies files', async (ctx) => {
Expand Down
24 changes: 24 additions & 0 deletions apps/app-builder/src/commands/copy/copyForPlatform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {resolvePath} from '../../utils/resolvePath';
import {copyFile} from '../../utils/fs/copyFile';
import {logTargetPath} from '../../utils/debug/logTargetPath';
import {type PdkBuilderContextWithPlatformArgs} from '../../types/command.types';

export const copyForPlatform = async (
context: PdkBuilderContextWithPlatformArgs,
sourceFiles: string[],
): Promise<void> => {
const {args, debug} = context;

debug('Copying files to %s', logTargetPath(args.platformOutDir, context));

await Promise.all(
sourceFiles.map(async (file) => {
const source = resolvePath(file, context);
const target = resolvePath([args.platformOutDir, file], context);

await copyFile(source, target, context);
}),
);

debug('Finished copying files to %s', logTargetPath(args.platformOutDir, context));
};
24 changes: 24 additions & 0 deletions apps/app-builder/src/commands/copy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {usesPhpScoper} from '../../utils/usesPhpScoper';
import {globFiles} from '../../utils/globFiles';
import {copyScopedFilesForPlatform} from '../../utils/copyScopedFilesForPlatform';
import {executePerPlatform} from '../../utils/command/executePerPlatform';
import {type PdkBuilderCommand} from '../../types/command.types';
import {copyForPlatform} from './copyForPlatform';

const copy = (async (context) => {
const {debug, config} = context;

const files = globFiles(config.source, context).sort();

await executePerPlatform(context, (context) => copyForPlatform(context, files));

debug('Finished copying files');

if (await usesPhpScoper(context)) {
await executePerPlatform(context, copyScopedFilesForPlatform);

debug('Finished copying scoped files');
}
}) satisfies PdkBuilderCommand;

export default copy;
Loading

0 comments on commit 8448ff8

Please sign in to comment.