-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathzip-src.ts
More file actions
120 lines (98 loc) · 3.15 KB
/
zip-src.ts
File metadata and controls
120 lines (98 loc) · 3.15 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
import archiver from 'archiver';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as url from 'node:url';
const DEST_DIR = url.fileURLToPath(new URL('../dist-src', import.meta.url));
async function main() {
console.log('Creating dest directory...');
await ensureDir(DEST_DIR);
const versionString = getPackageVersion();
const zipFilename = `10ten-ja-reader-${versionString}-src.zip`;
console.log(`Writing to ${zipFilename}...`);
const zipPath = path.join(DEST_DIR, zipFilename);
if (fs.existsSync(zipPath)) {
console.log('(Replacing existing file)');
fs.unlinkSync(zipPath);
}
const zipFile = fs.createWriteStream(path.join(DEST_DIR, zipFilename));
const archive = archiver('zip', { zlib: { level: 9 } });
archive.on('warning', (err: any) => {
if (err?.code === 'ENOENT') {
console.warn('File not found');
console.warn(err);
} else {
throw err;
}
});
archive.on('error', function (err: unknown) {
throw err;
});
const finishWritePromise = new Promise<void>((resolve) => {
zipFile.on('close', resolve);
});
archive.pipe(zipFile);
const rootDir = url.fileURLToPath(new URL('..', import.meta.url));
const dirs = ['_locales', 'css', 'data', 'docs', 'fonts', 'images'];
for (const dir of dirs) {
archive.directory(path.join(rootDir, dir), dir);
}
// Add only the critical files from the tests folder
//
// These are needed for the build task to work and for the start:firefox and
// start:chrome tasks to show something useful.
archive.file(path.join(rootDir, 'tests', 'content-loader.ts'), {
name: 'tests/content-loader.ts',
});
archive.file(path.join(rootDir, 'tests', 'browser-polyfill.ts'), {
name: 'tests/browser-polyfill.ts',
});
archive.glob('tests/playground.*', { cwd: rootDir });
archive.glob('tests/playground-*.*', { cwd: rootDir });
// Add `src` while excluding fixtures
archive.glob('src/**/*', { cwd: rootDir, ignore: '**/*.fixture.tsx' });
const files = [
'CHANGELOG.md',
'CONTRIBUTING.md',
'README.md',
'manifest.json.src',
'package.json',
'pnpm-lock.yaml',
'postcss.config.js',
'rspack.config.js',
'scripts/postcss-property-to-custom-prop.js',
'tsconfig.json',
];
for (const file of files) {
archive.file(path.join(rootDir, file), { name: file });
}
await archive.finalize();
await finishWritePromise;
console.log(`Wrote ${archive.pointer()} bytes`);
}
async function ensureDir(dir: string) {
return new Promise<void>((resolve, reject) => {
fs.mkdir(dir, { recursive: true }, (err) =>
err && err.code !== 'EEXIST' ? reject(err) : resolve()
);
});
}
function getPackageVersion() {
const packageJsonPath = url.fileURLToPath(
new URL('../package.json', import.meta.url)
);
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
const versionString = packageJson.version;
if (!versionString) {
throw new Error('Could not find version in package.json');
}
return versionString;
}
main()
.then(() => {
console.log('Done.');
process.exit(0);
})
.catch((err) => {
console.error(err);
process.exit(1);
});