From 91a2b4890041b926c7d45dcfbcc268edb48bc695 Mon Sep 17 00:00:00 2001 From: zdm Date: Tue, 31 Dec 2024 00:24:48 +0200 Subject: [PATCH] chore: refactor lint file results --- lib/lint/file.js | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/lib/lint/file.js b/lib/lint/file.js index c6c1c822..7b497534 100644 --- a/lib/lint/file.js +++ b/lib/lint/file.js @@ -257,46 +257,47 @@ export class LintFile { constructor ( file, { cwd, processUnsupportedTypes, write, fix = true, log = true, cache } = {} ) { this.#file = File.new( file ); + this.#cwd = cwd; this.#processUnsupportedTypes = Boolean( processUnsupportedTypes ); this.#write = Boolean( write ); this.#cache = cache || {}; this.#fix = Boolean( fix ); this.#log = Boolean( log ); + } + + // properties + get cwd () { + return this.#cwd; + } + get path () { + return this.#path; + } + + // public + async run ( action ) { if ( !this.#file.path ) { - throw new Error( "File path is required" ); + return result( [ 500, "File path is required" ] ); } if ( path.isAbsolute( this.#file.path ) ) { this.#path = path.normalize( this.#file.path ); } else { - if ( !cwd ) { - throw new Error( "Cwd is required" ); + if ( !this.#cwd ) { + return result( [ 500, "Cwd is required when file path is relative" ] ); } - this.#path = path.normalize( path.join( cwd, this.#file.path ) ); + this.#path = path.normalize( path.join( this.#cwd, this.#file.path ) ); } this.#cwd = path.dirname( this.#path ); - } - - // properties - get cwd () { - return this.#cwd; - } - get path () { - return this.#path; - } - - // public - async run ( action ) { - if ( this.#write && !this.path ) { - throw new Error( "Path is required" ); + if ( this.#write && !this.#path ) { + return result( [ 500, "File path is required to write content" ] ); } - this.#fullPath = this.path; + this.#fullPath = this.#path; // read file content this.#originalData = this.#data = await this.#file.text( "utf8" ); @@ -321,7 +322,7 @@ export class LintFile { if ( this.#type ) { const extnames = customMime.get( this.#type.type )?.extnames; - if ( extnames?.default && !extnames.has( path.extname( this.path ) ) ) { + if ( extnames?.default && !extnames.has( path.extname( this.#path ) ) ) { this.#fullPath += extnames.default; } } @@ -340,7 +341,7 @@ export class LintFile { res.meta.bytesDelta = this.#data.length - this.#originalData.length; if ( this.#write ) { - await fs.promises.writeFile( this.path, this.#data ); + await fs.promises.writeFile( this.#path, this.#data ); } } else { @@ -499,7 +500,7 @@ export class LintFile { this.#cache.prettierConfigs ||= {}; if ( !this.#cache.prettierConfigs[ configs.prettier ] ) { - this.#cache.prettierConfigs[ configs.prettier ] = await prettier.resolveConfig( this.path, { + this.#cache.prettierConfigs[ configs.prettier ] = await prettier.resolveConfig( this.#path, { "config": configs.prettier, "editorconfig": false, } ); @@ -541,7 +542,7 @@ export class LintFile { try { // pre-parse package.json - if ( path.basename( this.path ) === "package.json" ) { + if ( path.basename( this.#path ) === "package.json" ) { const res = this.#runJson( "compress" ); if ( !res.ok ) return res;