@@ -16,12 +16,14 @@ for (const userScript of userScripts) {
1616
1717class RunCommandsPlugin {
1818 private readonly env : Record < string , unknown > ;
19+ private isFirstRun = true ;
20+ private localesWatcher : null | ReturnType < typeof watch > = null ;
1921
2022 public constructor ( env : Record < string , unknown > ) {
2123 this . env = env ;
2224 }
2325
24- public static generateTypeGuards ( callback ?: ( ) => void ) : void {
26+ private static generateTypeGuards ( callback ?: ( ) => void ) : void {
2527 // eslint-disable-next-line no-console
2628 console . log ( "Generating type guards..." ) ;
2729 exec ( "npx ts-auto-guard ./src/types/**/*.ts" , ( err , stdout ) => {
@@ -38,7 +40,7 @@ class RunCommandsPlugin {
3840 } ) ;
3941 }
4042
41- public static updateManifest ( ) : void {
43+ private static updateManifest ( ) : void {
4244 exec ( "npx tsx ./script/copyManifest.ts" , ( err , stdout ) => {
4345 if ( err ) {
4446 // eslint-disable-next-line no-console
@@ -50,24 +52,42 @@ class RunCommandsPlugin {
5052 } ) ;
5153 }
5254
55+ private static updatePrivacyPolicy ( callback ?: ( ) => void ) : void {
56+ exec ( "npx tsx ./script/updatePrivacyPolicy.ts" , ( err , stdout ) => {
57+ if ( err ) {
58+ // eslint-disable-next-line no-console
59+ console . error ( `Error: ${ err . message } ` ) ;
60+ } else {
61+ // eslint-disable-next-line no-console
62+ console . log ( stdout ) ;
63+ if ( callback ) {
64+ callback ( ) ;
65+ }
66+ }
67+ } ) ;
68+ }
69+
5370 // eslint-disable-next-line max-lines-per-function
5471 public apply ( compiler : Compiler ) : void {
5572 let typeWatcher : null | ReturnType < typeof watch > = null ;
5673 let manifestWatcher : null | ReturnType < typeof watch > = null ;
5774 let isWatchMode = false ;
5875
5976 compiler . hooks . beforeCompile . tapAsync ( "RunCommandsPlugin" , ( _params , callback ) => {
60- if ( isWatchMode ) {
77+ if ( ! isWatchMode ) {
78+ RunCommandsPlugin . generateTypeGuards ( callback ) ;
79+ } else if ( this . isFirstRun ) {
80+ // `this.isFirstRun` is also used in the afterEmit hook, so it should be set to false in there.
81+ RunCommandsPlugin . generateTypeGuards ( callback ) ;
82+ } else {
6183 callback ( ) ;
62- return ;
6384 }
64-
65- RunCommandsPlugin . generateTypeGuards ( callback ) ;
6685 } ) ;
6786
6887 compiler . hooks . watchRun . tapAsync ( "RunCommandsPlugin" , ( _params , callback ) => {
6988 isWatchMode = true ;
70- if ( ! manifestWatcher || ! typeWatcher ) {
89+
90+ if ( ! manifestWatcher || ! typeWatcher || ! this . localesWatcher ) {
7191 manifestWatcher = watch ( "src/manifest/" , {
7292 ignored : ( pathString , stats ) => Boolean ( stats && stats . isFile ( ) && ! pathString . endsWith ( ".json" ) )
7393 } ) ;
@@ -77,36 +97,46 @@ class RunCommandsPlugin {
7797 RunCommandsPlugin . updateManifest ( ) ;
7898 } ) ;
7999
80- RunCommandsPlugin . updateManifest ( ) ;
81-
82100 typeWatcher = watch ( "src/types/" , {
83101 ignored : ( pathString , stats ) => Boolean ( stats && stats . isFile ( ) && ! pathString . endsWith ( ".d.ts" ) )
84102 } ) ;
85103
86104 typeWatcher . on ( "change" , ( pathString : string ) => {
87105 // eslint-disable-next-line no-console
88106 console . log ( `Type definition file changed: ${ pathString } ` ) ;
89- RunCommandsPlugin . generateTypeGuards ( ) ;
107+ RunCommandsPlugin . generateTypeGuards ( ( ) => {
108+ if ( compiler && compiler . watching ) {
109+ compiler . watching . invalidate ( ) ;
110+ }
111+ } ) ;
90112 } ) ;
91113
92- RunCommandsPlugin . generateTypeGuards ( callback ) ;
114+ if ( ! this . localesWatcher ) {
115+ this . localesWatcher = watch ( "src/_locales/" , {
116+ ignored : ( pathString , stats ) =>
117+ Boolean ( stats && stats . isFile ( ) && ! pathString . endsWith ( ".json" ) )
118+ } ) ;
119+ this . localesWatcher . on ( "change" , ( pathString : string ) => {
120+ // eslint-disable-next-line no-console
121+ console . log ( `Locale file changed: ${ pathString } ` ) ;
122+ RunCommandsPlugin . updatePrivacyPolicy ( ) ;
123+ } ) ;
124+ }
125+ callback ( ) ;
93126 } else {
94127 callback ( ) ;
95128 }
96129 } ) ;
97130
98131 compiler . hooks . afterEmit . tapAsync ( "RunCommandsPlugin" , ( _compilation , callback ) => {
99- RunCommandsPlugin . updateManifest ( ) ;
100-
101- exec ( "npx tsx ./script/updatePrivacyPolicy.ts" , ( err , stdout ) => {
102- if ( err ) {
103- // eslint-disable-next-line no-console
104- console . error ( `Error: ${ err . message } ` ) ;
105- } else {
106- // eslint-disable-next-line no-console
107- console . log ( stdout ) ;
108- }
109- } ) ;
132+ if ( ! isWatchMode ) {
133+ RunCommandsPlugin . updateManifest ( ) ;
134+ RunCommandsPlugin . updatePrivacyPolicy ( ) ;
135+ } else if ( this . isFirstRun ) {
136+ this . isFirstRun = false ;
137+ RunCommandsPlugin . updateManifest ( ) ;
138+ RunCommandsPlugin . updatePrivacyPolicy ( ) ;
139+ }
110140
111141 if ( this . env . updateUserScripts ) {
112142 exec ( "npx tsx ./script/addUserScriptsComment.ts" , ( err , stdout ) => {
0 commit comments