Skip to content

Commit bc8bd3a

Browse files
committed
wip
1 parent e05cbeb commit bc8bd3a

File tree

3 files changed

+40
-140
lines changed

3 files changed

+40
-140
lines changed

packages/signals/signals-runtime/bundle-dts.js

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,22 @@ const path = require('path')
44

55
function 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

2622
function 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()

packages/signals/signals-runtime/generated/web.d.ts

Lines changed: 0 additions & 114 deletions
This file was deleted.

packages/signals/signals-runtime/src/web-exports-globals.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
/**
2-
* This will be appended to the generated code -
3-
*dts-bundle-generator will not write declare const for whatever reason.
4-
*/
5-
// @ts-ignore
1+
// @ts-nocheck
2+
// This will be appended to the generated code - dts-bundle-generator will not write declare const for whatever reason.
63
declare const signals: SignalsRuntimeAPI
74
declare const SignalType: {
85
Interaction: 'interaction'
@@ -12,7 +9,6 @@ declare const SignalType: {
129
Instrumentation: 'instrumentation'
1310
UserDefined: 'userDefined'
1411
}
15-
1612
declare const EventType: {
1713
Track: 'track'
1814
Page: 'page'

0 commit comments

Comments
 (0)