Skip to content

Commit

Permalink
added support for customizing the file name
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Dec 4, 2022
1 parent 14a6edd commit 89ec4ed
Show file tree
Hide file tree
Showing 4 changed files with 868 additions and 1,020 deletions.
51 changes: 29 additions & 22 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { readFileSync } from 'fs';
import path from 'path';
import findUp from 'find-up';
import yaml from 'yaml';
import log from './utils/logger';
import { readFileSync } from "fs";
import path from "path";
import findUp from "find-up";
import yaml from "yaml";
import log from "./utils/logger";

export enum EXTENSION_TARGET {
CHROME = 'chrome',
MOZILLA = 'mozilla',
CHROME = "chrome",
MOZILLA = "mozilla",
}

export const CONFIG_FILE_NAMES = [
'cwex.yml',
'.cwexrc',
];
export const CONFIG_FILE_NAMES = ["cwex.yml", ".cwexrc"];

interface ManifestIconMap {
16: string;
Expand All @@ -36,7 +33,10 @@ interface ManifestSettingsOptions {
open_in_tab: boolean;
}

type ManifestContentScriptRunAtOption = 'document_idle' | 'document_start' | 'document_end';
type ManifestContentScriptRunAtOption =
| "document_idle"
| "document_start"
| "document_end";
export interface ManifestContentScriptOptions {
matches: string[];
css?: string[];
Expand Down Expand Up @@ -146,7 +146,7 @@ export interface ManifestOptions {
* specify how this extension will behave if allowed to run in incognito mode
* https://developer.chrome.com/extensions/manifest/incognito
*/
incognito?: 'spanning' | 'split' | 'not_allowed';
incognito?: "spanning" | "split" | "not_allowed";

/**
* the version of chrome that your extension requires
Expand Down Expand Up @@ -195,6 +195,9 @@ export interface CwexConfig {
/** directory where the build would be compiled to */
outDir: string;

/** name of the file for the compiled extension */
outFile?: string;

/** path to script to execute before compiling extension */
beforeCompile?: string;

Expand All @@ -211,41 +214,45 @@ export interface ExtensionInfo {
fileType: string;
}

export type ExtensionInfoGenerator = (config: CwexConfig) => Promise<ExtensionInfo>;
export type ExtensionInfoGenerator = (
config: CwexConfig
) => Promise<ExtensionInfo>;

export interface ExtensionCompilerOption {
extensionFilesDir: string;
extensionBuildOutputDir: string;
config: CwexConfig;
}

export type ExtensionCompiler = (opts: ExtensionCompilerOption) => Promise<boolean>;
export type ExtensionCompiler = (
opts: ExtensionCompilerOption
) => Promise<boolean>;

export const defaultConfig: CwexConfig = {
include: [],
exclude: [],
targets: [...Object.values(EXTENSION_TARGET)],
rootDir: './',
outDir: 'out',
rootDir: "./",
outDir: "out",
};

export const getConfigFile = async () => {
return await findUp(CONFIG_FILE_NAMES);
};

export const getConfig = async (configPath = '') => {
export const getConfig = async (configPath = "") => {
const pathToConfig = configPath ? configPath : await getConfigFile();
let config = { ...defaultConfig };
if (pathToConfig) {
log('Config file found:', pathToConfig);
log("Config file found:", pathToConfig);
try {
config = { ...config, ...yaml.parse(readFileSync(pathToConfig, 'utf8')) };
config = { ...config, ...yaml.parse(readFileSync(pathToConfig, "utf8")) };
} catch (err) {
log('The config file is invalid. Check that you have a valid YAML file.');
log("The config file is invalid. Check that you have a valid YAML file.");
throw err;
}
} else {
log('Config file not found.');
log("Config file not found.");
}

return config;
Expand Down
58 changes: 34 additions & 24 deletions src/targets/chrome.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import fs from "fs";
import path from "path";
import {
CwexConfig,
ManifestIcons,
Expand All @@ -8,10 +8,10 @@ import {
ExtensionCompiler,
IDictionary,
ManifestContentScriptOptions,
} from '../config';
import archiver from 'archiver';
import { ensureFile } from 'fs-extra';
import log from '../utils/logger';
} from "../config";
import archiver from "archiver";
import { ensureFile } from "fs-extra";
import log from "../utils/logger";

interface ChromeExtensionBrowserAction {
default_icon?: ManifestIcons;
Expand Down Expand Up @@ -45,14 +45,16 @@ interface ChromeExtension {
content_scripts?: IDictionary<ManifestContentScriptOptions>;
devtools_page?: string;
homepage_url?: string;
incognito?: 'spanning' | 'split' | 'not_allowed';
incognito?: "spanning" | "split" | "not_allowed";
minimum_chrome_version?: string;
omnibox?: IDictionary;
storage?: IDictionary;
web_accessible_resources?: IDictionary;
}

const buildBrowserAction = (config: CwexConfig): ChromeExtensionBrowserAction => {
const buildBrowserAction = (
config: CwexConfig
): ChromeExtensionBrowserAction => {
if (config.manifestOptions?.browser_action) {
return {
default_icon: config.manifestOptions.browser_action.default_icon,
Expand All @@ -61,7 +63,9 @@ const buildBrowserAction = (config: CwexConfig): ChromeExtensionBrowserAction =>
return {};
};

const buildOptionsUi = (config: CwexConfig): ChromeExtensionOptionsUi | undefined => {
const buildOptionsUi = (
config: CwexConfig
): ChromeExtensionOptionsUi | undefined => {
if (config.manifestOptions?.options_ui) {
return {
page: config.manifestOptions.options_ui.page,
Expand Down Expand Up @@ -105,28 +109,34 @@ const buildExtensionData = (config: CwexConfig): ChromeExtension | null => {
};
};

export const generateExtensionInfo = async(config: CwexConfig): Promise<ExtensionInfo> => {
export const generateExtensionInfo = async (
config: CwexConfig
): Promise<ExtensionInfo> => {
const extensionData = buildExtensionData(config);
return {
content: extensionData ? JSON.stringify(extensionData, null, 2): '',
fileName: 'manifest.json',
fileType: 'json',
content: extensionData ? JSON.stringify(extensionData, null, 2) : "",
fileName: "manifest.json",
fileType: "json",
};
};

export const compileExtension: ExtensionCompiler = async(opts: ExtensionCompilerOption) => {
const outputPath = path.resolve(opts.extensionBuildOutputDir, 'chrome.zip');
export const compileExtension: ExtensionCompiler = async (
opts: ExtensionCompilerOption
) => {
const outputPath = path.resolve(
opts.extensionBuildOutputDir,
opts.config.outFile ?? "chrome.zip"
);
await ensureFile(outputPath);
const output = fs.createWriteStream(outputPath);

return new Promise((resolve, reject) => {

// zip extension files
const archive = archiver('zip');
const archive = archiver("zip");

// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
output.on('close', () => {
output.on("close", () => {
// log(archive.pointer() + ' total bytes');
// log('archiver has been finalized and the output file descriptor has closed.');
return resolve(true);
Expand All @@ -135,14 +145,14 @@ export const compileExtension: ExtensionCompiler = async(opts: ExtensionCompiler
// This event is fired when the data source is drained no matter what was the data source.
// It is not part of this library but rather from the NodeJS Stream API.
// @see: https://nodejs.org/api/stream.html#stream_event_end
output.on('end', () => {
log('Data has been drained');
output.on("end", () => {
log("Data has been drained");
return resolve(true);
});

// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', (err) => {
if (err.code === 'ENOENT') {
archive.on("warning", (err) => {
if (err.code === "ENOENT") {
// log warning
log(err);
} else {
Expand All @@ -152,13 +162,13 @@ export const compileExtension: ExtensionCompiler = async(opts: ExtensionCompiler
});

// good practice to catch this error explicitly
archive.on('error', (err) => {
archive.on("error", (err) => {
return reject(err);
});

// pipe archive data to the file
archive.pipe(output);

// append files from a sub-directory, putting its contents at the root of archive
archive.directory(opts.extensionFilesDir, false);

Expand Down
26 changes: 19 additions & 7 deletions src/targets/mozilla.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { CwexConfig, ManifestIcons, ManifestBrowserAction, ExtensionInfo, ExtensionCompiler, ManifestBrowserSpecificSettings } from '../config';
const webExt = require('web-ext').default;
import {
CwexConfig,
ManifestIcons,
ManifestBrowserAction,
ExtensionInfo,
ExtensionCompiler,
ManifestBrowserSpecificSettings,
} from "../config";
const webExt = require("web-ext").default;

interface MozillaAddonBackground {
scripts: string[];
Expand Down Expand Up @@ -37,7 +44,9 @@ const buildBrowserAction = (config: CwexConfig): ManifestBrowserAction => {
return {};
};

const buildOptionsUi = (config: CwexConfig): MozillaAddonOptionsUi | undefined => {
const buildOptionsUi = (
config: CwexConfig
): MozillaAddonOptionsUi | undefined => {
if (config.manifestOptions?.options_ui) {
return {
page: config.manifestOptions.options_ui.page,
Expand Down Expand Up @@ -70,12 +79,14 @@ const buildExtensionData = (config: CwexConfig): MozillaAddon | null => {
};
};

export const generateExtensionInfo = async(config: CwexConfig): Promise<ExtensionInfo> => {
export const generateExtensionInfo = async (
config: CwexConfig
): Promise<ExtensionInfo> => {
const extensionData = buildExtensionData(config);
return {
content: extensionData ? JSON.stringify(extensionData, null, 2): '',
fileName: 'manifest.json',
fileType: 'json',
content: extensionData ? JSON.stringify(extensionData, null, 2) : "",
fileName: "manifest.json",
fileType: "json",
};
};

Expand All @@ -86,5 +97,6 @@ export const compileExtension: ExtensionCompiler = async (opts) => {
sourceDir: opts.extensionFilesDir,
overwriteDest: true,
artifactsDir: opts.extensionBuildOutputDir,
filename: opts.config.outFile,
});
};
Loading

0 comments on commit 89ec4ed

Please sign in to comment.