-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·206 lines (175 loc) · 5.5 KB
/
Copy pathbuild.js
File metadata and controls
executable file
·206 lines (175 loc) · 5.5 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// Read version from package.json
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const version = packageJson.version;
console.log(`Building Markdown Printer v${version}...`);
// Determine which builds to create
const args = process.argv.slice(2);
const buildChrome = args.length === 0 || args.includes('chrome');
const buildFirefox = args.length === 0 || args.includes('firefox');
// Shared source files (copied from src/ to both extensions)
const sharedSourceFiles = [
'background.js',
'log-buffer.js',
'logger.js',
'rating-policy.js',
'popup.html',
'popup.js',
'turndown.js',
'icon16.png',
'icon48.png',
'icon128.png',
];
// Common directories to copy
const commonDirs = ['_locales'];
// Manifest templates
const chromeManifest = {
manifest_version: 3,
name: '__MSG_extensionName__',
version: version,
description: '__MSG_extensionDescription__',
default_locale: 'en',
author: 'Lev Gelfenbuim',
homepage_url: 'https://github.com/levz0r/markdown-printer',
permissions: ['activeTab', 'contextMenus', 'downloads', 'scripting'],
background: {
service_worker: 'background.js',
},
icons: {
16: 'icon16.png',
48: 'icon48.png',
128: 'icon128.png',
},
action: {
default_popup: 'popup.html',
default_icon: {
16: 'icon16.png',
48: 'icon48.png',
128: 'icon128.png',
},
},
};
const firefoxManifest = {
...chromeManifest,
browser_specific_settings: {
gecko: {
id: 'markdown-printer@lev.engineer',
strict_min_version: '121.0',
data_collection_permissions: {
required: ['none'],
},
},
},
background: {
// Firefox MV3 runs background.scripts in an event-page context where
// importScripts() is unavailable, so logger and its dependency must be
// listed here. Order matters: log-buffer.js exposes helpers consumed
// by logger.js, which in turn exposes mdpLog used by background.js.
scripts: ['log-buffer.js', 'logger.js', 'background.js'],
},
};
// Function to ensure directory exists
function ensureDir(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
// Function to copy files
function copyFiles(sourceDir, destDir, files) {
files.forEach(file => {
const sourcePath = path.join(sourceDir, file);
const destPath = path.join(destDir, file);
if (fs.existsSync(sourcePath)) {
fs.copyFileSync(sourcePath, destPath);
console.log(` ✓ Copied ${file}`);
} else {
console.warn(` ⚠ Warning: ${file} not found in ${sourceDir}`);
}
});
}
// Function to copy directories recursively
function copyDir(src, dest) {
if (!fs.existsSync(src)) {
console.warn(` ⚠ Warning: Directory ${src} not found`);
return;
}
ensureDir(dest);
const entries = fs.readdirSync(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
console.log(` ✓ Copied directory ${path.basename(src)}/`);
}
// Function to create zip package
function createZip(sourceDir, outputName) {
try {
const command = `cd ${sourceDir} && zip -r ../${outputName} . -x "*.DS_Store"`;
execSync(command, { stdio: 'inherit' });
console.log(` ✓ Created ${outputName}`);
} catch (error) {
console.error(` ✗ Failed to create ${outputName}:`, error.message);
}
}
// Build Chrome version
if (buildChrome) {
console.log('\n📦 Building Chrome version...');
const chromeDir = 'extension-chrome';
ensureDir(chromeDir);
// Copy shared source files from src/
copyFiles('src', chromeDir, sharedSourceFiles);
// Copy common directories (like _locales)
commonDirs.forEach(dir => {
copyDir(dir, path.join(chromeDir, dir));
});
// Write manifest
fs.writeFileSync(path.join(chromeDir, 'manifest.json'), JSON.stringify(chromeManifest, null, 2));
console.log(' ✓ Updated manifest.json');
// Create dist directory
ensureDir('dist');
// Create zip package
createZip(chromeDir, `dist/markdown-printer-chrome-v${version}.zip`);
}
// Build Firefox version
if (buildFirefox) {
console.log('\n🦊 Building Firefox version...');
const firefoxDir = 'extension-firefox';
ensureDir(firefoxDir);
// Copy shared source files from src/
copyFiles('src', firefoxDir, sharedSourceFiles);
// Copy common directories (like _locales)
commonDirs.forEach(dir => {
copyDir(dir, path.join(firefoxDir, dir));
});
// Write manifest
fs.writeFileSync(
path.join(firefoxDir, 'manifest.json'),
JSON.stringify(firefoxManifest, null, 2)
);
console.log(' ✓ Updated manifest.json');
// Create dist directory
ensureDir('dist');
// Create zip package
createZip(firefoxDir, `dist/markdown-printer-firefox-v${version}.zip`);
}
console.log('\n✅ Build complete!\n');
console.log('📦 Packages created in dist/ directory');
console.log(` Version: ${version}`);
if (buildChrome) {
console.log(` - markdown-printer-chrome-v${version}.zip`);
}
if (buildFirefox) {
console.log(` - markdown-printer-firefox-v${version}.zip`);
}
console.log('\nTo bump version and rebuild:');
console.log(' npm run version:patch # 1.0.0 -> 1.0.1');
console.log(' npm run version:minor # 1.0.0 -> 1.1.0');
console.log(' npm run version:major # 1.0.0 -> 2.0.0');