-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
231 lines (187 loc) · 7.38 KB
/
Copy pathbuild.js
File metadata and controls
231 lines (187 loc) · 7.38 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
class ExtensionBuilder {
constructor() {
this.sourceDir = process.cwd();
this.buildDir = path.join(this.sourceDir, 'build');
this.distDir = path.join(this.sourceDir, 'dist');
// Files and directories to exclude from build
this.excludePatterns = [
'build.js',
'package.json',
'package-lock.json',
'node_modules',
'.git',
'.gitignore',
'CLAUDE.md',
'CHANGELOG.md',
'README.md',
'PRIVACY_POLICY.md',
'CONTRIBUTING.md',
'LICENSE',
'TECHNICAL_DOCS.md',
'to-do.txt',
'dev-tools',
'doc_internal',
'.claude',
'build',
'dist'
];
this.requiredManifestFields = [
'name',
'version',
'manifest_version',
'description'
];
}
// Create build and dist directories
createDirectories() {
console.log('📁 Creating directories...');
if (fs.existsSync(this.buildDir)) {
fs.rmSync(this.buildDir, { recursive: true });
}
if (fs.existsSync(this.distDir)) {
fs.rmSync(this.distDir, { recursive: true });
}
fs.mkdirSync(this.buildDir, { recursive: true });
fs.mkdirSync(this.distDir, { recursive: true });
console.log('✅ Directories created');
}
// Check if file/directory should be excluded
shouldExclude(fileName) {
return this.excludePatterns.some(pattern => {
if (fileName === pattern) return true;
if (fileName.startsWith(pattern + '/')) return true;
return false;
});
}
// Copy files recursively, excluding unwanted files
copyFiles(srcDir, destDir) {
const items = fs.readdirSync(srcDir);
for (const item of items) {
if (this.shouldExclude(item)) {
console.log(`⏭️ Skipping: ${item}`);
continue;
}
const srcPath = path.join(srcDir, item);
const destPath = path.join(destDir, item);
const stat = fs.statSync(srcPath);
if (stat.isDirectory()) {
fs.mkdirSync(destPath, { recursive: true });
this.copyFiles(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
console.log(`📄 Copied: ${item}`);
}
}
}
// Disable debug mode in constants.js
disableDebugMode() {
console.log('🔧 Disabling debug mode...');
const constantsPath = path.join(this.buildDir, 'config', 'constants.js');
if (!fs.existsSync(constantsPath)) {
console.log('⚠️ Warning: constants.js not found');
return;
}
let content = fs.readFileSync(constantsPath, 'utf8');
// Replace DEBUG.ENABLED: true with DEBUG.ENABLED: false
content = content.replace(
/DEBUG:\s*\{[\s\S]*?ENABLED:\s*true/g,
match => match.replace('ENABLED: true', 'ENABLED: false')
);
// Also disable other debug flags
content = content.replace(/LOG_API_CALLS:\s*true/g, 'LOG_API_CALLS: false');
content = content.replace(/LOG_USER_ACTIONS:\s*true/g, 'LOG_USER_ACTIONS: false');
fs.writeFileSync(constantsPath, content);
console.log('✅ Debug mode disabled');
}
// Validate manifest.json
validateManifest() {
console.log('🔍 Validating manifest.json...');
const manifestPath = path.join(this.buildDir, 'manifest.json');
if (!fs.existsSync(manifestPath)) {
throw new Error('❌ manifest.json not found');
}
let manifest;
try {
const content = fs.readFileSync(manifestPath, 'utf8');
manifest = JSON.parse(content);
} catch (error) {
throw new Error(`❌ Invalid JSON in manifest.json: ${error.message}`);
}
// Check required fields
for (const field of this.requiredManifestFields) {
if (!manifest[field]) {
throw new Error(`❌ Missing required field in manifest.json: ${field}`);
}
}
// Check manifest version
if (manifest.manifest_version !== 3) {
console.log('⚠️ Warning: Not using Manifest V3');
}
// Check if icon files exist
if (manifest.icons) {
for (const [size, iconPath] of Object.entries(manifest.icons)) {
const fullIconPath = path.join(this.buildDir, iconPath);
if (!fs.existsSync(fullIconPath)) {
throw new Error(`❌ Icon file not found: ${iconPath}`);
}
}
}
console.log('✅ Manifest validation passed');
return manifest;
}
// Create ZIP archive
async createArchive(manifest) {
console.log('📦 Creating ZIP archive...');
const archiveName = `coinpeek-v${manifest.version}.zip`;
const archivePath = path.join(this.distDir, archiveName);
return new Promise((resolve, reject) => {
const output = fs.createWriteStream(archivePath);
const archive = archiver('zip', { zlib: { level: 9 } });
output.on('close', () => {
const sizeKB = Math.round(archive.pointer() / 1024);
console.log(`✅ Archive created: ${archiveName} (${sizeKB} KB)`);
resolve(archivePath);
});
archive.on('error', (err) => {
reject(err);
});
archive.pipe(output);
archive.directory(this.buildDir, false);
archive.finalize();
});
}
// Main build process
async build() {
try {
console.log('🚀 Starting build process...\n');
this.createDirectories();
console.log('\n📂 Copying files...');
this.copyFiles(this.sourceDir, this.buildDir);
console.log('\n');
this.disableDebugMode();
console.log('\n');
const manifest = this.validateManifest();
console.log('\n');
const archivePath = await this.createArchive(manifest);
console.log('\n🎉 Build completed successfully!');
console.log(`📦 Archive: ${path.basename(archivePath)}`);
console.log(`📁 Location: ${this.distDir}`);
console.log('\n📋 Next steps:');
console.log(' 1. Test the extension by loading unpacked from build/ folder');
console.log(' 2. Upload the ZIP file to Chrome Web Store');
} catch (error) {
console.error('\n❌ Build failed:', error.message);
process.exit(1);
}
}
}
// Run build if script is executed directly
if (require.main === module) {
const builder = new ExtensionBuilder();
builder.build();
}
module.exports = ExtensionBuilder;