Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/rstack/src/configLayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,19 @@ export const resolveRslintConfig = async (
const configs = await resolveConfigLayers(layers, 'lint');
return configs.length > 1 ? configs.flat() : configs[0];
};

export const resolveStagedConfig = async (
layers: readonly Configs[],
): Promise<StagedConfig | undefined> => {
const configs = await resolveConfigLayers(layers, 'staged');
if (configs.length <= 1) {
return configs[0];
}

// Top-level task generators replace the whole config rather than glob keys.
return configs.reduce((merged, config) =>
typeof merged === 'function' || typeof config === 'function'
? config
: { ...merged, ...config },
);
};
5 changes: 3 additions & 2 deletions packages/rstack/src/staged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import lintStaged from 'lint-staged';
import { parseArgs } from './cli/args.ts';
import { printCommandHelp } from './cli/help.ts';
import { loadRstackConfig } from './config.ts';
import { resolveStagedConfig } from './configLayers.ts';

export type StagedSyncTaskGenerator = (
stagedFileNames: readonly string[],
Expand Down Expand Up @@ -51,8 +52,8 @@ export async function runStagedCLI(args: string[]): Promise<void> {
}

const { configs } = await loadRstackConfig();
const stagedConfig = configs.staged;
if (!stagedConfig) {
const stagedConfig = await resolveStagedConfig([configs]);
if (stagedConfig === undefined) {
throw new Error(
'No define.staged config found. Add define.staged({ "*": "your-command" }) to rstack config file',
);
Expand Down
50 changes: 50 additions & 0 deletions packages/rstack/tests/config/staged-merge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { expect, rs, test } from 'rstack/test';
import { resolveStagedConfig } from '../../src/configLayers.ts';

test('shallowly merges staged globs and replaces task arrays', async () => {
const generate = rs.fn(() => 'rs fmt');
const task = rs.fn();
const config = await resolveStagedConfig([
{
staged: {
'*.ts': ['rs lint', 'rs fmt'],
'*.md': generate,
'*.json': { title: 'Check JSON', task },
},
},
{ staged: { '*.ts': ['rs check'], '*.css': 'rs fmt' } },
]);

expect(config).toEqual({
'*.ts': ['rs check'],
'*.md': generate,
'*.json': { title: 'Check JSON', task },
'*.css': 'rs fmt',
});
expect(generate).not.toHaveBeenCalled();
expect(task).not.toHaveBeenCalled();
});

test('a top-level task generator replaces earlier staged config without running', async () => {
const generate = rs.fn(() => ['rs check']);
const config = await resolveStagedConfig([
{ staged: { '*.ts': 'rs lint' } },
{ staged: generate },
]);

expect(config).toBe(generate);
expect(generate).not.toHaveBeenCalled();
});

test('glob maps after a top-level task generator start a new staged config', async () => {
const generate = rs.fn(() => ['rs check']);
const config = await resolveStagedConfig([
{ staged: { '*.ts': 'rs lint' } },
{ staged: generate },
{ staged: { '*.md': 'rs fmt' } },
{ staged: { '*.css': 'rs fmt' } },
]);

expect(config).toEqual({ '*.md': 'rs fmt', '*.css': 'rs fmt' });
expect(generate).not.toHaveBeenCalled();
});
Loading