1- import { ALL_TOOLS } from "./tools" ;
2- import type { CheckOptions , Tool , Problem , ToolDefinition } from "./types" ;
3- import {
4- bold ,
5- cyan ,
6- debug as debugLog ,
7- dim ,
8- humanMs ,
9- isDebug ,
10- red ,
11- yellow ,
12- } from "./utils" ;
13- import { createTaskList } from "./tasklist" ;
1+ import { readFile } from "node:fs/promises" ;
142import { relative , resolve , sep , join } from "node:path" ;
3+
154import { isCI } from "ci-info" ;
16- import { readFile } from "node:fs/promises" ;
5+
6+ import { createTaskList } from "./tasklist" ;
7+ import { ALL_TOOLS } from "./tools" ;
8+ import type { CheckOptions , Tool , Problem , ToolDefinition } from "./types" ;
9+ import { bold , cyan , debug as debugLog , dim , humanMs , isDebug , red , yellow } from "./utils" ;
1710
1811export type * from "./types" ;
1912
2013export async function check ( options : CheckOptions = { } ) : Promise < never > {
2114 const { debug, fix = ! isCI , root = process . cwd ( ) } = options ;
22- const packageJson = JSON . parse (
23- await readFile ( join ( root , "package.json" ) , "utf8" ) ,
24- ) ;
15+ const packageJson = JSON . parse ( await readFile ( join ( root , "package.json" ) , "utf8" ) ) ;
2516 if ( debug ) {
2617 process . env . DEBUG = "true" ;
2718 }
2819 console . log ( ) ;
2920 debugLog ( "Options:" + JSON . stringify ( options ) ) ;
30- debugLog (
31- "Resolved options:" + JSON . stringify ( { debug, fix, root, packageJson } ) ,
32- ) ;
21+ debugLog ( "Resolved options:" + JSON . stringify ( { debug, fix, root, packageJson } ) ) ;
3322
3423 const tools = await findInstalledTools ( { root, packageJson } ) ;
3524 if ( tools . length === 0 ) {
@@ -41,28 +30,25 @@ export async function check(options: CheckOptions = {}): Promise<never> {
4130 console . log ( ) ;
4231 process . exit ( 1 ) ;
4332 }
44- const results = await createTaskList (
45- tools ,
46- async ( { input : tool , fail, succeed, warn } ) => {
47- const startTime = performance . now ( ) ;
48- // Run checks
49- const fn = fix ? ( tool . fix ?? tool . check ) : tool . check ;
50- const problems = await fn ( ) ;
51- // Ensure problems are absolute paths relative to the root dir
52- problems . forEach ( ( problem ) => {
53- problem . file = resolve ( root ?? process . cwd ( ) , problem . file ) ;
54- } ) ;
55- const duration = humanMs ( performance . now ( ) - startTime ) ;
56-
57- const title = `${ tool . name } ${ dim ( `(${ duration } )` ) } ` ;
58- const errorCount = problems . filter ( ( p ) => p . kind === "error" ) . length ;
59- if ( errorCount > 0 ) fail ( title ) ;
60- else if ( problems . length > 0 ) warn ( title ) ;
61- else succeed ( title ) ;
62-
63- return problems ;
64- } ,
65- ) ;
33+ const results = await createTaskList ( tools , async ( { input : tool , fail, succeed, warn } ) => {
34+ const startTime = performance . now ( ) ;
35+ // Run checks
36+ const fn = fix ? ( tool . fix ?? tool . check ) : tool . check ;
37+ const problems = await fn ( ) ;
38+ // Ensure problems are absolute paths relative to the root dir
39+ problems . forEach ( ( problem ) => {
40+ problem . file = resolve ( root ?? process . cwd ( ) , problem . file ) ;
41+ } ) ;
42+ const duration = humanMs ( performance . now ( ) - startTime ) ;
43+
44+ const title = `${ tool . name } ${ dim ( `(${ duration } )` ) } ` ;
45+ const errorCount = problems . filter ( ( p ) => p . kind === "error" ) . length ;
46+ if ( errorCount > 0 ) fail ( title ) ;
47+ else if ( problems . length > 0 ) warn ( title ) ;
48+ else succeed ( title ) ;
49+
50+ return problems ;
51+ } ) ;
6652
6753 const problems = results . flat ( ) ;
6854 console . log ( ) ;
@@ -97,10 +83,7 @@ export async function check(options: CheckOptions = {}): Promise<never> {
9783 return acc ;
9884 } , { } ) ,
9985 ) ;
100- const maxLength = files . reduce (
101- ( prev , [ file ] ) => Math . max ( prev , file . length ) ,
102- 0 ,
103- ) ;
86+ const maxLength = files . reduce ( ( prev , [ file ] ) => Math . max ( prev , file . length ) , 0 ) ;
10487 console . log ( ) ;
10588 console . log ( "Across " + plural ( files . length , "File:" , "Files:" ) ) ;
10689 files . forEach ( ( [ file , count ] ) => {
@@ -112,14 +95,11 @@ export async function check(options: CheckOptions = {}): Promise<never> {
11295 process . exit ( problems . length ) ;
11396}
11497
115- async function findInstalledTools (
116- opts : Parameters < ToolDefinition > [ 0 ] ,
117- ) : Promise < Tool [ ] > {
98+ async function findInstalledTools ( opts : Parameters < ToolDefinition > [ 0 ] ) : Promise < Tool [ ] > {
11899 const status = await Promise . all (
119100 ALL_TOOLS . map ( async ( def ) => {
120101 const tool = await def ( opts ) ;
121- const isInstalled =
122- ! ! opts . packageJson . devDependencies ?. [ tool . packageName ] ;
102+ const isInstalled = ! ! opts . packageJson . devDependencies ?. [ tool . packageName ] ;
123103 return { tool, isInstalled } ;
124104 } ) ,
125105 ) ;
@@ -135,9 +115,7 @@ async function findInstalledTools(
135115 const skipped = getTools ( false ) ;
136116 debugLog ( `Skipping: ${ skipped || "(none)" } ` ) ;
137117 }
138- return status
139- . filter ( ( { isInstalled } ) => isInstalled )
140- . map ( ( item ) => item . tool ) ;
118+ return status . filter ( ( { isInstalled } ) => isInstalled ) . map ( ( item ) => item . tool ) ;
141119}
142120
143121function plural ( count : number , singular : string , plural : string ) : string {
0 commit comments