Initial commit
This commit is contained in:
2
packages/dev-utils/lib/index.ts
Normal file
2
packages/dev-utils/lib/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './stream-file-to-zip.js';
|
||||
export * from './manifest-parser/index.js';
|
||||
39
packages/dev-utils/lib/manifest-parser/impl.ts
Normal file
39
packages/dev-utils/lib/manifest-parser/impl.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { IManifestParser } from './types.js';
|
||||
import type { ManifestType } from '@extension/shared';
|
||||
|
||||
const convertToFirefoxCompatibleManifest = (manifest: ManifestType) => {
|
||||
const manifestCopy = {
|
||||
...manifest,
|
||||
} as { [key: string]: unknown };
|
||||
|
||||
if (manifest.background?.service_worker) {
|
||||
manifestCopy.background = {
|
||||
scripts: [manifest.background.service_worker],
|
||||
type: 'module',
|
||||
};
|
||||
}
|
||||
if (manifest.options_page) {
|
||||
manifestCopy.options_ui = {
|
||||
page: manifest.options_page,
|
||||
browser_style: false,
|
||||
};
|
||||
}
|
||||
manifestCopy.content_security_policy = {
|
||||
extension_pages: "script-src 'self'; object-src 'self'",
|
||||
};
|
||||
manifestCopy.permissions = (manifestCopy.permissions as string[]).filter(value => value !== 'sidePanel');
|
||||
|
||||
delete manifestCopy.options_page;
|
||||
delete manifestCopy.side_panel;
|
||||
return manifestCopy as ManifestType;
|
||||
};
|
||||
|
||||
export const ManifestParserImpl: IManifestParser = {
|
||||
convertManifestToString: (manifest, isFirefox) => {
|
||||
if (isFirefox) {
|
||||
manifest = convertToFirefoxCompatibleManifest(manifest);
|
||||
}
|
||||
|
||||
return JSON.stringify(manifest, null, 2);
|
||||
},
|
||||
};
|
||||
4
packages/dev-utils/lib/manifest-parser/index.ts
Normal file
4
packages/dev-utils/lib/manifest-parser/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { ManifestParserImpl } from './impl.js';
|
||||
|
||||
export type * from './types.js';
|
||||
export const ManifestParser = ManifestParserImpl;
|
||||
5
packages/dev-utils/lib/manifest-parser/types.ts
Normal file
5
packages/dev-utils/lib/manifest-parser/types.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { ManifestType } from '@extension/shared';
|
||||
|
||||
export interface IManifestParser {
|
||||
convertManifestToString: (manifest: ManifestType, isFirefox: boolean) => string;
|
||||
}
|
||||
24
packages/dev-utils/lib/stream-file-to-zip.ts
Normal file
24
packages/dev-utils/lib/stream-file-to-zip.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { AsyncZipDeflate } from 'fflate';
|
||||
import { createReadStream } from 'node:fs';
|
||||
import type { Zip } from 'fflate';
|
||||
|
||||
export const streamFileToZip = (
|
||||
absPath: string,
|
||||
relPath: string,
|
||||
zip: Zip,
|
||||
onAbort: () => void,
|
||||
onError: (error: Error) => void,
|
||||
): void => {
|
||||
const data = new AsyncZipDeflate(relPath, { level: 1 });
|
||||
void zip.add(data);
|
||||
|
||||
createReadStream(absPath)
|
||||
.on('data', (chunk: string | Buffer) =>
|
||||
typeof chunk === 'string' ? data.push(Buffer.from(chunk), false) : data.push(chunk, false),
|
||||
)
|
||||
.on('end', () => data.push(new Uint8Array(0), true))
|
||||
.on('error', error => {
|
||||
onAbort();
|
||||
onError(error);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user