@@ -4,27 +4,22 @@ const path = require('path')
44
55function prependGenerated ( filePath ) {
66 const content = [
7- '// @ts-nocheck' ,
8- '// prettier-ignore' ,
9- '/* eslint-disable */' ,
7+ '/* These types will be used in the segment app UI for autocomplete */' ,
108 '\n' ,
119 ] . join ( '\n' )
12- if ( ! fs . existsSync ( filePath ) || ! content ) {
13- return console . error ( 'cannot prepend, invalid args.' )
14- }
1510 const fileContent = fs . readFileSync ( filePath , 'utf8' )
1611 fs . writeFileSync ( filePath , content + fileContent )
1712}
1813
19- function appendFileContents ( targetFilePath , sourceFilePath ) {
14+ function appendFileContents ( targetFilePath , sourceFilePath , divider = '\n' ) {
2015 const sourceContent = fs . readFileSync ( sourceFilePath , { encoding : 'utf-8' } )
21- fs . appendFileSync ( targetFilePath , `\n\n${ sourceContent } ` , {
16+ const newContent = `${ divider } ${ sourceContent } `
17+ fs . appendFileSync ( targetFilePath , newContent , {
2218 encoding : 'utf-8' ,
2319 } )
2420}
2521
2622function removeExport ( filePath ) {
27- console . log ( `Cleaning up ${ filePath } ...` )
2823 const data = fs . readFileSync ( filePath , { encoding : 'utf-8' } )
2924 // remove export declarations and non-interface/type exports
3025 const processedContent = data
@@ -46,14 +41,37 @@ function removeExport(filePath) {
4641 fs . writeFileSync ( filePath , processedContent , { encoding : 'utf-8' } )
4742}
4843
49- const outFile = `generated/web.d.ts`
50- const command = `yarn dts-bundle-generator -o ${ outFile } src/web-exports.ts --no-check --inline-declare-global --inline-declare-externals`
51- execSync ( command , { stdio : 'inherit' } )
52- // Example usage of processTSFile function
53- const tsFilePath = path . join ( __dirname , outFile )
54- removeExport ( tsFilePath )
55- prependGenerated ( tsFilePath )
56-
57- // Append the contents of web-exports-globals.ts
58- const globalsFilePath = path . join ( __dirname , 'src/web-exports-globals.ts' )
59- appendFileContents ( tsFilePath , globalsFilePath )
44+ /**
45+ * Filters the lines of a file based on a callback function and writes the processed content back to the file.
46+ *
47+ * @param {string } filePath - The path to the file to be processed.
48+ * @param {(line: string) => boolean } cb - A callback function that takes a line as input and returns a boolean indicating whether the line should be kept.
49+ */
50+ function filterLines ( filePath , cb ) {
51+ const data = fs . readFileSync ( filePath , { encoding : 'utf-8' } )
52+ const processedContent = data
53+ . split ( '\n' )
54+ . filter ( ( line ) => cb ( line ) )
55+ . join ( '\n' )
56+
57+ fs . writeFileSync ( filePath , processedContent , { encoding : 'utf-8' } )
58+ }
59+
60+ const main = ( ) => {
61+ const outFile = `dist/web.d.ts`
62+ const command = `yarn dts-bundle-generator -o ${ outFile } src/web-exports.ts --no-check`
63+ execSync ( command , { stdio : 'inherit' } )
64+ const outFileAbs = path . join ( __dirname , outFile )
65+ removeExport ( outFileAbs )
66+
67+ // Prepend ignore artifactions
68+ prependGenerated ( outFileAbs )
69+
70+ // Append the contents of web-exports-globals.ts
71+ const globalsFilePath = path . join ( __dirname , 'src/web-exports-globals.ts' )
72+ appendFileContents ( outFileAbs , globalsFilePath )
73+ // remove any comments that use // like ts-ignore, ts-nocheck etc (/* */ is OK)
74+ filterLines ( outFileAbs , ( line ) => ! line . startsWith ( '//' ) )
75+ }
76+
77+ main ( )
0 commit comments