Skip to content

Commit

Permalink
Merge pull request #386 from ikemo3/package/replace-scripts
Browse files Browse the repository at this point in the history
package: replace scripts
  • Loading branch information
ikemo3 authored Aug 13, 2022
2 parents 8a3a02e + 3c8323c commit 06f5924
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 223 deletions.
35 changes: 17 additions & 18 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: '2.1'
version: "2.1"
orbs:
browser-tools: circleci/[email protected]
browser-tools: circleci/[email protected]
jobs:
build:
docker:
- image: cimg/node:18.4.0-browsers
- image: cimg/node:16.16-browsers
environment:
- LANG: ja_JP.UTF-8
- LANGUAGE: ja_JP.UTF-8
Expand All @@ -17,29 +17,19 @@ jobs:

steps:
- checkout
- browser-tools/install-chrome
- browser-tools/install-chromedriver
- run:
name: "Install Japanese font"
command: |
sudo apt update
sudo apt install -y fonts-takao-gothic
- run:
name: "Download Firefox Developer Edition"
command: |
curl -L 'https://download.mozilla.org/?product=firefox-devedition-latest-ssl&os=linux64&lang=ja' > firefox.tar.bz2
tar xvfj firefox.tar.bz2
- run:
command: make ci
- store_test_results:
path: reports
- store_artifacts:
path: reports
- persist_to_workspace:
root: tmp/workspace
paths:
- gossip-site-blocker.*
- gossip-site-blocker-*
deploy:
docker:
- image: cimg/go:1.17.1
- image: cimg/node:16.16

working_directory: ~/repo

Expand All @@ -49,7 +39,16 @@ jobs:
at: tmp/workspace
- run:
name: "Publish Release on GitHub"
command: make deploy
command: |
# install ghr
sudo apt update
sudo apt install -y golang
go get -u -v github.com/tcnksm/ghr
# deploy
yarn install
export PATH=$PATH:~/go/bin
make deploy
workflows:
version: 2
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ ci:
./scripts/ci.sh

