Skip to content

Commit

Permalink
fix(app-builder): allow exempting commands from root command logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 24, 2023
1 parent 3d6e039 commit d103a2d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions apps/app-builder/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ export type PdkBuilderConfig = {
*/
rootCommand?: OneOrMore<string>;

/**
* Commands that should be run with the root command. Defaults to `['composer', 'php']`.
*/
rootCommands?: (string | RegExp)[];

/**
* Translations configuration.
*/
Expand Down
39 changes: 39 additions & 0 deletions apps/app-builder/src/utils/createCommand.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
20 changes: 18 additions & 2 deletions apps/app-builder/src/utils/createCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>): 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(' ');
};
1 change: 1 addition & 0 deletions apps/app-builder/src/utils/mergeDefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d103a2d

Please sign in to comment.