{"size": 1595, "ext": "js", "max_stars_repo_path": "tools/build.js", "max_stars_repo_name": "danieldiekmeier/preact-devtools", "max_stars_repo_stars_event_min_datetime": "2019-11-15T14:23:31.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-07T10:45:37.000Z", "max_issues_repo_path": "tools/build.js", "max_issues_repo_name": "danieldiekmeier/preact-devtools", "max_issues_repo_issues_event_min_datetime": "2019-11-23T09:48:14.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-09T11:42:04.000Z", "max_forks_repo_path": "tools/build.js", "max_forks_repo_name": "Stanislav1975/preact-devtools", "max_forks_repo_forks_event_min_datetime": "2019-11-27T23:14:42.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-19T16:37:53.000Z"}
{"max_stars_count": 262, "max_issues_count": 116, "max_forks_count": 28, "avg_line_length": 26.1475409836, "max_line_length": 77, "alphanum_fraction": 0.6501567398}
/* eslint-disable @typescript-eslint/no-var-requires,no-console */
const archiver = require("archiver");
const fs = require("fs");
const path = require("path");

const browser = process.env.BROWSER;

// Manually bundle injection code to make sure it runs before everything else
const dist = path.join(__dirname, "..", "dist", browser);
const source = path.join(dist, "installHook.js");
const target = path.join(dist, "content-script.js");
const hook = fs
	.readFileSync(source, "utf8")
	.replace(/^\(function \(\) \{/g, "function install() {")
	.replace(/\}\(\)\);[\s\n]*$/g, "}");

let targetFile = fs
	.readFileSync(target, "utf8")
	.replace(/^\(function \(\) \{/g, "")
	.replace(/\}\(\)\);[\s\n]*$/g, "")
	.replace(/"use\sstrict";/g, "")
	.replace(/['"]CODE_TO_INJECT['"]/g, "installHook.toString()");

targetFile = `;(function () {
	"use strict";

	let installHook = ${hook}

	${targetFile}
}());`;
fs.writeFileSync(target, targetFile);

// Now that we inlined installHook.js we can delete it
fs.unlinkSync(source);

// Rename injected css file so users understand where it's coming from.
fs.renameSync(
	path.join(dist, "installHook.css"),
	path.join(dist, "preact-devtools-page.css"),
);

// Package extension
const output = fs.createWriteStream(__dirname + `/../dist/${browser}.zip`);
const archive = archiver("zip", { zlib: { level: 9 } });

archive.on("warning", err => {
	if (err.code === "ENOENT") {
		console.log(err);
	} else {
		throw err;
	}
});

archive.on("error", err => {
	throw err;
});

archive.pipe(output);
archive.directory(`dist/${browser}`, false);
archive.finalize();
