Skip to content

Commit

Permalink
add script release-file
Browse files Browse the repository at this point in the history
  • Loading branch information
Xicheng Guo committed Aug 2, 2024
1 parent 1dd9a8d commit f5ce682
Show file tree
Hide file tree
Showing 26 changed files with 109 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Build with VitePress
run: npm run docs:build
- name: Release
run: npm run release
run: npm run release-file
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ node_modules/
docs/.vitepress/cache
docs/.vitepress/dist
!tsconfig.json
coverage
coverage
scripts/__tests__/examples/dist
!scripts/__tests__/examples/dist/.gitkeep
Binary file removed apk/OneClick-v1.0.apk
Binary file not shown.
Binary file removed apk/OneClick-v1.1.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.2.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.3.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.4.1.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.4.2.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.4.3.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.4.4.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.4.5.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.4.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.5.apk
Binary file not shown.
Binary file removed apk/SKIP-v2.0.0.apk
Binary file not shown.
Binary file removed apk/SKIP-v2.1.0.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
"release": "node release.js",
"release-file": "ts-node scripts/index.ts --func=release-file --apkPath=apk --releasePath=docs/.vitepress/dist",
"yaml-check": "ts-node ./scripts/yaml-check.ts",
"test": "jest --coverage"
},
Expand Down
15 changes: 0 additions & 15 deletions release.js

This file was deleted.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
50 changes: 50 additions & 0 deletions scripts/__tests__/release-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { releaseFile } from "../release-file";
import fs from "node:fs";

describe("Release File", () => {
it("目录不存在", () => {
expect(() =>
releaseFile({ apkPath: "scripts/__tests__/examples/not-exist", releasePath: "scripts/__tests__/examples/dist" })
).toThrow("apk path and release path should exist");
expect(() =>
releaseFile({
apkPath: "scripts/__tests__/examples/apk-multiple",
releasePath: "scripts/__tests__/examples/not-exist",
})
).toThrow("apk path and release path should exist");
});
it("apk 目录存在多个文件", () => {
expect(() =>
releaseFile({
apkPath: "scripts/__tests__/examples/apk-multiple",
releasePath: "scripts/__tests__/examples/dist",
})
).toThrow("apk path should contain only one file");
});
it("apk 的文件不是以 .apk 结尾", () => {
expect(() =>
releaseFile({ apkPath: "scripts/__tests__/examples/apk-not-apk", releasePath: "scripts/__tests__/examples/dist" })
).toThrow("apk file should end with .apk");
});
it("apk 的文件名不包含版本号", () => {
expect(() =>
releaseFile({
apkPath: "scripts/__tests__/examples/apk-no-version",
releasePath: "scripts/__tests__/examples/dist",
})
).toThrow("apk file should contain version number");
});
it("apk 的文件名包含版本号", () => {
expect(() =>
releaseFile({
apkPath: "scripts/__tests__/examples/apk-has-version",
releasePath: "scripts/__tests__/examples/dist",
})
).not.toThrow();
const apkVersion = fs.readFileSync("scripts/__tests__/examples/dist/latest_version.txt", "utf-8");
expect(apkVersion).toBe("2.1.1");
expect(() => fs.accessSync("scripts/__tests__/examples/dist/SKIP-v2.1.1.apk")).not.toThrow();
expect(() => fs.accessSync("scripts/__tests__/examples/dist/skip_config.yaml")).not.toThrow();
expect(() => fs.accessSync("scripts/__tests__/examples/dist/skip_config_v2.yaml")).not.toThrow();
});
});
2 changes: 1 addition & 1 deletion scripts/__tests__/yaml-check.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { yamlCheck } from "../yaml-check";

describe("YAML Check 2", () => {
describe("YAML Check", () => {
it("正确格式的 YAML", () => {
const detail = yamlCheck("scripts/__tests__/examples/correct-yaml.yaml");
expect(detail).toEqual([
Expand Down
13 changes: 13 additions & 0 deletions scripts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(() => {
const args = process.argv.slice(2);
const params: Record<string, string> = {};
args.forEach((arg) => {
const [key, value] = arg.split("=");
params[key.substring(2)] = value;
});

if (params.func === "release-file") {
const { releaseFile } = require("./release-file");
releaseFile(params);
}
})();
40 changes: 40 additions & 0 deletions scripts/release-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fs from "node:fs";

interface ReleaseFileOptions {
apkPath: string;
releasePath: string;
}

export function releaseFile(options: ReleaseFileOptions) {
const { apkPath, releasePath } = options;
if (!fs.existsSync(apkPath) || !fs.existsSync(releasePath)) {
throw new Error("apk path and release path should exist");
}

const apkFileList = fs.readdirSync(apkPath);

if (apkFileList.length !== 1) {
throw new Error("apk path should contain only one file");
}

const apkFile = apkFileList[0];
if (!apkFile.endsWith(".apk")) {
throw new Error("apk file should end with .apk");
}

const regex = /SKIP-v(\d+\.\d+\.\d+)\.apk/;
const match = apkFile.match(regex);
if (!match) {
throw new Error("apk file should contain version number");
}

const apkVersion = match[1];

// 写入 apkVersion 到 releasePath
fs.writeFileSync(`${releasePath}/latest_version.txt`, apkVersion);
// 复制 apkFile 到 releasePath
fs.copyFileSync(`${apkPath}/${apkFile}`, `${releasePath}/${apkFile}`);
// 复制 config
fs.copyFileSync("app/src/main/assets/skip_config.yaml", `${releasePath}/skip_config.yaml`);
fs.copyFileSync("app/src/main/assets/skip_config_v2.yaml", `${releasePath}/skip_config_v2.yaml`);
}

0 comments on commit f5ce682

Please sign in to comment.