diff --git a/lib/base64loader.ts b/lib/base64loader.ts index 1d2e6b95..120aa297 100644 --- a/lib/base64loader.ts +++ b/lib/base64loader.ts @@ -1,41 +1,8 @@ -import fs from "fs"; +import fs from "node:fs"; import mime from "mime-types"; -/** - * A Vite plugin designed to load files as base64 encoded data URLs. - * This plugin intercepts module imports that have a `?base64` query parameter - * appended to the file path. It then reads the targeted file, converts its content - * to a base64 string, and constructs a data URL which is then exported as the - * default export of a new JavaScript module. - * - * @example - * // To use this loader, import a file with ?base64 query: - * // import myImageBase64 from './path/to/myimage.png?base64'; - * // myImageBase64 will then be a string like "data:image/png;base64,..." - */ export const base64Loader = { - /** - * The name of the Vite plugin. - * @type {string} - */ name: "base64-loader", - /** - * The core transformation function of the Vite plugin. - * It is called by Vite for modules that might need transformation. This function - * checks if the module ID includes the `?base64` query. If so, it reads the - * specified file, converts it to a base64 data URL, and returns a new - * JavaScript module that default exports this data URL. - * - * @param {any} _ The original code of the file. This parameter is unused by this loader. - * @param {string} id The ID of the module being transformed. This string typically - * contains the absolute file path and any query parameters - * (e.g., "/path/to/file.png?base64"). - * @returns {string | null} If the module ID does not contain `?base64` query, - * it returns `null` to indicate no transformation. - * Otherwise, it returns a string of JavaScript code - * that default exports the base64 data URL of the file. - * For example: `export default 'data:image/png;base64,xxxx';` - */ transform(_: any, id: string) { const [filePath, query] = id.split("?"); if (query !== "base64") return null; diff --git a/lib/createManifest.ts b/lib/createManifest.ts index f64027ee..4623f9bf 100644 --- a/lib/createManifest.ts +++ b/lib/createManifest.ts @@ -1,46 +1,5 @@ import type { Browser, BuildTarget, Manifest } from "./types.ts"; -import type { AnyCase } from "./utils.ts"; -/** - * Packages a given manifest object with a specific target browser identifier. - * This function is typically used in multi-browser extension build processes - * to create a configuration object that pairs the manifest data with the browser - * it's intended for. The `AnyCase` type for the browser parameter - * implies that browser names like 'chrome', 'firefox', etc., can be provided - * in various casings. - * - * @export - * @param {Manifest} manifest The core manifest data for the extension, - * compatible with `chrome.runtime.ManifestV3` as defined by the {@link Manifest} type. - * @param {AnyCase} browser The target browser identifier (e.g., 'chrome', 'firefox', 'CHROME'). - * Refers to the {@link Browser} type, allowing for flexible casing. - * @returns {BuildTarget} An object that pairs the `manifest` with its target `browser`. - * The structure is `{ manifest: Manifest; browser: AnyCase; }` - * as defined by the {@link BuildTarget} type. - */ -export function createManifest( - manifest: Manifest, - browser: AnyCase, -): BuildTarget { - return { - manifest, - browser, - }; -} - -/** - * Defines a base manifest object. - * This function is typically used to establish a common, shared foundation for an extension's manifest - * (compatible with `chrome.runtime.ManifestV3` as per the {@link Manifest} type). - * This base can then be extended or modified for different browsers or specific build configurations. - * For example, you might define core permissions and properties here, and then add - * browser-specific keys in subsequent steps. - * - * @export - * @param {Manifest} manifest The core manifest data to be used as a base. - * This should conform to the {@link Manifest} type structure. - * @returns {Manifest} The provided manifest object, intended to serve as a reusable base. - */ -export function createManifestBase(manifest: Manifest): Manifest { - return manifest; +export function createManifest(manifest: Manifest, browser: Browser): BuildTarget { + return { manifest, browser }; } diff --git a/lib/publish.js b/lib/publish.js index 8d4117bd..94a227d2 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -15,18 +15,6 @@ import { globSync } from "glob"; import semver from "semver"; import { execSync } from "node:child_process"; -/** - * Determines the latest version string from a list of filenames that include version numbers. - * Filenames are expected to follow a pattern like `betterseqtaplus@3.4.5.1-chrome.zip`. - * This function handles potential 4-part versions (e.g., `3.4.5.1`) by trimming them - * to 3 parts (e.g., `3.4.5`) for comparison using the `semver` library. After identifying - * the latest semver-compatible version, it returns the original full version string - * (e.g., "3.4.5.1") that corresponds to this latest version. - * - * @param {string[]} files An array of filenames. - * @returns {string | null} The latest version string (e.g., "3.4.5.1") found among the files, - * or `null` if no valid version numbers are found or no files are provided. - */ function getLatestVersion(files) { console.log("Files passed to getLatestVersion:", files); @@ -82,17 +70,6 @@ function getLatestVersion(files) { return latestFullVersion; } -/** - * Finds the path to the latest built ZIP file for a specific browser. - * It constructs a glob pattern based on the browser name (e.g., `dist/betterseqtaplus@*-*chrome.zip`), - * finds all matching files, and then uses `getLatestVersion` to identify the version string - * of the most recent file. Finally, it returns the full path to that specific file. - * - * @param {string} browser A string indicating the target browser (e.g., "chrome", "firefox"). - * @returns {string | undefined} The filepath string to the latest ZIP file for the specified browser, - * or `undefined` if no matching file is found or if the latest version - * cannot be determined. - */ function getLatestFiles(browser) { const pattern = `dist/betterseqtaplus@*-*${browser}.zip`; console.log("Glob pattern:", pattern); @@ -118,14 +95,6 @@ function getLatestFiles(browser) { return latestFile; } -/** - * Creates a ZIP file of the project's source code, excluding specified development-related - * files and directories such as `node_modules`, `dist`, `.git`, etc. - * It uses the `7z` command-line tool to perform the archiving. - * The output filename is fixed as `dist/betterseqtaplus@latest-sources.zip`. - * - * @returns {string} The filename of the created ZIP file (e.g., `dist/betterseqtaplus@latest-sources.zip`). - */ function zipSources() { const zipFileName = `dist/betterseqtaplus@latest-sources.zip`; @@ -151,19 +120,6 @@ function zipSources() { return zipFileName; } -/** - * Orchestrates the extension publishing process for the specified browsers. - * This function performs the following steps: - * 1. Calls `getLatestFiles` to find the latest built ZIP for Chrome if "chrome" is in `browsers`. - * 2. Calls `getLatestFiles` to find the latest built ZIP for Firefox if "firefox" is in `browsers`. - * 3. Calls `zipSources` to create a source code ZIP if "firefox" is in `browsers` (required for Mozilla Add-ons). - * 4. Validates that all required files were found and that at least one browser was specified. Exits if not. - * 5. Constructs the `publish-extension` command-line string with the appropriate arguments - * based on the found ZIP files for the specified browsers. - * 6. Executes the constructed `publish-extension` command. - * - * @param {string[]} browsers An array of browser strings (e.g., ["chrome", "firefox"]) for which to publish the extension. - */ function runPublishCommand(browsers) { const chromeZip = browsers.includes("chrome") ? getLatestFiles("chrome") diff --git a/lib/types.ts b/lib/types.ts index e18318d1..d46f3cd8 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1,240 +1,10 @@ import type { ManifestV3Export } from "@crxjs/vite-plugin"; -import { type AnyCase, createEnum, ObjectValues } from "./utils.ts"; -/** - * Enumerates supported JavaScript frameworks for project generation or configuration. - */ -export const FrameworkEnum = { - React: "React", - Vanilla: "Vanilla", - Preact: "Preact", - Lit: "Lit", - Svelte: "Svelte", - Vue: "Vue", -} as const; - -/** - * Enumerates supported web browsers, typically for targeting builds or configurations. - */ -export const BrowserEnum = { - Chrome: "Chrome", - Brave: "Brave", - Opera: "Opera", - Edge: "Edge", - Firefox: "Firefox", - Safari: "Safari", -} as const; - -/** - * @private - * Enumerates supported programming languages for project setup. - * This enum is not exported, suggesting it's for internal use within this module or related modules. - */ -const LanguageEnum = { - TypeScript: "TypeScript", - JavaScript: "JavaScript", -} as const; - -/** - * Enumerates supported styling options or libraries. - */ -export const StyleEnum = { - Tailwind: "Tailwind", -} as const; - -/** - * Enumerates supported package managers. - */ -export const PackageManagerEnum = { - Bun: "Bun", - PnPm: "PnPm", - Npm: "Npm", - Yarn: "Yarn", -} as const; - -/** - * Defines the structure for browser-specific settings within a web extension manifest. - * This is particularly used for Firefox (gecko) extensions to specify properties like - * an extension ID, and minimum/maximum supported browser versions. - * The structure is based on common manifest extensions for Firefox. - * See: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/browser_specific_settings - * The link in the original code (// see: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/firefox-webext-browser/index.d.ts) - * also points to type definitions that include this structure. - * - * @property {object} [browser_specific_settings] - Container for browser-specific settings. - * @property {object} [browser_specific_settings.gecko] - Settings specific to Gecko-based browsers (e.g., Firefox). - * @property {string} [browser_specific_settings.gecko.id] - The unique identifier for the extension in Firefox. - * @property {string} [browser_specific_settings.gecko.strict_min_version] - The minimum version of Firefox the extension is compatible with. - * @property {string} [browser_specific_settings.gecko.strict_max_version] - The maximum version of Firefox the extension is compatible with. - */ -export type BrowserSpecificSettings = { - browser_specific_settings?: { - gecko?: { - id: string; - strict_min_version?: string; - strict_max_version?: string; - }; - }; -}; - -/** - * Represents the structure of a Chrome Manifest V3 file. - * This type is an alias for `ManifestV3Export` from the `@crxjs/vite-plugin`, - * which provides a comprehensive definition for Chrome extension manifests. - */ export type Manifest = ManifestV3Export; -/** Alias for the `icons` property within a Chrome Manifest V3. */ -export type ManifestIcons = chrome.runtime.ManifestIcons; -/** Alias for the `background` property within a Chrome Manifest V3. */ -export type ManifestBackground = chrome.runtime.ManifestV3["background"]; -/** Alias for the `content_scripts` property within a Chrome Manifest V3. */ -export type ManifestContentScripts = - chrome.runtime.ManifestV3["content_scripts"]; -/** Alias for the `web_accessible_resources` property within a Chrome Manifest V3. */ -export type ManifestWebAccessibleResources = - chrome.runtime.ManifestV3["web_accessible_resources"]; -/** Alias for the `commands` property within a Chrome Manifest V3. */ -export type ManifestCommands = chrome.runtime.ManifestV3["commands"]; -/** Alias for the `action` property (or `browser_action`/`page_action`) within a Chrome Manifest V3. */ -export type ManifestAction = chrome.runtime.ManifestV3["action"]; -/** Alias for the `permissions` property within a Chrome Manifest V3. */ -export type ManifestPermissions = chrome.runtime.ManifestV3["permissions"]; -/** Alias for the `options_ui` property within a Chrome Manifest V3. */ -export type ManifestOptionsUI = chrome.runtime.ManifestV3["options_ui"]; -/** Alias for the `chrome_url_overrides` property within a Chrome Manifest V3. */ -export type ManifestURLOverrides = - chrome.runtime.ManifestV3["chrome_url_overrides"]; +export type Browser = "chrome" | "brave" | "edge" | "firefox" | "opera" | "safari"; -/** - * Creates a type that accepts a string literal `T` in either its capitalized or lowercase form. - * Useful for defining types that should be case-insensitive for specific known strings. - * @template T - A string literal type. - */ -export type BrowserName = Capitalize | Lowercase; - -/** - * Creates a record type where both keys and values are derived from a string literal `T`, - * specifically using `BrowserName` which allows for capitalized or lowercase forms. - * This could be used to define an object where, for example, keys are 'Chrome' or 'chrome' - * and values are also 'Chrome' or 'chrome'. - * @template T - A string literal type, typically representing a browser name. - */ -export type BrowserEnumType = { - [browser in BrowserName]: BrowserName; -}; - -/** - * Represents the target browser for a build, allowing for various casings of browser names - * (e.g., "chrome", "Chrome", "CHROME") through the `AnyCase` utility type. - * `Browser` itself is a union of specific browser name strings (e.g., "Chrome" | "Firefox"). - */ -export type BuildMode = AnyCase; - -/** - * Defines an object structure that pairs a web extension `Manifest` - * with its target `browser` (represented as `AnyCase`). - * This is commonly used in build processes to manage configurations for different browsers. - */ export type BuildTarget = { manifest: Manifest; - browser: AnyCase; + browser: Browser; }; - -/** - * Defines the configuration options for a build process. - * @property {"build" | "serve"} [command] - The type of build command (e.g., 'build' for production, 'serve' for development). - * @property {AnyCase | string | undefined} [mode] - The target build mode, typically a browser name (allowing various casings) - * or potentially other custom mode strings. - */ -export type BuildConfig = { - command?: "build" | "serve"; - mode?: AnyCase | string | undefined; -}; - -/** - * Defines the structure for repository information, commonly found in `package.json`. - * @property {string} type - The type of the repository (e.g., "git"). - * @property {string} [url] - The URL of the repository. - * @property {Bugs} [bugs] - An object containing information about where to report bugs. - */ -export interface Repository { - type: string; - url?: string; - bugs?: Bugs; -} - -/** - * Defines the structure for bug reporting information, often part of the `Repository` interface. - * @property {string} [url] - The URL of the issue tracker. - * @property {string} [email] - The email address for reporting bugs. - */ -export interface Bugs { - url?: string; - email?: string; -} - -/** - * A string literal union type representing supported browser names, derived from the values of `BrowserEnum`. - * e.g., "Chrome" | "Firefox" | ... - */ -export type Browser = ObjectValues; - -/** - * A constant intended to provide access to browser names, potentially in various casings. - * Its type `AnyCase` suggests it can be used where case-insensitivity for browser names is needed. - * The `createEnum(BrowserEnum)` call aims to produce a representation of browser names from `BrowserEnum`. - * Note: `createEnum` from `lib/utils.ts` has a declared return type of `ObjectValues` (a union of values), - * while its implementation uses `Object.values()` which returns an array. This constant will hold the - * runtime array value, but its JSDoc type refers to the more restrictive `AnyCase` union type. - */ -export const Browser: AnyCase = createEnum(BrowserEnum); - -/** - * A string literal union type representing supported package managers, derived from the values of `PackageManagerEnum`. - * e.g., "Bun" | "PnPm" | "Npm" | "Yarn" - */ -export type PackageManager = ObjectValues; -/** - * A constant intended to provide access to package manager names, potentially in various casings. - * Its type `AnyCase` suggests it can be used where case-insensitivity for package manager names is needed. - * Utilizes `createEnum(PackageManagerEnum)`. Refer to notes on `Browser` constant regarding `createEnum` behavior. - */ -export const PackageManager: AnyCase = - createEnum(PackageManagerEnum); - -/** - * A string literal union type representing supported JavaScript frameworks, derived from the values of `FrameworkEnum`. - * e.g., "React" | "Vanilla" | ... - */ -export type Framework = ObjectValues; -/** - * A constant intended to provide access to framework names, potentially in various casings. - * Its type `AnyCase` suggests it can be used where case-insensitivity for framework names is needed. - * Utilizes `createEnum(FrameworkEnum)`. Refer to notes on `Browser` constant regarding `createEnum` behavior. - */ -export const Framework: AnyCase = createEnum(FrameworkEnum); - -/** - * A string literal union type representing supported styling options, derived from the values of `StyleEnum`. - * e.g., "Tailwind" - */ -export type Style = ObjectValues; -/** - * A constant intended to provide access to style option names, potentially in various casings. - * Its type `AnyCase