-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-extension.js
More file actions
83 lines (70 loc) · 2.49 KB
/
build-extension.js
File metadata and controls
83 lines (70 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const PROJECT_ROOT = __dirname;
const BUILD_DIR = path.join(PROJECT_ROOT, 'dist-prod');
const ARCHIVE_NAME = 'lingocontext-production.zip';
const ARCHIVE_PATH = path.join(PROJECT_ROOT, ARCHIVE_NAME);
const INCLUDE_PATHS = [
'manifest.json',
'background.js',
'config.js',
'content.js',
'dashboard.html',
'dashboard.js',
'db-hook.js',
'i18n.js',
'popup.html',
'popup.js',
'styles.css',
'_locales',
'icons',
'README.md'
];
console.log('Starting production build for Chrome Web Store...');
if (fs.existsSync(BUILD_DIR)) {
console.log('Cleaning old build directory...');
fs.rmSync(BUILD_DIR, { recursive: true, force: true });
}
if (fs.existsSync(ARCHIVE_PATH)) {
fs.unlinkSync(ARCHIVE_PATH);
}
fs.mkdirSync(BUILD_DIR, { recursive: true });
console.log('Copying extension files...');
for (const item of INCLUDE_PATHS) {
const sourcePath = path.join(PROJECT_ROOT, item);
const destPath = path.join(BUILD_DIR, item);
if (fs.existsSync(sourcePath)) {
const stats = fs.statSync(sourcePath);
if (stats.isDirectory()) {
execSync(`cp -R "${sourcePath}" "${BUILD_DIR}"`);
} else {
fs.copyFileSync(sourcePath, destPath);
}
} else {
console.warn(`Warning: ${item} not found.`);
}
}
console.log('Patching config.js for production...');
const configPath = path.join(BUILD_DIR, 'config.js');
let configContent = fs.readFileSync(configPath, 'utf8');
configContent = configContent
.replace(/DEV_MODE:\s*true/, 'DEV_MODE: false')
.replace(/BACKEND_URL:\s*["']http:\/\/localhost:3000\/api["']/, '// BACKEND_URL: "http://localhost:3000/api"');
if (!configContent.includes('BACKEND_URL: "https://lingo-context-api.vercel.app/api"')) {
configContent = configContent.replace(
/\/\/\s*BACKEND_URL:\s*["']https:\/\/lingo-context-api\.vercel\.app\/api["']/,
'BACKEND_URL: "https://lingo-context-api.vercel.app/api"'
);
}
fs.writeFileSync(configPath, configContent);
console.log('Creating ZIP archive...');
try {
execSync(`cd "${BUILD_DIR}" && zip -r -q "../${ARCHIVE_NAME}" ./*`);
console.log(`Success! Production archive created at: ${ARCHIVE_NAME}`);
console.log('Cleaning up temporary build directory...');
fs.rmSync(BUILD_DIR, { recursive: true, force: true });
} catch (e) {
console.error('Failed to create zip archive.', e.message);
process.exit(1);
}