From d103a2d5f4d267081f997a176e9c3648b0a5f4c0 Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Tue, 24 Oct 2023 16:57:08 +0200 Subject: [PATCH] fix(app-builder): allow exempting commands from root command logic --- apps/app-builder/src/types/config.ts | 5 +++ .../src/utils/createCommand.spec.ts | 39 +++++++++++++++++++ apps/app-builder/src/utils/createCommand.ts | 20 +++++++++- .../src/utils/mergeDefaultConfig.ts | 1 + 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 apps/app-builder/src/utils/createCommand.spec.ts diff --git a/apps/app-builder/src/types/config.ts b/apps/app-builder/src/types/config.ts index e29086993..4468cf215 100644 --- a/apps/app-builder/src/types/config.ts +++ b/apps/app-builder/src/types/config.ts @@ -108,6 +108,11 @@ export type PdkBuilderConfig = { */ rootCommand?: OneOrMore; + /** + * Commands that should be run with the root command. Defaults to `['composer', 'php']`. + */ + rootCommands?: (string | RegExp)[]; + /** * Translations configuration. */ diff --git a/apps/app-builder/src/utils/createCommand.spec.ts b/apps/app-builder/src/utils/createCommand.spec.ts new file mode 100644 index 000000000..1c531056b --- /dev/null +++ b/apps/app-builder/src/utils/createCommand.spec.ts @@ -0,0 +1,39 @@ +import {describe, expect, it} from 'vitest'; +import {createTestContext} from '../__tests__/createTestContext'; +import {createCommand} from './createCommand'; + +describe('createCommand', () => { + it.each([ + ['foo', 'foo'], + ['foo bar', 'foo bar'], + [['foo'], 'foo'], + [['foo', 'bar'], 'foo bar'], + [['foo', 'bar', 'baz'], 'foo bar baz'], + ])('should return the command as a string', (command, expected) => { + const context = createTestContext(); + + expect(createCommand(context.config, command)).toEqual(expected); + }); + + it('should prepend the command with the root command', () => { + const context = createTestContext({ + config: { + rootCommand: 'root command', + rootCommands: ['foo'], + }, + }); + + expect(createCommand(context.config, 'foo')).toEqual('root command foo'); + }); + + it('should not prepend the command with the root command when the command is not in the rootCommands array', () => { + const context = createTestContext({ + config: { + rootCommand: 'root command', + rootCommands: [], + }, + }); + + expect(createCommand(context.config, 'bar')).toEqual('bar'); + }); +}); diff --git a/apps/app-builder/src/utils/createCommand.ts b/apps/app-builder/src/utils/createCommand.ts index 51d069809..de885485f 100644 --- a/apps/app-builder/src/utils/createCommand.ts +++ b/apps/app-builder/src/utils/createCommand.ts @@ -2,8 +2,24 @@ import {type OneOrMore, toArray} from '@myparcel/ts-utils'; import {type PdkBuilderConfig} from '../types'; export const createCommand = (config: PdkBuilderConfig, command: OneOrMore): string => { - const resolvedRootCommand = toArray(config.rootCommand ?? []); const resolvedCommand = toArray(command ?? []); + const [firstCommand] = resolvedCommand; - return [...resolvedRootCommand, ...resolvedCommand].filter(Boolean).join(' '); + const rootCommands = toArray(config.rootCommands ?? []); + + const present = rootCommands.some((rootCommand) => { + return typeof rootCommand === 'string' ? rootCommand === firstCommand : rootCommand.test(firstCommand); + }); + + const commandList = []; + + if (present) { + const resolvedRootCommand = toArray(config.rootCommand ?? []); + + commandList.push(...resolvedRootCommand); + } + + commandList.push(...resolvedCommand); + + return commandList.filter(Boolean).join(' '); }; diff --git a/apps/app-builder/src/utils/mergeDefaultConfig.ts b/apps/app-builder/src/utils/mergeDefaultConfig.ts index 993e0a1d6..cf6be4e30 100644 --- a/apps/app-builder/src/utils/mergeDefaultConfig.ts +++ b/apps/app-builder/src/utils/mergeDefaultConfig.ts @@ -14,6 +14,7 @@ export const mergeDefaultConfig = (config: PdkBuilderConfig): ResolvedPdkBuilder outDir: 'dist', platformFolderName: '{{platform}}-{{name}}', rootCommand: '', + rootCommands: ['composer', 'php'], version: '0.0.0', yarnCommand: undefined, ...config,