Skip to content

Commit

Permalink
stage: tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
northword committed Jan 25, 2024
1 parent 03ca4f7 commit fb6197d
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 44 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"packageManager": "[email protected]",
"dependencies": {
"@inquirer/prompts": "^3.3.0",
"@octokit/rest": "^20.0.2",
"bumpp": "^9.3.0",
"chalk": "^5.3.0",
"chokidar": "^3.5.3",
Expand All @@ -65,6 +66,7 @@
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
"lodash": "^4.17.21",
"octokit": "^3.1.2",
"ora": "^8.0.1",
"release-it": "^17.0.1",
"replace-in-file": "^7.1.0",
Expand Down
52 changes: 26 additions & 26 deletions src/lib/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ export default class Build extends LibBase {

const replaceResult = replaceInFileSync({
files: this.config.assets.map((asset) => `${this.config.dist}/${asset}`),
from: _.uniq(replaceFrom),
to: _.uniq(replaceTo),
from: replaceFrom,
to: replaceTo,
countMatches: true,
});

Expand Down Expand Up @@ -272,28 +272,28 @@ export default class Build extends LibBase {
// });
}

private get version() {
return this.config.define.buildVersion;
}
private get addonName() {
return this.config.define.addonName;
}
private get addonID() {
return this.config.define.addonID;
}
private get addonRef() {
return this.config.define.addonRef;
}
private get addonInstence() {
return this.config.define.addonInstance;
}
private get updateLink() {
return this.config.define.updateLink;
}
private get updateURL() {
return this.config.define.updateURL;
}
private get xpiName() {
return this.config.define.xpiName;
}
// private get version() {
// return this.config.define.buildVersion;
// }
// private get addonName() {
// return this.config.define.addonName;
// }
// private get addonID() {
// return this.config.define.addonID;
// }
// private get addonRef() {
// return this.config.define.addonRef;
// }
// private get addonInstence() {
// return this.config.define.addonInstance;
// }
// private get updateLink() {
// return this.config.define.updateLink;
// }
// private get updateURL() {
// return this.config.define.updateURL;
// }
// private get xpiName() {
// return this.config.define.xpiName;
// }
}
2 changes: 2 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export async function loadConfig(file?: string): Promise<Config> {
description: pkg.description || "",
homepage: pkg.homepage,
author: pkg.author,
ghOwner: owner,
ghRepo: repo,
addonRef: pkg.config?.addonRef || _.kebabCase(addonName),
addonInstance: pkg.config?.addonInstence || _.camelCase(addonName),
prefsPrefix: `extensions.zotero.${addonRef}`,
Expand Down
96 changes: 90 additions & 6 deletions src/lib/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import { LibBase } from "../utils/libBase.js";
import versionBump from "bumpp";
import ci from "ci-info";
import { default as glob } from "fast-glob";
import fs from "fs-extra";
import _ from "lodash";
import { Octokit } from "octokit";
import path from "path";
import releaseIt from "release-it";

export default class Release extends LibBase {
isCI: boolean;
client: Octokit["rest"];
constructor(config: Config) {
super(config);
this.isCI = ci.isCI;
this.client = this.getClient().rest;
}

/**
Expand All @@ -30,8 +35,8 @@ export default class Release extends LibBase {
);
}
this.uploadXPI();
this.createRelease();
this.uploadAssets();
// this.createRelease();
// this.uploadAssets();
}
}

Expand Down Expand Up @@ -69,15 +74,94 @@ export default class Release extends LibBase {
releaseIt(_.defaultsDeep(releaseItConfig, this.config.release.releaseIt));
}

createRelease() {
//
// @ts-ignore 01111
async getRelease(tag: string, isPreRelease: boolean) {
try {
return await this.client.repos.getReleaseByTag({
owner: this.owner,
repo: this.repo,
tag: tag,
});
} catch {
return await this.client.repos.createRelease({
owner: this.owner,
repo: this.repo,
tag_name: tag,
prerelease: isPreRelease,
});
}
}

uploadAssets() {
//
async uploadAsset(
release: any,
asset: string,
contentType: string,
isUpdate: boolean,
) {
this.logger.debug(
`uploading ${path.basename(asset)} to ${release.data.tag_name}`,
);
const name = path.basename(asset);
// const contentType = mime.contentType(name) || 'application/octet-stream';
const contentLength = fs.statSync(asset).size;

const exists = (
await this.client.repos.listReleaseAssets({
owner: this.owner,
repo: this.repo,
release_id: release.data.id,
})
).data.find((a) => a.name === name);
if (exists && isUpdate) {
await this.client.repos.deleteReleaseAsset({
owner: this.owner,
repo: this.repo,
asset_id: exists.id,
});
} else {
throw new Error(
`failed to upload ${path.basename(asset)} to ${release.data.html_url}: asset exists`,
);
}

try {
await this.client.repos.uploadReleaseAsset({
owner: this.owner,
repo: this.repo,
url: release.data.upload_url,
release_id: release.data.id,
data: fs.readFileSync(asset) as unknown as string,
headers: {
"content-type": contentType,
"content-length": contentLength,
},
name,
});
} catch (err) {
throw new Error(
`failed to upload ${path.basename(asset)} to ${release.data.html_url}: ${err}`,
);
}
}

uploadUpdateJSON() {
//
}

getClient(): Octokit {
if (!process.env.GITHUB_TOKEN) throw new Error("No GITHUB_TOKEN.");
const client = new Octokit({
auth: process.env.GITHUB_TOKEN,
userAgent: `zotero-plugin-scaffold/${this.version}`,
});

return client;
}

get owner(): string {
return this.config.define.ghOwner;
}
get repo(): string {
return this.config.define.ghRepo;
}
}
15 changes: 3 additions & 12 deletions src/lib/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default class Serve extends LibBase {
this.builder.run();

// start Zotero
this.startZotero();
// this.startZoteroWebExt();
// this.startZotero();
this.startZoteroWebExt();

// watch
await this.config.extraServer(this.config);
Expand Down Expand Up @@ -117,7 +117,7 @@ export default class Serve extends LibBase {
keepProfileChanges: true,
args: ["--debugger", "--purgecaches"],
// browserConsole: true,
// openDevTool: true, // need Zotero upgrade to firefox 115
// openDevTool: true, // need Zotero upgrade to firefox 115
},
{
// These are non CLI related options for each function.
Expand Down Expand Up @@ -275,13 +275,4 @@ export default class Serve extends LibBase {
private get dataDir() {
return process.env.dataDir ?? "";
}
private get addonID() {
return this.config.define.addonID;
}
private get addonName() {
return this.config.define.addonName;
}
private get version() {
return this.config.define.buildVersion;
}
}
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export interface ConfigBase {
author: string;
description: string;
homepage: string;
ghOwner: string;
ghRepo: string;

// code
/**
Expand Down
25 changes: 25 additions & 0 deletions src/utils/libBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,29 @@ export abstract class LibBase {
this.config = config;
this.logger = new Log(config);
}

get version() {
return this.config.define.buildVersion;
}
get addonName() {
return this.config.define.addonName;
}
get addonID() {
return this.config.define.addonID;
}
get addonRef() {
return this.config.define.addonRef;
}
get addonInstence() {
return this.config.define.addonInstance;
}
get updateLink() {
return this.config.define.updateLink;
}
get updateURL() {
return this.config.define.updateURL;
}
get xpiName() {
return this.config.define.xpiName;
}
}

0 comments on commit fb6197d

Please sign in to comment.