@@ -76,6 +76,19 @@ const samePropertyRuns = (codePointProperties) => {
7676 return result ;
7777} ;
7878
79+ // Only write the file if its content has changed.
80+ // Avoid the unnecessary filesystem I/O
81+ const writeFileIfChanged = ( filePath , content ) => {
82+ try {
83+ if ( fs . readFileSync ( filePath , 'utf8' ) === content ) {
84+ return ;
85+ }
86+ } catch {
87+ // Ignore errors such ENOENT (file not found)
88+ }
89+ fs . writeFileSync ( filePath , content ) ;
90+ } ;
91+
7992const writeFiles = ( options ) => {
8093 const version = options . version ;
8194 const subType = options . subType ;
@@ -147,11 +160,11 @@ const writeFiles = (options) => {
147160 if ( type === 'Sequence_Property' || isNameAliases ) {
148161 const sequences = codePoints ;
149162 const output = `import { gunzipSync } from 'node:zlib';\n\nexport default ${ gzipInline ( sequences ) } ;\n` ;
150- fs . writeFileSync (
163+ writeFileIfChanged (
151164 path . resolve ( dir , 'index.mjs' ) ,
152165 output
153166 ) ;
154- fs . writeFileSync (
167+ writeFileIfChanged (
155168 path . resolve ( dir , 'index.d.mts' ) ,
156169 type === 'Sequence_Property'
157170 ? `declare const data: string[];\nexport default data;\n`
@@ -167,19 +180,19 @@ const writeFiles = (options) => {
167180 let symbolsType = 'string[]' ;
168181 if ( ! isCaseFoldingOrMapping ) {
169182 const encodedRanges = codePoints instanceof regenerate ? encodeRegenerate ( codePoints ) : encodeRanges ( codePoints ) ;
170- fs . writeFileSync (
183+ writeFileIfChanged (
171184 path . resolve ( dir , 'ranges.mjs' ) ,
172185 `import decodeRanges from '../../decode-ranges.mjs';\n\nexport default decodeRanges('${ encodedRanges } ');\n`
173186 ) ;
174- fs . writeFileSync (
187+ writeFileIfChanged (
175188 path . resolve ( dir , 'ranges.d.mts' ) ,
176189 'import type { UnicodeRange } from "../../decode-ranges.mjs";\n\ndeclare const ranges: UnicodeRange[];\nexport default ranges;\n'
177190 ) ;
178- fs . writeFileSync (
191+ writeFileIfChanged (
179192 path . resolve ( dir , 'regex.mjs' ) ,
180193 'export default /' + regenerate ( codePoints ) . toString ( ) + '/;\n'
181194 ) ;
182- fs . writeFileSync (
195+ writeFileIfChanged (
183196 path . resolve ( dir , 'regex.d.mts' ) ,
184197 'declare const regex: RegExp;\nexport default regex;\n'
185198 ) ;
@@ -213,19 +226,19 @@ const writeFiles = (options) => {
213226 }
214227 symbolsType = 'Map<string, string>' ;
215228 }
216- fs . writeFileSync (
229+ writeFileIfChanged (
217230 path . resolve ( dir , 'code-points.mjs' ) ,
218231 codePointsFileContent
219232 ) ;
220- fs . writeFileSync (
233+ writeFileIfChanged (
221234 path . resolve ( dir , 'code-points.d.mts' ) ,
222235 `declare const codePoints: ${ codePointsType } ;\nexport default codePoints;\n`
223236 ) ;
224- fs . writeFileSync (
237+ writeFileIfChanged (
225238 path . resolve ( dir , 'symbols.mjs' ) ,
226239 symbolsFileContent
227240 ) ;
228- fs . writeFileSync (
241+ writeFileIfChanged (
229242 path . resolve ( dir , 'symbols.d.mts' ) ,
230243 `declare const symbols: ${ symbolsType } ;\nexport default symbols;\n`
231244 ) ;
@@ -249,11 +262,11 @@ const writeFiles = (options) => {
249262 JSON . stringify ( bidiMirroringGlyphFlatPairs )
250263 } .map((v, i, a) => pair(i & 1, a[i ^ 1], v)));\n`
251264 ] . join ( '\n' ) ;
252- fs . writeFileSync (
265+ writeFileIfChanged (
253266 path . resolve ( dir , 'index.mjs' ) ,
254267 output
255268 ) ;
256- fs . writeFileSync (
269+ writeFileIfChanged (
257270 path . resolve ( dir , 'index.d.mts' ) ,
258271 `declare const data: Map<number, string>;\nexport default data;\n`
259272 ) ;
@@ -275,8 +288,8 @@ const writeFiles = (options) => {
275288 const output = `import { gunzipSync } from 'node:zlib';\nimport decodePropertyMap from '../decode-property-map.mjs';\n\nexport default decodePropertyMap(${ gzipInline (
276289 flatRuns
277290 ) } );\n`;
278- fs . writeFileSync ( path . resolve ( dir , 'index.mjs' ) , output ) ;
279- fs . writeFileSync ( path . resolve ( dir , 'index.d.mts' ) , `declare const map: Map<number, string>;\nexport default map;\n` ) ;
291+ writeFileIfChanged ( path . resolve ( dir , 'index.mjs' ) , output ) ;
292+ writeFileIfChanged ( path . resolve ( dir , 'index.d.mts' ) , `declare const map: Map<number, string>;\nexport default map;\n` ) ;
280293 }
281294 }
282295 return dirMap ;
0 commit comments