@@ -6,8 +6,16 @@ import { sep as posixSep } from 'node:path/posix';
6
6
7
7
// Fetch latest version
8
8
9
- console . log ( 'Getting latest version of \x1b[36m@types/chrome\x1b[0m' ) ;
10
- await spawn ( 'pnpm' , [ 'i' , '--ignore-scripts' , '-D' , '@types/chrome@latest' ] ) ;
9
+ console . log (
10
+ 'Getting latest version of \x1b[36m@types/chrome\x1b[0m, \x1b[36m@types/firefox-webext-browser\x1b[0m' ,
11
+ ) ;
12
+ await spawn ( 'pnpm' , [
13
+ 'i' ,
14
+ '--ignore-scripts' ,
15
+ '-D' ,
16
+ '@types/chrome@latest' ,
17
+ '@types/firefox-webext-browser@latest' ,
18
+ ] ) ;
11
19
12
20
// Generate new package.json
13
21
@@ -16,11 +24,17 @@ console.log('Generating new \x1b[36mpackage.json\x1b[0m');
16
24
const pkgJsonPath = fileURLToPath (
17
25
import . meta. resolve ( '@types/chrome/package.json' ) ,
18
26
) ;
27
+ const firefoxPkgJsonPath = fileURLToPath (
28
+ import . meta. resolve ( '@types/firefox-webext-browser/package.json' ) ,
29
+ ) ;
19
30
const pkgDir = dirname ( pkgJsonPath ) ;
20
31
const pkgJson = await fs . readJson ( pkgJsonPath ) ;
32
+ const firefoxPkgJson = await fs . readJson ( firefoxPkgJsonPath ) ;
21
33
const pkgJsonTemplate = await fs . readFile ( 'templates/package.json' , 'utf8' ) ;
22
34
const newPkgJson = JSON . parse (
23
- pkgJsonTemplate . replaceAll ( '{{chromeTypesVersion}}' , pkgJson . version ) ,
35
+ pkgJsonTemplate
36
+ . replaceAll ( '{{chromeTypesVersion}}' , pkgJson . version )
37
+ . replaceAll ( '{{firefoxTypesVersion}}' , firefoxPkgJson . version ) ,
24
38
) ;
25
39
newPkgJson . dependencies = pkgJson . dependencies ;
26
40
newPkgJson . peerDependencies = pkgJson . peerDependencies ;
@@ -51,7 +65,38 @@ const declarationFileMapping = (
51
65
52
66
for ( const { file, srcPath, destPath } of declarationFileMapping ) {
53
67
const content = await fs . readFile ( srcPath , 'utf8' ) ;
54
- const transformedContent = transformFile ( file , content ) ;
68
+ const transformedContent = transformChromeFile ( file , content ) ;
69
+ const destDir = dirname ( destPath ) ;
70
+ await fs . mkdir ( destDir , { recursive : true } ) ;
71
+ await fs . writeFile ( destPath , transformedContent ) ;
72
+ console . log ( ` \x1b[2m-\x1b[0m \x1b[36m${ file } \x1b[0m` ) ;
73
+ }
74
+ // Generate Firefox declaration files
75
+ console . log ( 'Generating \x1b[36mfirefox\x1b[0m declaration files' ) ;
76
+
77
+ const firefoxPkgDir = dirname ( firefoxPkgJsonPath ) ;
78
+ const firefoxDeclarationFileMapping = (
79
+ await fs . readdir ( firefoxPkgDir , {
80
+ recursive : true ,
81
+ encoding : 'utf8' ,
82
+ } )
83
+ )
84
+ // Filter to .d.ts files
85
+ . filter ( ( file ) => file . endsWith ( '.d.ts' ) )
86
+ // Map to usable paths
87
+ . map ( ( file ) => ( {
88
+ file : file . includes ( 'index' )
89
+ ? file . replaceAll ( sep , posixSep ) . replace ( 'index' , 'firefox' )
90
+ : file . replaceAll ( sep , posixSep ) ,
91
+ srcPath : join ( firefoxPkgDir , file ) ,
92
+ destPath : file . includes ( 'index' )
93
+ ? join ( outDir , 'firefox.d.ts' )
94
+ : join ( outDir , file ) ,
95
+ } ) ) ;
96
+
97
+ for ( const { file, srcPath, destPath } of firefoxDeclarationFileMapping ) {
98
+ const content = await fs . readFile ( srcPath , 'utf8' ) ;
99
+ const transformedContent = transformFirefoxFile ( file , content ) ;
55
100
const destDir = dirname ( destPath ) ;
56
101
await fs . mkdir ( destDir , { recursive : true } ) ;
57
102
await fs . writeFile ( destPath , transformedContent ) ;
@@ -66,7 +111,7 @@ console.log(
66
111
67
112
// Transformations
68
113
69
- function transformFile ( file : string , content : string ) : string {
114
+ function transformChromeFile ( file : string , content : string ) : string {
70
115
return (
71
116
// Add prefix
72
117
`/* DO NOT EDIT - generated by scripts/generate.ts */\n\n${ content } \n`
@@ -80,3 +125,13 @@ function transformFile(file: string, content: string): string {
80
125
. replaceAll ( 'developer.Browser.com' , 'developer.chrome.com' )
81
126
) ;
82
127
}
128
+ function transformFirefoxFile ( file : string , content : string ) : string {
129
+ return (
130
+ // Add prefix
131
+ `/* DO NOT EDIT - generated by scripts/generate.ts */\n\n${ content } \n`
132
+ // Rename `browser` namespace to `Firefox` and export it
133
+ . replaceAll ( 'declare namespace browser' , 'export namespace Firefox' )
134
+ // Update references to `browser` namespace to `Firefox`
135
+ . replaceAll ( 'browser.' , 'Firefox.' )
136
+ ) ;
137
+ }
0 commit comments