11import type { Stats } from 'node:fs' ;
2- import { rm } from 'node:fs/promises' ;
32import chokidar , { type FSWatcher } from 'chokidar' ;
4- import { WatchInitializationError } from './error.js' ;
53import type { Logger } from './logger/logger.js' ;
64import { createProject , type Project } from './project.js' ;
75
@@ -13,28 +11,6 @@ interface RunnerArgs {
1311export interface Watcher {
1412 /** Exported for testing purposes */
1513 project : Project ;
16- close ( ) : Promise < void > ;
17- }
18-
19- /**
20- * Run css-modules-kit .d.ts generation.
21- * @param project The absolute path to the project directory or the path to `tsconfig.json`.
22- * @throws {ReadCSSModuleFileError } When failed to read CSS Module file.
23- * @throws {WriteDtsFileError }
24- * @returns Whether the process succeeded without errors.
25- */
26- export async function runCMK ( args : RunnerArgs , logger : Logger ) : Promise < boolean > {
27- const project = createProject ( args ) ;
28- if ( args . clean ) {
29- await rm ( project . config . dtsOutDir , { recursive : true , force : true } ) ;
30- }
31- await project . emitDtsFiles ( ) ;
32- const diagnostics = project . getDiagnostics ( ) ;
33- if ( diagnostics . length > 0 ) {
34- logger . logDiagnostics ( diagnostics ) ;
35- return false ;
36- }
37- return true ;
3814}
3915
4016/**
@@ -52,20 +28,16 @@ export async function runCMK(args: RunnerArgs, logger: Logger): Promise<boolean>
5228 * @throws {WatchInitializationError }
5329 */
5430export async function runCMKInWatchMode ( args : RunnerArgs , logger : Logger ) : Promise < Watcher > {
55- let initialized = false ;
5631 const fsWatchers : FSWatcher [ ] = [ ] ;
5732 const project = createProject ( args ) ;
5833 let emitAndReportDiagnosticsTimer : NodeJS . Timeout | undefined = undefined ;
5934
60- if ( args . clean ) {
61- await rm ( project . config . dtsOutDir , { recursive : true , force : true } ) ;
62- }
6335 await emitAndReportDiagnostics ( ) ;
6436
6537 // Watch project files and report diagnostics on changes
6638 const readyPromises : Promise < void > [ ] = [ ] ;
6739 for ( const wildcardDirectory of project . config . wildcardDirectories ) {
68- const { promise, resolve, reject } = promiseWithResolvers < void > ( ) ;
40+ const { promise, resolve } = promiseWithResolvers < void > ( ) ;
6941 readyPromises . push ( promise ) ;
7042 fsWatchers . push (
7143 chokidar
@@ -88,15 +60,6 @@ export async function runCMKInWatchMode(args: RunnerArgs, logger: Logger): Promi
8860 ignoreInitial : true ,
8961 ...( wildcardDirectory . recursive ? { } : { depth : 0 } ) ,
9062 } )
91- . on ( 'add' , ( fileName ) => {
92- try {
93- project . addFile ( fileName ) ;
94- } catch ( e ) {
95- logger . logError ( e ) ;
96- return ;
97- }
98- scheduleEmitAndReportDiagnostics ( ) ;
99- } )
10063 . on ( 'change' , ( fileName ) => {
10164 console . log ( 'change event: ' , fileName ) ;
10265 try {
@@ -107,26 +70,13 @@ export async function runCMKInWatchMode(args: RunnerArgs, logger: Logger): Promi
10770 }
10871 scheduleEmitAndReportDiagnostics ( ) ;
10972 } )
110- . on ( 'unlink' , ( fileName : string ) => {
111- project . removeFile ( fileName ) ;
112- scheduleEmitAndReportDiagnostics ( ) ;
113- } )
11473 . on ( 'raw' , ( eventName , fileName , details ) => {
11574 console . log ( 'raw event:' , { fileName } ) ;
11675 } )
117- // eslint-disable-next-line no-loop-func
118- . on ( 'error' , ( e ) => {
119- if ( ! initialized ) {
120- reject ( new WatchInitializationError ( e ) ) ;
121- } else {
122- logger . logError ( e ) ;
123- }
124- } )
12576 . on ( 'ready' , ( ) => resolve ( ) ) ,
12677 ) ;
12778 }
12879 await Promise . all ( readyPromises ) ;
129- initialized = true ;
13080
13181 function scheduleEmitAndReportDiagnostics ( ) {
13282 // Switching between git branches results in numerous file changes occurring rapidly.
@@ -157,11 +107,7 @@ export async function runCMKInWatchMode(args: RunnerArgs, logger: Logger): Promi
157107 ) ;
158108 }
159109
160- async function close ( ) {
161- await Promise . all ( fsWatchers . map ( async ( watcher ) => watcher . close ( ) ) ) ;
162- }
163-
164- return { project, close } ;
110+ return { project } ;
165111}
166112
167113function promiseWithResolvers < T > ( ) {
0 commit comments