11import { build } from 'esbuild' ;
2- import {
3- copyFileSync ,
4- readFileSync ,
5- readdirSync ,
6- writeFileSync ,
7- existsSync ,
8- } from 'node:fs' ;
2+ import { copyFileSync , readFileSync , readdirSync , writeFileSync } from 'node:fs' ;
93import { dirname , join , resolve } from 'node:path' ;
104import { fileURLToPath } from 'node:url' ;
11- import AdmZip from 'adm-zip' ;
12- import { format } from 'prettier' ;
5+ import { format , resolveConfig } from 'prettier' ;
136
147import { localExternals } from './build/plugins/local-externals.ts' ;
158import { createGirsResolver } from './build/plugins/girs-resolver.ts' ;
169import { gobjectDecorator } from './build/plugins/gobject-decorator.ts' ;
1710import { addBlankLinesBetweenMembers } from './build/formatting.ts' ;
1811
19- interface ExtensionMetadata {
20- name : string ;
21- version : string ;
22- uuid : string ;
23- }
24-
2512const currentDir = dirname ( fileURLToPath ( import . meta. url ) ) ;
26- const metadataPath = resolve ( currentDir , 'metadata.json' ) ;
27- const metadata = JSON . parse ( readFileSync ( metadataPath , 'utf8' ) ) as ExtensionMetadata ;
2813
2914const srcDir = resolve ( currentDir , 'src' ) ;
3015const entryPoints = ( readdirSync ( srcDir , { recursive : true } ) as string [ ] )
3116 . filter ( ( file ) => file . endsWith ( '.ts' ) && ! file . endsWith ( '.d.ts' ) )
3217 . map ( ( file ) => join ( 'src' , file ) ) ;
3318
34- console . debug ( `Building ${ metadata . name } v${ metadata . version } ...` ) ;
35-
36- const sharedOptions = {
37- outdir : 'dist' ,
38- outbase : 'src' ,
39- bundle : true ,
40- plugins : [ gobjectDecorator , localExternals , createGirsResolver ( currentDir ) ] ,
41- // Do not remove the functions `enable()`, `disable()` and `init()`
42- treeShaking : false ,
43- // firefox60 // Since GJS 1.53.90
44- // firefox68 // Since GJS 1.63.90
45- // firefox78 // Since GJS 1.65.90
46- // firefox91 // Since GJS 1.71.1
47- // firefox102 // Since GJS 1.73.2
48- target : 'firefox102' as const ,
49- format : 'esm' as const ,
50- external : [ 'gi://*' , 'resource://*' , 'system' , 'gettext' , 'cairo' ] ,
51- } ;
52-
53- Promise . all (
54- entryPoints . map ( ( entryPoint ) =>
55- build ( { ...sharedOptions , entryPoints : [ entryPoint ] } ) ,
56- ) ,
57- )
58- . then ( async ( ) => {
59- const distDir = resolve ( currentDir , 'dist' ) ;
60- const metaDist = resolve ( distDir , 'metadata.json' ) ;
61- const schemasSrc = resolve ( currentDir , 'schemas' ) ;
62- const styleFiles = [
63- 'stylesheet.css' ,
64- 'stylesheet-light.css' ,
65- 'stylesheet-dark.css' ,
66- ] ;
67- const zipFilename = `${ metadata . uuid } .zip` ;
68- const zipDist = resolve ( distDir , zipFilename ) ;
69-
70- // Format output files with prettier
71- const jsFiles = ( readdirSync ( distDir , { recursive : true } ) as string [ ] )
72- . filter ( ( file ) => file . endsWith ( '.js' ) ) ;
73-
74- for ( const file of jsFiles ) {
75- const filePath = resolve ( distDir , file ) ;
76- const content = readFileSync ( filePath , 'utf8' ) ;
77- const formatted = await format ( content , { parser : 'babel' , filepath : filePath } ) ;
78- writeFileSync ( filePath , addBlankLinesBetweenMembers ( formatted ) ) ;
79- }
19+ console . debug ( 'Building extension...' ) ;
20+
21+ try {
22+ await build ( {
23+ entryPoints,
24+ outdir : 'dist' ,
25+ outbase : 'src' ,
26+ bundle : true ,
27+ plugins : [ gobjectDecorator , localExternals , createGirsResolver ( currentDir ) ] ,
28+ treeShaking : false ,
29+ target : 'firefox102' ,
30+ format : 'esm' ,
31+ external : [ 'gi://*' , 'resource://*' , 'system' , 'gettext' , 'cairo' ] ,
32+ } ) ;
8033
81- copyFileSync ( metadataPath , metaDist ) ;
34+ const distDir = resolve ( currentDir , 'dist' ) ;
35+ const jsFiles = ( readdirSync ( distDir , { recursive : true } ) as string [ ] )
36+ . filter ( ( file ) => file . endsWith ( '.js' ) ) ;
37+
38+ console . debug ( 'Formatting output files...' ) ;
39+
40+ for ( const file of jsFiles ) {
41+ const filePath = resolve ( distDir , file ) ;
42+ const content = readFileSync ( filePath , 'utf8' ) ;
43+
44+ const prettierConfig = await resolveConfig ( filePath ) || { } ;
45+
46+ const formatted = await format ( content , {
47+ ...prettierConfig ,
48+ parser : 'babel' ,
49+ filepath : filePath
50+ } ) ;
8251
83- const zip : AdmZip = new AdmZip ( ) ;
52+ writeFileSync ( filePath , addBlankLinesBetweenMembers ( formatted ) ) ;
53+ }
8454
85- for ( const file of jsFiles ) {
86- const filePath = resolve ( distDir , file ) ;
87- const dir = dirname ( file ) ;
88- zip . addLocalFile ( filePath , dir === '.' ? '' : dir ) ;
89- }
55+ copyFileSync ( resolve ( currentDir , 'metadata.json' ) , resolve ( distDir , 'metadata.json' ) ) ;
9056
91- styleFiles . forEach ( ( styleFile ) => {
92- const stylePath = resolve ( distDir , styleFile ) ;
93- if ( existsSync ( stylePath ) ) {
94- zip . addLocalFile ( stylePath ) ;
95- }
96- } ) ;
97- zip . addLocalFile ( metaDist ) ;
98- if ( existsSync ( schemasSrc ) ) {
99- zip . addLocalFolder ( schemasSrc , 'schemas' ) ;
100- }
101- zip . writeZip ( zipDist ) ;
102-
103- console . debug ( `Build complete. Zip file: ${ zipFilename } ` ) ;
104- } )
105- . catch ( ( error : unknown ) => {
106- console . error ( 'Build failed:' , error ) ;
107- process . exit ( 1 ) ;
108- } ) ;
57+ console . debug ( 'Build complete.' ) ;
58+ } catch ( error ) {
59+ console . error ( 'Build failed:' , error ) ;
60+ process . exit ( 1 ) ;
61+ }
0 commit comments