|  | 
|  | 1 | +// Synchronize the sources for our c-ares gyp file from c-ares' Makefiles. | 
|  | 2 | +import { readFileSync, writeFileSync } from 'node:fs'; | 
|  | 3 | +import { join } from 'node:path'; | 
|  | 4 | + | 
|  | 5 | +const srcroot = join(import.meta.dirname, '..', '..'); | 
|  | 6 | +const options = { encoding: 'utf8' }; | 
|  | 7 | + | 
|  | 8 | +// Extract list of sources from the gyp file. | 
|  | 9 | +const gypFile = join(srcroot, 'deps', 'cares', 'cares.gyp'); | 
|  | 10 | +const contents = readFileSync(gypFile, options); | 
|  | 11 | +const sourcesRE = /^\s+'cares_sources_common':\s+\[\s*\n(?<files>[\s\S]*?)\s+\],$/gm; | 
|  | 12 | +const sourcesCommon = sourcesRE.exec(contents); | 
|  | 13 | + | 
|  | 14 | +// Extract the list of sources from c-ares' Makefile.inc. | 
|  | 15 | +const makefile = join(srcroot, 'deps', 'cares', 'src', 'lib', 'Makefile.inc'); | 
|  | 16 | +const libSources = readFileSync(makefile, options).split('\n') | 
|  | 17 | +  // Extract filenames (excludes comments and variable assignment). | 
|  | 18 | +  .map((line) => line.match(/^(?:.*= |\s*)?([^#\s]*)\s*\\?/)?.[1]) | 
|  | 19 | +  // Filter out empty lines. | 
|  | 20 | +  .filter((line) => line !== '') | 
|  | 21 | +  // Prefix with directory and format as list entry. | 
|  | 22 | +  .map((line) => `      'src/lib/${line}',`); | 
|  | 23 | + | 
|  | 24 | +// Extract include files. | 
|  | 25 | +const includeMakefile = join(srcroot, 'deps', 'cares', 'include', 'Makefile.am'); | 
|  | 26 | +const includeSources = readFileSync(includeMakefile, options) | 
|  | 27 | +  .match(/include_HEADERS\s*=\s*(.*)/)[1] | 
|  | 28 | +  .split(/\s/) | 
|  | 29 | +  .map((header) => `      'include/${header}',`); | 
|  | 30 | + | 
|  | 31 | +// Combine the lists. Alphabetically sort to minimize diffs. | 
|  | 32 | +const fileList = includeSources.concat(libSources).sort(); | 
|  | 33 | + | 
|  | 34 | +// Replace the list of sources. | 
|  | 35 | +const newContents = contents.replace(sourcesCommon.groups.files, fileList.join('\n')); | 
|  | 36 | +if (newContents !== contents) { | 
|  | 37 | +  writeFileSync(gypFile, newContents, options); | 
|  | 38 | +} | 
0 commit comments