chrome extension for skeeting tweets at the sky
1import * as esbuild from 'esbuild';
2import { exec } from 'child_process';
3import { promises as fs } from 'fs';
4import path from 'path';
5
6// Helper function to execute shell commands
7async function execCommand(command) {
8 return new Promise((resolve, reject) => {
9 exec(command, (error, stdout, stderr) => {
10 if (error) {
11 console.error(`Error executing command: ${error}`);
12 reject(error);
13 return;
14 }
15 if (stderr) {
16 console.warn(`Command stderr: ${stderr}`);
17 }
18 resolve(stdout);
19 });
20 });
21}
22
23// Helper function to ensure directory exists
24async function ensureDir(dir) {
25 try {
26 await fs.mkdir(dir, { recursive: true });
27 } catch (error) {
28 if (error.code !== 'EEXIST') {
29 throw error;
30 }
31 }
32}
33
34// Main build function
35async function build() {
36 try {
37 console.log('Starting build process...');
38
39 // Clean dist directory
40 await execCommand('rm -rf dist');
41 await ensureDir('dist');
42 await ensureDir('dist/scripts');
43 await ensureDir('dist/lib');
44 await ensureDir('dist/assets/css');
45 await ensureDir('dist/assets/icon');
46 await ensureDir('dist/pages');
47
48 // Copy OAuth-related files from pack to src
49 console.log('Copying OAuth-related files from pack to src...');
50
51 // Create src/scripts directory if it doesn't exist
52 await ensureDir('src/scripts');
53
54 // Build JavaScript files with minification
55 console.log('Building and minifying JavaScript files...');
56 await esbuild.build({
57 entryPoints: [
58 'src/scripts/popup.js',
59 'src/scripts/background.js',
60 'src/scripts/content-script.js',
61 'src/scripts/extension-id-provider.js',
62 ],
63 bundle: true,
64 outdir: 'dist/scripts',
65 format: 'esm',
66 platform: 'browser',
67 target: ['chrome90'],
68 loader: {
69 '.js': 'jsx',
70 '.ts': 'ts',
71 },
72 define: {
73 'process.env.NODE_ENV': '"production"',
74 },
75 minify: true,
76 drop: ['console'],
77 });
78
79 // Build the API TypeScript file with minification
80 console.log('Building and minifying API TypeScript file...');
81 await esbuild.build({
82 entryPoints: ['src/lib/api.ts'],
83 bundle: true,
84 outdir: 'dist/lib',
85 format: 'esm',
86 platform: 'browser',
87 target: ['chrome90'],
88 loader: {
89 '.ts': 'ts',
90 },
91 define: {
92 'process.env.NODE_ENV': '"production"',
93 },
94 minify: true,
95 drop: ['console'],
96 });
97
98 // Copy static files
99 console.log('Copying static files...');
100
101 // Copy manifest.json
102 await fs.copyFile('manifest.json', 'dist/manifest.json');
103
104 // Copy HTML files
105 const htmlFiles = [
106 { src: 'src/pages/popup.html', dest: 'dist/pages/popup.html' },
107 ];
108
109 for (const file of htmlFiles) {
110 try {
111 console.log(`Copying ${file.src} to ${file.dest}`);
112 await fs.copyFile(file.src, file.dest);
113 } catch (error) {
114 console.error(`Error copying ${file.src}: ${error.message}`);
115 }
116 }
117
118 // Copy icon files
119 const iconDir = 'src/assets/icon';
120 const iconDestDir = 'dist/assets/icon';
121 try {
122 const iconFiles = await fs.readdir(iconDir);
123 for (const file of iconFiles) {
124 if (file.endsWith('.png')) {
125 console.log(`Copying icon file: ${file}`);
126 await fs.copyFile(path.join(iconDir, file), path.join(iconDestDir, file));
127 }
128 }
129 } catch (error) {
130 console.error(`Error copying icon files: ${error.message}`);
131 }
132
133 // Copy logo.svg file
134 try {
135 await ensureDir('dist/assets');
136 console.log('Copying logo.svg file');
137 await fs.copyFile('src/assets/logo.svg', 'dist/assets/logo.svg');
138 } catch (error) {
139 console.error(`Error copying logo.svg file: ${error.message}`);
140 }
141
142 console.log('Build completed successfully!');
143 } catch (error) {
144 console.error('Build failed:', error);
145 process.exit(1);
146 }
147}
148
149// Run the build
150build();