Skip to content
Open
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
9 changes: 7 additions & 2 deletions packages/knip/src/plugins/moonrepo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '../../types/config.js';
import { hasDependency } from '../../util/plugin.js';
import { createCommandProcessor } from '../../util/string.js';
import type { MoonConfiguration } from './types.js';

// https://moonrepo.dev/docs
Expand All @@ -15,16 +16,20 @@ const isRootOnly = true;
const config = ['moon.yml', '.moon/tasks.yml', '.moon/tasks/*.yml'];

const resolveConfig: ResolveConfig<MoonConfiguration> = async (config, options) => {
const commandProcessor = createCommandProcessor({
'$projectRoot': options.cwd,
'$workspaceRoot': options.rootCwd,
})
const tasks = config.tasks ? Object.values(config.tasks) : [];
const inputs = tasks
.map(task => task.command)
.filter(command => command)
.map(command => command.replace('$workspaceRoot', options.rootCwd))
.map(command => command.replace('$projectRoot', options.cwd))
.map(commandProcessor)
.flatMap(command => options.getInputsFromScripts(command));
return [...inputs];
};


export default {
title,
enablers,
Expand Down
2 changes: 1 addition & 1 deletion packages/knip/src/plugins/moonrepo/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface MoonConfiguration {
tasks?: {
[taskName: string]: {
command: string;
command: string | string[];
};
};
}
42 changes: 42 additions & 0 deletions packages/knip/src/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,45 @@ export const prettyMilliseconds = (ms: number): string => {
if (minutes > 0) return `${minutes}m ${Math.floor(seconds % 60)}s`;
return seconds % 1 ? `${seconds.toFixed(1)}s` : `${Math.floor(seconds)}s`;
};

/**
* Template literal processor for commands that might be
* strings or arrays of strings.
*
* @example
* ```ts
* const processor = createCommandProcessor({ '$projectRoot': '/path/to/project' });
*
* console.log(processor('echo $projectRoot'));
* // 'echo /path/to/project'
*
* console.log(processor(['npx', 'tsx', '$projectRoot/server/worker.js', '--', '--force']));
* // ['npx', 'tsx', '/path/to/project/server/worker.js', '--', '--force']
* ```
*/
export const createCommandProcessor = <T extends object>(context: T) => {

const templater = (template: string):string => {
return template.replace(/\$(\w+)/g, (_, key) => {
const value = context[key as keyof T];
if (!value) {
return key;
}
return String(value);
});
}

const processor = <C extends string | string[]>(command: C): C => {
if (typeof command === 'string') {
return templater(command) as C;
}

if (Array.isArray(command)) {
return command.map(cmd => processor(cmd)) as C;
}

throw new TypeError('Command must be a string or an array of strings');
}

return processor
}