Initial commit
This commit is contained in:
13
packages/zipper/index.mts
Normal file
13
packages/zipper/index.mts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { resolve } from 'node:path';
|
||||
import { zipBundle } from './lib/index.js';
|
||||
import { IS_FIREFOX } from '@extension/env';
|
||||
|
||||
const YYYY_MM_DD = new Date().toISOString().slice(0, 10).replace(/-/g, '');
|
||||
const HH_mm_ss = new Date().toISOString().slice(11, 19).replace(/:/g, '');
|
||||
const fileName = `extension-${YYYY_MM_DD}-${HH_mm_ss}`;
|
||||
|
||||
await zipBundle({
|
||||
distDirectory: resolve(import.meta.dirname, '..', '..', '..', 'dist'),
|
||||
buildDirectory: resolve(import.meta.dirname, '..', '..', '..', 'dist-zip'),
|
||||
archiveName: IS_FIREFOX ? `${fileName}.xpi` : `${fileName}.zip`,
|
||||
});
|
||||
1
packages/zipper/lib/index.ts
Normal file
1
packages/zipper/lib/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './zip-bundle.js';
|
||||
87
packages/zipper/lib/zip-bundle.ts
Normal file
87
packages/zipper/lib/zip-bundle.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { streamFileToZip } from '@extension/dev-utils';
|
||||
import fg from 'fast-glob';
|
||||
import { Zip } from 'fflate';
|
||||
import { createWriteStream, existsSync, mkdirSync } from 'node:fs';
|
||||
import { posix, resolve } from 'node:path';
|
||||
|
||||
const toMB = (bytes: number): number => bytes / 1024 / 1024;
|
||||
|
||||
const ensureBuildDirectoryExists = (buildDirectory: string): void => {
|
||||
if (!existsSync(buildDirectory)) {
|
||||
mkdirSync(buildDirectory, { recursive: true });
|
||||
}
|
||||
};
|
||||
|
||||
const logPackageSize = (size: number, startTime: number): void => {
|
||||
console.log(`Zip Package size: ${toMB(size).toFixed(2)} MB in ${Date.now() - startTime}ms`);
|
||||
};
|
||||
|
||||
export const zipBundle = async (
|
||||
{
|
||||
distDirectory,
|
||||
buildDirectory,
|
||||
archiveName,
|
||||
}: {
|
||||
distDirectory: string;
|
||||
buildDirectory: string;
|
||||
archiveName: string;
|
||||
},
|
||||
withMaps = false,
|
||||
): Promise<void> => {
|
||||
ensureBuildDirectoryExists(buildDirectory);
|
||||
|
||||
const zipFilePath = resolve(buildDirectory, archiveName);
|
||||
const output = createWriteStream(zipFilePath);
|
||||
|
||||
const fileList = await fg(
|
||||
[
|
||||
'**/*', // Pick all nested files
|
||||
...(!withMaps ? ['!**/(*.js.map|*.css.map)'] : []), // Exclude source maps conditionally
|
||||
],
|
||||
{
|
||||
cwd: distDirectory,
|
||||
onlyFiles: true,
|
||||
},
|
||||
);
|
||||
|
||||
return new Promise<void>((pResolve, pReject) => {
|
||||
let aborted = false;
|
||||
let totalSize = 0;
|
||||
const timer = Date.now();
|
||||
const zip = new Zip((err, data, final) => {
|
||||
if (err) {
|
||||
pReject(err);
|
||||
} else {
|
||||
totalSize += data.length;
|
||||
output.write(data);
|
||||
if (final) {
|
||||
logPackageSize(totalSize, timer);
|
||||
output.end();
|
||||
pResolve();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (const file of fileList) {
|
||||
if (aborted) return;
|
||||
|
||||
const absPath = resolve(distDirectory, file);
|
||||
const absPosixPath = posix.resolve(distDirectory, file);
|
||||
const relPosixPath = posix.relative(distDirectory, absPosixPath);
|
||||
|
||||
console.log(`Adding file: ${relPosixPath}`);
|
||||
streamFileToZip(
|
||||
absPath,
|
||||
relPosixPath,
|
||||
zip,
|
||||
() => {
|
||||
aborted = true;
|
||||
zip.terminate();
|
||||
},
|
||||
error => pReject(`Error reading file ${absPath}: ${error.message}`),
|
||||
);
|
||||
}
|
||||
|
||||
zip.end();
|
||||
});
|
||||
};
|
||||
30
packages/zipper/package.json
Normal file
30
packages/zipper/package.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "@extension/zipper",
|
||||
"version": "0.5.0",
|
||||
"description": "chrome extension - zipper",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"sideEffects": false,
|
||||
"files": [
|
||||
"dist/**"
|
||||
],
|
||||
"types": "index.mts",
|
||||
"main": "dist/index.mjs",
|
||||
"scripts": {
|
||||
"clean:bundle": "rimraf dist",
|
||||
"clean:node_modules": "pnpx rimraf node_modules",
|
||||
"clean:turbo": "rimraf .turbo",
|
||||
"clean": "pnpm clean:bundle && pnpm clean:node_modules && pnpm clean:turbo",
|
||||
"zip": "node --env-file=../../.env dist/index.mjs",
|
||||
"lint": "eslint .",
|
||||
"ready": "tsc -b",
|
||||
"lint:fix": "pnpm lint --fix",
|
||||
"format": "prettier . --write --ignore-path ../../.prettierignore",
|
||||
"type-check": "tsc --noEmit"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@extension/tsconfig": "workspace:*",
|
||||
"@extension/dev-utils": "workspace:*",
|
||||
"@extension/env": "workspace:*"
|
||||
}
|
||||
}
|
||||
8
packages/zipper/tsconfig.json
Normal file
8
packages/zipper/tsconfig.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "@extension/tsconfig/module",
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": ["index.mts", "lib"]
|
||||
}
|
||||
Reference in New Issue
Block a user