Skip to content

Commit c4d77a6

Browse files
committed
feat: add lint trim option
1 parent 08d8de5 commit c4d77a6

3 files changed

Lines changed: 28 additions & 21 deletions

File tree

lib/lint/file.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Table from "#core/text/table";
1515
import * as utils from "#core/utils";
1616
import uuid from "#core/uuid";
1717
import Markdown from "#lib/markdown";
18+
import { getCliConfig } from "#lib/utils";
1819

1920
// NOTE: status codes:
2021
const STATUSES = {
@@ -411,9 +412,7 @@ export class LintFile {
411412
// private
412413
async #lint () {
413414
const configs = await this.#getConfigs( this.#cwd ),
414-
cliConfig = configs.cli
415-
? await this.#getCliConfig( configs.cli )
416-
: null,
415+
cliConfig = this.#getCliConfig( configs.cli ),
417416
editorConfig = await this.#getEditorConfig( {
418417
"fallbackToDefault": configs.package
419418
? Boolean( configs.cli )
@@ -1184,15 +1183,13 @@ export class LintFile {
11841183
}
11851184
}
11861185

1187-
async #getCliConfig ( path ) {
1186+
#getCliConfig ( path ) {
11881187
if ( !path ) return;
11891188

11901189
this.#cache.cliConfigs ||= new Map();
11911190

11921191
if ( !this.#cache.cliConfigs.has( path ) ) {
1193-
this.#cache.cliConfigs.set( path, null );
1194-
1195-
// XXX
1192+
this.#cache.cliConfigs.set( path, getCliConfig( path ) );
11961193
}
11971194

11981195
return this.#cache.cliConfigs.get( path );

lib/package.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import "#core/temporal";
22
import childProcess from "node:child_process";
33
import fs from "node:fs";
44
import path from "node:path";
5-
import Ajv from "#core/ajv";
65
import ansi from "#core/ansi";
76
import GitHubApi from "#core/api/github";
87
import { readConfig, readConfigSync, writeConfigSync } from "#core/config";
@@ -15,17 +14,15 @@ import GlobPatterns from "#core/glob/patterns";
1514
import Locale from "#core/locale";
1615
import SemanticVersion from "#core/semantic-version";
1716
import Table from "#core/text/table";
18-
import { confirm, mergeObjects, objectIsEmpty, repeatAction } from "#core/utils";
17+
import { confirm, objectIsEmpty, repeatAction } from "#core/utils";
1918
import yaml from "#core/yaml";
2019
import Git from "#lib/git";
2120
import lintFile from "#lib/lint/file";
2221
import Dependencies from "#lib/package/dependencies";
2322
import Docs from "#lib/package/docs";
2423
import Localization from "#lib/package/localization";
2524
import Wiki from "#lib/package/wiki";
26-
27-
const validateCliConfig = new Ajv().compileFile( import.meta.resolve( "#resources/schemas/cli.config.schema.yaml" ) ),
28-
defaultCliConfig = await readConfig( "#resources/cli.config.yaml", { "resolve": import.meta.url } );
25+
import { getCliConfig } from "#lib/utils";
2926

3027
export default class Package {
3128
#root;
@@ -153,15 +150,9 @@ export default class Package {
153150

154151
get cliConfig () {
155152
if ( this.#cliConfig === undefined ) {
156-
this.#cliConfig = null;
157-
158-
if ( fs.existsSync( this.root + "/cli.config.yaml" ) ) {
159-
const cliConfig = mergeObjects( {}, defaultCliConfig, readConfigSync( this.root + "/cli.config.yaml" ) );
160-
161-
if ( !validateCliConfig( cliConfig ) ) throw `CLI config is not valid:\n${ validateCliConfig.errors }`;
162-
163-
this.#cliConfig = cliConfig;
164-
}
153+
this.#cliConfig = getCliConfig( this.root + "/cli.config.yaml", {
154+
"validate": true,
155+
} );
165156
}
166157

167158
return this.#cliConfig;

lib/utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
import fs from "node:fs";
2+
import Ajv from "#core/ajv";
13
import ansi from "#core/ansi";
4+
import { readConfig, readConfigSync } from "#core/config";
25
import env from "#core/env";
36
import { globSync } from "#core/glob";
47
import GlobPatterns from "#core/glob/patterns";
58
import Table from "#core/text/table";
9+
import { mergeObjects } from "#core/utils";
610
import Package from "#lib/package";
711

812
env.loadUserEnv();
913

14+
const validateCliConfig = new Ajv().compileFile( import.meta.resolve( "#resources/schemas/cli.config.schema.yaml" ) ),
15+
defaultCliConfig = await readConfig( "#resources/cli.config.yaml", { "resolve": import.meta.url } );
16+
1017
export function getLintReportTable ( options = {} ) {
1118
return new Table( {
1219
"style": "borderless",
@@ -89,3 +96,15 @@ export function findWorkspacePackages ( { patterns, git = true, "package": isPac
8996

9097
return result( 200, packages );
9198
}
99+
100+
export function getCliConfig ( path, { validate = true } = {} ) {
101+
if ( !fs.existsSync( path ) ) return null;
102+
103+
const cliConfig = mergeObjects( {}, defaultCliConfig, readConfigSync( path ) );
104+
105+
if ( validate ) {
106+
if ( !validateCliConfig( cliConfig ) ) throw `CLI config is not valid:\n${ validateCliConfig.errors }`;
107+
}
108+
109+
return cliConfig;
110+
}

0 commit comments

Comments
 (0)