Skip to content

Commit

Permalink
little bit faster, fix broken binary build
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Feb 16, 2021
1 parent 87456ea commit dbaa6ae
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 88 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ win32/**/obj
*/**.DS_Store

*/**/mono*.json
nccbin
/pkgbin
68 changes: 34 additions & 34 deletions bin/git-peek

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bin/git-peek.map

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ const codeSplitFiles = [
"src/registerProtocol",
];

const PKG_DEFINES = {
SEARCH_PATH: "'src/Search'",
REGISTER_PROTOCOL_PATH: "'src/registerProtocol'",
CONFIRM_PROMPT_PATH: "'src/confirmPrompt'",
};
const REGULAR_DEFINES = {
SEARCH_PATH: "_SEARCH_PATH",
REGISTER_PROTOCOL_PATH: "_REGISTER_PROTOCOL_PATH",
CONFIRM_PROMPT_PATH: "_CONFIRM_PROMPT_PATH",
};

const minify = true;

async function run() {
Expand All @@ -39,8 +50,21 @@ async function run() {
platform: "node",
sourcemap: "external",
external: [...globalExternals, ...codeSplitFiles],
define: REGULAR_DEFINES,
minify,
}).then((a) => console.log("Built.", a.outputFiles));
}

build({
bundle: true,
target: ["node12"],
entryPoints: ["./src/index.ts"],
outfile: "./pkgbin/git-peek",
platform: "node",
sourcemap: "external",
external: [...globalExternals],
define: PKG_DEFINES,
minify,
}).then((a) => console.log("Built.", a.outputFiles));

