Simplify MasterRequest.getFilesFromArgs options
MODIFY
packages/@romejs/core/master/MasterRequest.ts
packages/@romejs/core/master/MasterRequest.ts
@@ -22,7 +22,10 @@ import {
createBlessedDiagnosticMessage,
} from '@romejs/diagnostics';
import {DiagnosticsPrinterFlags} from '@romejs/cli-diagnostics';
-import {ProjectDefinition} from '@romejs/project';
+import {
+ ProjectDefinition,
+ ProjectConfigCategoriesWithIgnoreAndEnabled,
+} from '@romejs/project';
import {ResolverOptions} from './fs/Resolver';
import {BundlerConfig} from '../common/types/bundler';
import MasterBridge, {
@@ -168,8 +171,12 @@ export default class MasterRequest {
if (min === max) {
if (args.length !== min) {
+ if (min === 0) {
+ message = `Expected no arguments`;
+ } else {
message = `Expected exactly <number emphasis>${min}</number> arguments`;
}
+ }
} else {
if (args.length < min) {
message = `Expected at least <number emphasis>${min}</number> arguments`;
@@ -280,14 +287,16 @@ export default class MasterRequest {
}
async getFilesFromArgs(
- globOpts:
- & MemoryFSGlobOptions
+ opts:
+ & Omit<MemoryFSGlobOptions, 'getProjectIgnore' | 'getProjectEnabled'>
& {
+ ignoreProjectIgnore?: boolean;
disabledDiagnosticCategory?: DiagnosticCategory;
advice?: DiagnosticAdvice;
- configCategory?: string;
+ configCategory?: ProjectConfigCategoriesWithIgnoreAndEnabled;
verb?: string;
noun?: string;
+ args?: Array<string>;
} = {},
): Promise<{
projects: Set<ProjectDefinition>;
@@ -297,14 +306,15 @@ export default class MasterRequest {
const {flags} = this.client;
const projects: Set<ProjectDefinition> = new Set();
- // Build up args, defaulting to the current project dir if none passed
- const rawArgs = [...this.query.args];
+ const rawArgs = opts.args === undefined ? this.query.args : opts.args;
const resolvedArgs: Array<{
path: AbsoluteFilePath;
location: DiagnosticLocation;
project: ProjectDefinition;
}> = [];
- if (rawArgs.length === 0) {
+
+ // If args was explicitly provided then don't assume empty args is the project root
+ if (rawArgs.length === 0 && opts.args === undefined) {
const location = this.getDiagnosticPointerForClientCwd();
const project = await this.assertClientCwdProject();
resolvedArgs.push({
@@ -343,10 +353,22 @@ export default class MasterRequest {
}
}
+ const {configCategory, ignoreProjectIgnore} = opts;
+
+ const extendedGlobOpts: MemoryFSGlobOptions = {...opts};
+
+ if (configCategory !== undefined) {
+ extendedGlobOpts.getProjectEnabled = (project) =>
+ project.config[configCategory].enabled;
+
+ extendedGlobOpts.getProjectIgnore = (project) =>
+ ignoreProjectIgnore ? [] : project.config[configCategory].ignore;
+ }
+
// Build up files
const paths: AbsoluteFilePathSet = new AbsoluteFilePathSet();
for (const {path, location, project} of resolvedArgs) {
- const matches = master.memoryFs.glob(path, globOpts);
+ const matches = master.memoryFs.glob(path, extendedGlobOpts);
if (matches.size > 0) {
for (const path of matches) {
@@ -357,30 +379,33 @@ export default class MasterRequest {
let category: DiagnosticCategory = 'args/fileNotFound';
- let advice: DiagnosticAdvice = globOpts.advice === undefined ? [] : [
- ...globOpts.advice,
+ let advice: DiagnosticAdvice = opts.advice === undefined ? [] : [
+ ...opts.advice,
];
// Hint if `path` failed `globOpts.test`
- if (globOpts.getProjectEnabled !== undefined) {
- const test = globOpts.getProjectEnabled(project);
-
- if (!test.enabled && test.source !== undefined) {
- const testSource = test.source;
+ if (configCategory !== undefined && !project.config[configCategory].enabled) {
+ const enabledSource = master.projectManager.findProjectConfigConsumer(
+ project,
+ (consumer) =>
+ consumer.has(configCategory) && consumer.get(configCategory).get(
+ 'enabled',
+ ),
+ );
- const explanationPrefix = globOpts.verb === undefined
- ? 'Files excluded' : `Files excluded from ${globOpts.verb}`;
+ const explanationPrefix = opts.verb === undefined
+ ? 'Files excluded' : `Files excluded from ${opts.verb}`;
- if (globOpts.disabledDiagnosticCategory !== undefined) {
- category = globOpts.disabledDiagnosticCategory;
+ if (opts.disabledDiagnosticCategory !== undefined) {
+ category = opts.disabledDiagnosticCategory;
}
- if (testSource.value === undefined) {
+ if (enabledSource.value === undefined) {
let explanation =
`${explanationPrefix} as it's not enabled for this project`;
- if (globOpts.configCategory !== undefined) {
+ if (configCategory !== undefined) {
explanation +=
- `. Run <command>rome config enable-category ${globOpts.configCategory}</command> to enable it.`;
+ `. Run <command>rome config enable-category ${configCategory}</command> to enable it.`;
}
advice.push({
type: 'log',
@@ -394,7 +419,7 @@ export default class MasterRequest {
message: `${explanationPrefix} as it's explicitly disabled in this project config`,
});
- const disabledPointer = testSource.value.getDiagnosticLocation(
+ const disabledPointer = enabledSource.value.getDiagnosticLocation(
'value',
);
advice.push({
@@ -403,15 +428,12 @@ export default class MasterRequest {
});
}
}
- }
// Hint if all files were ignored
- if (globOpts.getProjectIgnore !== undefined) {
- const ignore = globOpts.getProjectIgnore(project);
-
+ if (configCategory !== undefined) {
const {paths: withoutIgnore} = await this.getFilesFromArgs({
- ...globOpts,
- getProjectIgnore: undefined,
+ ...opts,
+ ignoreProjectIgnore: true,
});
if (withoutIgnore.size > 0) {
@@ -429,8 +451,16 @@ export default class MasterRequest {
truncate: true,
});
- if (ignore.source !== undefined && ignore.source.value !== undefined) {
- const ignorePointer = ignore.source.value.getDiagnosticLocation(
+ const ignoreSource = master.projectManager.findProjectConfigConsumer(
+ project,
+ (consumer) =>
+ consumer.has(configCategory) && consumer.get(configCategory).get(
+ 'ignore',
+ ),
+ );
+
+ if (ignoreSource.value !== undefined) {
+ const ignorePointer = ignoreSource.value.getDiagnosticLocation(
'value',
);
@@ -452,8 +482,8 @@ export default class MasterRequest {
location,
description: {
category,
- message: createBlessedDiagnosticMessage(globOpts.noun === undefined
- ? 'No files found' : `No files to ${globOpts.noun} found`
+ message: createBlessedDiagnosticMessage(opts.noun === undefined
+ ? 'No files found' : `No files to ${opts.noun} found`
),
advice,
},

MODIFY
packages/@romejs/core/master/commands/format.ts
packages/@romejs/core/master/commands/format.ts
@@ -25,29 +25,9 @@ export default createMasterCommand({
},
async default(req: MasterRequest, flags: Flags): Promise<void> {
- const {reporter, master} = req;
+ const {reporter} = req;
const {paths} = await req.getFilesFromArgs({
- getProjectIgnore: (project) =>
- ({
- patterns: project.config.format.ignore,
- source: master.projectManager.findProjectConfigConsumer(project, (
- consumer,
- ) =>
- consumer.has('format') && consumer.get('format').has('ignore')
- ? consumer.get('format').get('ignore') : undefined
- ),
- }),
- getProjectEnabled: (project) =>
- ({
- enabled: project.config.format.enabled,
- source: master.projectManager.findProjectConfigConsumer(project, (
- consumer,
- ) =>
- consumer.has('format')
- ? consumer.get('format').get('enabled') : undefined
- ),
- }),
noun: 'formatting',
verb: 'linting',
configCategory: 'format',

MODIFY
packages/@romejs/core/master/commands/lint.ts
packages/@romejs/core/master/commands/lint.ts
import {MasterRequest} from '@romejs/core';
import {createMasterCommand} from '../../commands';
-import Linter from '../linter/Linter';
+import Linter, {LinterOptions} from '../linter/Linter';
import {commandCategories} from '../../commands';
import {Consumer} from '@romejs/consume';
-import {DiagnosticLocation} from '@romejs/diagnostics';
-type Flags = {fix: boolean};
+type Flags = {
+ fix: boolean;
+ changes: undefined | string;
+};
export default createMasterCommand<Flags>({
category: commandCategories.CODE_QUALITY,
@@ -21,21 +23,51 @@ export default createMasterCommand<Flags>({
defineFlags(consumer: Consumer): Flags {
return {
fix: consumer.get('fix').asBoolean(false),
+ changes: consumer.get('changes').asStringOrVoid(),
};
},
async default(req: MasterRequest, flags: Flags): Promise<void> {
- const fix = flags.fix === false
+ const {reporter} = req;
+
+ const fixLocation = flags.fix === false
? undefined : req.getDiagnosticPointerFromFlags({
type: 'flag',
key: 'fix',
});
+ // Look up arguments manually in vsc if we were passed a changes branch
+ let args;
+ if (flags.changes !== undefined) {
+ // No arguments expected when using this flag
+ req.expectArgumentLength(0);
+
+ const client = await req.master.projectManager.getVCSClient(
+ await req.assertClientCwdProject(),
+ );
+ args = await client.getModifiedFiles(flags.changes);
+
+ if (args.length === 0) {
+ reporter.warn(
+ `No files changed from <emphasis>${flags.changes}</emphasis>`,
+ );
+ } else {
+ reporter.info(`Files changed from <emphasis>${flags.changes}</emphasis>`);
+ reporter.list(args.map((arg) => `<filelink target="${arg}" />`));
+ reporter.hr();
+ }
+ }
+
+ const opts: LinterOptions = {
+ fixLocation,
+ args,
+ };
+
return new Promise((resolve, reject) => {
if (req.query.requestFlags.watch) {
- initWatchLint(req, fix, reject);
+ initWatchLint(req, opts, reject);
} else {
- resolve(runLint(req, fix));
+ resolve(runLint(req, opts));
}
});
},
@@ -43,7 +75,7 @@ export default createMasterCommand<Flags>({
function initWatchLint(
req: MasterRequest,
- fix: undefined | DiagnosticLocation,
+ opts: LinterOptions,
reject: (err: Error) => void,
) {
const {master, reporter} = req;
@@ -71,7 +103,7 @@ function initWatchLint(
running = true;
reporter.clear();
- runLint(req, fix).then(() => {
+ runLint(req, opts).then(() => {
running = false;
if (runAgainAfterComplete) {
@@ -105,10 +137,7 @@ function initWatchLint(
runWatchLint();
}
-async function runLint(
- req: MasterRequest,
- fix: undefined | DiagnosticLocation,
-): Promise<void> {
- const linter = new Linter(req, fix);
+async function runLint(req: MasterRequest, opts: LinterOptions): Promise<void> {
+ const linter = new Linter(req, opts);
await linter.lint();
}

MODIFY
packages/@romejs/core/master/commands/test.ts
packages/@romejs/core/master/commands/test.ts
@@ -34,29 +34,9 @@ export default createMasterCommand({
},
async default(req: MasterRequest, commandFlags: Flags): Promise<void> {
- const {reporter, master} = req;
+ const {reporter} = req;
const {paths} = await req.getFilesFromArgs({
- getProjectIgnore: (project) =>
- ({
- patterns: project.config.tests.ignore,
- source: master.projectManager.findProjectConfigConsumer(project, (
- consumer,
- ) =>
- consumer.has('tests') && consumer.get('tests').has('ignore')
- ? consumer.get('tests').get('ignore') : undefined
- ),
- }),
- getProjectEnabled: (project) =>
- ({
- enabled: project.config.tests.enabled,
- source: master.projectManager.findProjectConfigConsumer(project, (
- consumer,
- ) =>
- consumer.has('tests')
- ? consumer.get('tests').get('enabled') : undefined
- ),
- }),
test: (path) => path.hasExtension('test'),
noun: 'test',
verb: 'testing',

MODIFY
packages/@romejs/core/master/fs/MemoryFileSystem.ts
packages/@romejs/core/master/fs/MemoryFileSystem.ts
@@ -40,7 +40,6 @@ import {
getFileHandler,
getFileHandlerExtensions,
} from '../../common/fileHandlers';
-import {ProjectConfigSource} from '../project/ProjectManager';
const DEFAULT_DENYLIST = ['.hg', '.git'];
@@ -94,14 +93,8 @@ export type WatcherClose = () => void;
export type MemoryFSGlobOptions = {
extensions?: Array<string>;
overrideIgnore?: PathPatterns;
- getProjectIgnore?: (project: ProjectDefinition) => {
- patterns: PathPatterns;
- source?: ProjectConfigSource;
- };
- getProjectEnabled?: (project: ProjectDefinition) => {
- enabled: boolean;
- source?: ProjectConfigSource;
- };
+ getProjectIgnore?: (project: ProjectDefinition) => PathPatterns;
+ getProjectEnabled?: (project: ProjectDefinition) => boolean;
test?: (path: AbsoluteFilePath) => boolean;
};
@@ -891,10 +884,7 @@ export default class MemoryFileSystem {
if (getProjectIgnore !== undefined) {
const projectIgnore = ignoresByProject.get(project);
if (projectIgnore === undefined) {
- ignore = concatGlobIgnore([
- ...ignore,
- ...getProjectIgnore(project).patterns,
- ]);
+ ignore = concatGlobIgnore([...ignore, ...getProjectIgnore(project)]);
ignoresByProject.set(project, ignore);
} else {
ignore = projectIgnore;
@@ -910,8 +900,7 @@ export default class MemoryFileSystem {
// Add if a matching file
if (this.files.has(path) && ignoreMatched === 'NO_MATCH') {
- if (getProjectEnabled !== undefined &&
- !getProjectEnabled(project).enabled) {
+ if (getProjectEnabled !== undefined && !getProjectEnabled(project)) {
continue;
}

Simplify MasterRequest.getFilesFromArgs options