deploy:
./scripts/deploy.sh
yarn deploy
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@typescript-eslint/parser": "^5.33.0",
"copy-webpack-plugin": "^11.0.0",
"crx": "^5.0.1",
"date-fns": "2.29.1",
"dayjs": "^1.11.4",
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
Expand All @@ -31,11 +32,13 @@
"typescript": "^4.7.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-shell-plugin-next": "^2.2.2"
"webpack-shell-plugin-next": "^2.2.2",
"zip-a-folder": "1.1.5"
},
"scripts": {
"archive": "ts-node ./scripts/archive.ts",
"build": "webpack",
"deploy": "ts-node scripts/github_release.ts",
"fix": "yarn eslint --fix --ext .ts apps scripts test *.ts jest.config.js",
"lint": "prettier --write .; yarn eslint --ext .ts apps scripts test *.ts jest.config.js",
"lint-for-ci": "prettier --check .; yarn eslint --format junit --output-file ./reports/eslint/eslint.xml --ext .ts apps scripts test *.ts jest.config.js",
Expand Down
128 changes: 72 additions & 56 deletions scripts/archive.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,74 @@
import { writeFileSync } from "fs";
import { join } from "path";
import { execFileSync, execSync } from "child_process";
import { createManifest, getManifest, getPackageName, getWebExtensionId, mkdirIfNeeded, writeManifest } from "./libs";
import { distDir, projectTop } from "./const";

const packageName = getPackageName();
const pemBase64 = process.env.PEM_BASE64;

// create manifest.json
createManifest();

// create Chrome Extension
mkdirIfNeeded(join(projectTop, "tmp"));
mkdirIfNeeded(join(projectTop, "tmp", "workspace"));

if (pemBase64 !== undefined) {
// decode private key
const buffer = Buffer.from(pemBase64, "base64");
const pem = buffer.toString();
const pemPath = join(projectTop, "tmp", `${packageName}.pem`);
writeFileSync(pemPath, pem);

// create Chrome Extension(signed)
console.info(`Create ${packageName}.crx with private key`);
execFileSync("yarn", [
"crx",
"pack",
"-o",
`tmp/workspace/${packageName}.crx`,
"dist",
"-p",
`tmp/${packageName}.pem`,
]);
} else {
// create Chrome Extension(signed, create private key)
console.info(`Create ${packageName}.crx`);
execFileSync("yarn", ["crx", "pack", "-o", `tmp/workspace/${packageName}.crx`, "dist"]);
import { copyFileSync, cpSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
import path from "path";
import { execSync } from "child_process";
import { zip } from "zip-a-folder";

function clearDirectory(path: string) {
if (existsSync(path)) {
rmSync(path, { recursive: true });
}
}

function getPackageName(projectDir: string) {
const packageJsonPath = projectDir + "/package.json";
const packageJson = JSON.parse(readFileSync(packageJsonPath).toString());
return packageJson.name;
}

function getExtensionId(projectDir: string) {
const extensionIdPath = projectDir + "/apps/.web-extension-id";
const webExtensionId = readFileSync(extensionIdPath).toString();
return webExtensionId.split("\n")[2];
}

async function createChromeExtension(packageName: string, projectDir: string) {
console.log(`Create ${packageName}.zip`);
await zip(projectDir + "/dist-chrome", projectDir + `/tmp/workspace/${packageName}-chrome.zip`);
}

async function createFirefoxExtension(packageName: string, projectDir: string) {
// load manifest.json
const extensionId = getExtensionId(projectDir);
console.log(`Extension ID: ${extensionId}`);

const manifestJsonPath = projectDir + "/apps/manifest.json";
const manifestJson = JSON.parse(readFileSync(manifestJsonPath).toString());
manifestJson.browser_specific_settings = { gecko: { id: extensionId } };

// write manifest.json
const manifestJsonWritePath = projectDir + "/dist-firefox/manifest.json";
writeFileSync(manifestJsonWritePath, JSON.stringify(manifestJson, null, 2));

// create Firefox extension(xpi)
await zip(projectDir + "/dist-firefox", projectDir + `/tmp/workspace/${packageName}-firefox.xpi`);
}

async function main() {
// create if there is no dist directory
const projectDir = path.dirname(__dirname);
clearDirectory(projectDir + "/dist-chrome");
clearDirectory(projectDir + "/dist-firefox");
clearDirectory(projectDir + "/tmp");
mkdirSync(projectDir + "/tmp");
mkdirSync(projectDir + "/tmp/workspace");

// copy resources
execSync("webpack", { stdio: "inherit" });
cpSync(projectDir + "/dist", projectDir + "/dist-chrome", { recursive: true });
cpSync(projectDir + "/dist", projectDir + "/dist-firefox", { recursive: true });
copyFileSync(projectDir + "/apps/.web-extension-id", projectDir + "/dist-firefox/.web-extension-id");

// load package.json
const packageName = getPackageName(projectDir);

// create Chrome extension
await createChromeExtension(packageName, projectDir);

// create Firefox extension
await createFirefoxExtension(packageName, projectDir);
}

// create Chrome Extension(unsigned)
console.info(`Create ${packageName}.zip`);
execFileSync("yarn", ["crx", "pack", "--zip-output", `tmp/workspace/${packageName}.zip`, "dist"]);

// create Firefox Extension
const manifest = getManifest();
const extensionId = getWebExtensionId();
manifest.browser_specific_settings = {
gecko: {
id: extensionId,
},
};
console.info(`extension id: ${extensionId}`);
writeManifest(manifest);

// create *.xpi
console.info(`Create ${packageName}.xpi`);
execSync(`zip -r ../tmp/workspace/${packageName}.xpi *`, { cwd: distDir });
main().catch((err) => {
console.error(err);
process.exit(1);
});
6 changes: 0 additions & 6 deletions scripts/const.ts

This file was deleted.

74 changes: 0 additions & 74 deletions scripts/deploy.sh

This file was deleted.

60 changes: 60 additions & 0 deletions scripts/github_release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { execSync, spawnSync } from "child_process";
import { readFileSync, renameSync } from "fs";
import path from "path";
import { isError, configureGhrOption } from "./libs";

function getGitSha() {
const sha = execSync("git rev-parse HEAD");
return sha.toString().trim();
}

function getManifestVersion(projectDir: string) {
const manifestJsonPath = projectDir + "/apps/manifest.json";
const manifestJson = JSON.parse(readFileSync(manifestJsonPath).toString());
return manifestJson.version;
}

function getManifestVersionName(projectDir: string) {
const manifestJsonPath = projectDir + "/apps/manifest.json";
const manifestJson = JSON.parse(readFileSync(manifestJsonPath).toString());
return manifestJson.version_name;
}

function getPackageVersion(projectDir: string) {
const packageJsonPath = projectDir + "/package.json";
const packageJson = JSON.parse(readFileSync(packageJsonPath).toString());
return packageJson.version;
}

function getPackageName(projectDir: string) {
const packageJsonPath = projectDir + "/package.json";
const packageJson = JSON.parse(readFileSync(packageJsonPath).toString());
return packageJson.name;
}

function main() {
const projectDir = path.dirname(__dirname);

const branch = process.env.CIRCLE_BRANCH;
const tag = process.env.CIRCLE_TAG;
const packageVersion = getPackageVersion(projectDir);
const manifestVersion = getManifestVersion(projectDir);
const manifestVersionName = getManifestVersionName(projectDir);

const config = configureGhrOption(branch, tag, manifestVersion, manifestVersionName, packageVersion);
if (isError(config)) {
console.log(config.message);
process.exit(config.exitCode);
}

const packageName = getPackageName(projectDir);

renameSync(`tmp/workspace/${packageName}-chrome.zip`, `tmp/workspace/${packageName}-${config.name}-chrome.zip`);
renameSync(`tmp/workspace/${packageName}-firefox.xpi`, `tmp/workspace/${packageName}-${config.name}-firefox.xpi`);

const args = ["-c", getGitSha(), "-n", config.name, ...config.ghrOptions, config.tag, "tmp/workspace/"];
console.log("ghr", args);
spawnSync("ghr", args);
}

main();
Loading

0 comments on commit 06f5924

Please sign in to comment.