run();
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jarred/git-peek",
"version": "1.3.8",
"version": "1.3.7",
"author": "Jarred Sumner @Jarred-Sumner",
"description": "Quickly preview remote Git repositories in your local editor",
"bin": {
Expand All @@ -16,11 +16,9 @@
"@types/tar": "^4.0.4",
"@types/tmp": "^0.2.0",
"@vercel/fetch": "^6.1.0",
"abortcontroller-polyfill": "^1.7.1",
"chalk": "^4.1.0",
"dotenv": "^8.2.0",
"dotenv-cli": "^4.0.0",
"pnpm": "^5.17.2",
"esbuild": "^0.8.43",
"git-url-parse": "^11.4.4",
"ink": "^3.0.8",
Expand All @@ -33,6 +31,7 @@
"normalize-git-url": "^3.0.2",
"open-editor": "^3.0.0",
"pkg": "^4.4.9",
"pnpm": "^5.17.2",
"promise-any-polyfill": "^0.1.2",
"qs": "^6.9.6",
"react": "^17.0.1",
Expand Down Expand Up @@ -86,8 +85,8 @@
"release": "dotenv release-it",
"bt": "pnpm build && git-peek Jarred-Sumner/git-peek",
"copy-win32": "cp win32/git-peek-windows-launcher/git-peek-windows-launcher/git-peek-windows-launcher/bin/net5.0/win-x64/publish/git-peek-win32.exe bin/git-peek-win32.exe",
"pkg-mac": "pkg --public --options='no-warnings' -t 'node14-macos-x64' ./bin/git-peek -o ./brew/git-peek-macOS",
"pkg-windows": "pkg --public --options='no-warnings' -t 'node14-windows-x64' ./bin/git-peek -o ./brew/git-peek.exe",
"pkg-mac": "pkg --public --options='no-warnings' -t 'node14-macos-x64' ./pkgbin/git-peek -o ./brew/git-peek-macOS",
"pkg-windows": "pkg --public --options='no-warnings' -t 'node14-windows-x64' ./pkgbin/git-peek -o ./brew/git-peek.exe",
"pkg-all": "pnpm pkg-mac; pnpm pkg-windows",
"prerelease": "pnpm pkg-all",
"build-cs": "dotnet publish -c Release --self-contained false"
Expand Down
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 68 additions & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ import type { Writable } from "stream";
import zlib from "zlib";
import rimraf from "rimraf";

const _SEARCH_PATH = "src/Search";
const _REGISTER_PROTOCOL_PATH = "src/registerProtocol";
const _CONFIRM_PROMPT_PATH = "src/confirmPrompt";

function resolveAfterDelay(delay) {
return new Promise((resolve, reject) => setTimeout(resolve, delay));
}
// global.fetch = require("node-fetch");

// if (typeof global.AbortController === "undefined") {
// require("abortcontroller-polyfill/dist/polyfill-patch-fetch");
// }

// This is to trick esbuild into code splitting these files
const SEARCH_PATH = path.join(__dirname, "Search.js");
const REGISTER_PROTOCOL_PATH = path.join(__dirname, "registerProtocol.js");
const CONFIRM_PROMPT_PATH = path.join(__dirname, "confirmPrompt.js");

// const AbortController = global.AbortController;

Expand Down Expand Up @@ -137,6 +141,8 @@ async function resolveRefFromPullRequest(url: string) {

async function resolveRefFromURL(owner: string, repo: string) {
const apiURL = `https://${GITHUB_API_DOMAIN}/repos/${owner}/${repo}`;
if (process.env.VERBOSE)
console.log("Couldn't auto-detect ref, asking github what the ref is");

const result = await githubFetch(apiURL);
if (!result.ok) {
Expand Down Expand Up @@ -349,38 +355,42 @@ If this is a private repo, consider setting $GITHUB_TOKEN. To save $GITHUB_TOKEN
didUseFallback = false;
_tar: Writable;
unzipPromise: Promise<any>;
archiveStartPromise: Promise<any>;
unzip(owner, name, ref, fallback, to: string) {
return new Promise((resolve2, reject2) => {
this.unzipPromise = new Promise(async (resolve, reject) => {
const archive = await this.getArchive(
`https://${GITHUB_API_DOMAIN}/repos/${owner}/${name}/tarball/${ref}`,
`https://${GITHUB_API_DOMAIN}/repos/${owner}/${name}/tarball/${fallback}`
);

this.log("⏳ Extracting repository to temp folder...");
archive.pipe(
(this._tar = tar.x({
cwd: to,
strip: 1,
"keep-newer-files": true,
noMtime: true,
// onentry(entry) {},
// onwarn(message, data) {},
}))
);

archive.on("end", () => {
if (exiting) return;
this.log("💿 Finished downloading repository!");
resolve();
resolve2();
});
archive.on("error", (error) => {
if (didRemove || exiting) return;
this.archiveStartPromise = new Promise((resolve3, reject3) => {
this.unzipPromise = new Promise(async (resolve, reject) => {
const archive = await this.getArchive(
`https://${GITHUB_API_DOMAIN}/repos/${owner}/${name}/tarball/${ref}`,
`https://${GITHUB_API_DOMAIN}/repos/${owner}/${name}/tarball/${fallback}`
);
resolve3();

this.log("⏳ Extracting repository to temp folder...");
archive.pipe(
(this._tar = tar.x({
cwd: to,
strip: 1,
"keep-newer-files": true,
noMtime: true,
// onentry(entry) {},
// onwarn(message, data) {},
}))
);

archive.on("end", () => {
if (exiting) return;
this.log("💿 Finished downloading repository!");
resolve();
resolve2();
});
archive.on("error", (error) => {
if (didRemove || exiting) return;

this.log("💿 Failed to download repository!");
reject(error);
reject2(error);
this.log("💿 Failed to download repository!");
reject(error);
reject2(error);
});
});
});
});
Expand Down Expand Up @@ -604,7 +614,7 @@ to the appropriate URLs.
}

if (version) {
cli.showVersion();
console.log(require("package.json").version);
process.exit(0);
}

Expand Down Expand Up @@ -668,7 +678,7 @@ to the appropriate URLs.
link.resource === GITHUB_BASE_DOMAIN &&
(branch === "default" ||
defaultBranch ||
(branch === "" && cli.flags.fromscript))
(link.ref === "" && cli.flags.fromscript))
) {
ref = await resolveRefFromURL(link.owner, link.name);
} else if (branch !== "") {
Expand Down Expand Up @@ -709,6 +719,7 @@ to the appropriate URLs.
}
);
this.destination = tmpobj.name;
let chosenEditorPromise = fetchEditor(_editor, false);

didRemove = false;
process.once("beforeExit", doExit);
Expand All @@ -728,28 +739,43 @@ to the appropriate URLs.
if (link.resource === GITHUB_BASE_DOMAIN) {
let fallback = ref === "main" ? "master" : "main";

await Promise.any([
this.prefetchGithub(
let prefetchPromise;
if (ALLOW_JSDELIVR) {
prefetchPromise = this.prefetchGithub(
link.name,
link.owner,
specificFile,
ref,
fallback,
openPath
),
this.unzip(link.owner, link.name, ref, fallback, tmpobj.name),
]);
);
}
let unzipPromise = this.unzip(
link.owner,
link.name,
ref,
fallback,
tmpobj.name
);
let archiveStartPromise = this.archiveStartPromise.then(() =>
resolveAfterDelay(100)
);

if (prefetchPromise) {
await Promise.any([prefetchPromise, unzipPromise, archiveStartPromise]);
} else {
await Promise.any([unzipPromise, archiveStartPromise]);
}
} else {
await this.clone(link.href, tmpobj.name);
}

let chosenEditor = await fetchEditor(_editor, false);

let editorSpecificCommands = [];

// console.log(path.join(tmpobj.name, specificFile));

this.editorMode = EditorMode.unknown;
let chosenEditor = await chosenEditorPromise;

// VSCode is the happy case.
// When passed a folder, "--wait" correctly waits until the Window is closed.
Expand Down Expand Up @@ -962,5 +988,6 @@ if (DOTENV_EXISTS) {

const GITHUB_BASE_DOMAIN = process.env.GITHUB_BASE_DOMAIN || "github.com";
const GITHUB_API_DOMAIN = process.env.GITHUB_API_DOMAIN || "api.github.com";
const ALLOW_JSDELIVR = GITHUB_API_DOMAIN === "api.github.com";
instance = new Command();
instance.run();

0 comments on commit dbaa6ae

Please sign in to comment.