Skip to content

Commit

Permalink
Release 1.3.14
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Feb 17, 2021
1 parent 5a8a781 commit 63c7f1e
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 73 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ There are also [precompiled binaries](https://github.com/Jarred-Sumner/git-peek/
- [Windows x64 – 1.3.13](https://github.com/Jarred-Sumner/git-peek/releases/download/1.3.13/git-peek.exe)
- [macOS x64 – 1.3.13](https://github.com/Jarred-Sumner/git-peek/releases/download/1.3.13/git-peek-macOS)

**NEW**: [Try the chrome/firefox extension](https://github.com/Jarred-Sumner/1-click-from-github-to-editor). It adds a "Peek" button to pull requests, files, and repositories, so you can read code with your editor without needing to copy-paste into the terminal. Note: vim and Linux are both not supported yet
**NEW**: [Try the chrome/firefox extension](https://github.com/Jarred-Sumner/1-click-from-github-to-editor). It adds a "Peek" button to pull requests, files, and repositories, so you can read code with your editor without needing to copy-paste into the terminal.

## Usage:

Expand Down Expand Up @@ -118,7 +118,7 @@ If you paste a link to a file on GitHub, it will quickly open the file in your l

OPTIONS
-e, --editor=editor [default: auto] editor to open with, possible values:
auto, code, subl, vim, vi, code-insiders.
auto, code, subl, nvim, vim, vi, code-insiders.
By default, it will search $EDITOR. If not found, it
will try code, then subl, then vim.

Expand Down Expand Up @@ -180,6 +180,11 @@ When your editor closes or you close `git peek`, it deletes the repository from
### Changelog
##### 1.3.14
- Auto-detect `nvim`
- Add `/n?vim?/` support for Peek (browser extension) on macoS.
##### `1.3.8 - 1.3.13`
- Improve reliability of `git peek -r` on macOS
Expand Down
74 changes: 41 additions & 33 deletions bin/git-peek

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions bin/git-peek.map

Large diffs are not rendered by default.

35 changes: 19 additions & 16 deletions bin/registerProtocol.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions bin/registerProtocol.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jarred/git-peek",
"version": "1.3.13",
"version": "1.3.14",
"author": "Jarred Sumner @Jarred-Sumner",
"description": "Quickly preview remote Git repositories in your local editor",
"bin": {
Expand Down Expand Up @@ -43,7 +43,8 @@
"tar": "^6.1.0",
"tmp": "^0.2.1",
"typescript": "beta",
"which": "^2.0.2"
"which": "^2.0.2",
"which-term": "^1.0.2"
},
"engines": {
"node": ">=10.0.0"
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

118 changes: 110 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import rimraf from "rimraf";
const _SEARCH_PATH = path.join(__dirname, "Search");
const _REGISTER_PROTOCOL_PATH = path.join(__dirname, "registerProtocol");
const _CONFIRM_PROMPT_PATH = path.join(__dirname, "confirmPrompt");
import { Terminal } from "which-term";

function resolveAfterDelay(delay) {
return new Promise((resolve, reject) => setTimeout(resolve, delay));
Expand All @@ -41,7 +42,7 @@ const HOME =

const GIT_PEEK_ENV_PATH = path.join(HOME, ".git-peek");

let editorsToTry = ["code", "subl", "code-insiders", "vim", "vi"];
let editorsToTry = ["code", "subl", "nvim", "code-insiders", "vim", "vi"];

let shouldKeep = false;

Expand Down Expand Up @@ -436,7 +437,7 @@ OPTIONS
}] editor to open with, possible values:
auto, ${editorsToTry.join(", ")}.
By default, it will search $EDITOR. If not found, it
will try code, then subl, then vim.
will try code, then subl, then nvim then vim.
-d [default: false] Ask the GitHub API
for the default_branch to clone.
Expand Down Expand Up @@ -757,13 +758,13 @@ to the appropriate URLs.
fallback,
tmpobj.name
);
let archiveStartPromise = this.archiveStartPromise.then(() =>
resolveAfterDelay(100)
);

if (prefetchPromise) {
await Promise.any([prefetchPromise, unzipPromise, archiveStartPromise]);
await Promise.any([prefetchPromise, unzipPromise]);
} else {
let archiveStartPromise = this.archiveStartPromise.then(() =>
resolveAfterDelay(100)
);
await Promise.any([unzipPromise, archiveStartPromise]);
}
} else {
Expand Down Expand Up @@ -828,7 +829,9 @@ to the appropriate URLs.
}

if (
((this.editorMode === EditorMode.vim && usingDefaultFile) ||
((this.editorMode === EditorMode.vim &&
usingDefaultFile &&
!cli.flags.fromscript) ||
cli.flags.wait) &&
this.unzipPromise
) {
Expand All @@ -837,7 +840,7 @@ to the appropriate URLs.
}

await new Promise((resolve, reject) => {
if (this.editorMode === EditorMode.vim) {
if (this.editorMode === EditorMode.vim && !cli.flags.fromscript) {
process.stdin.setRawMode(true);
process.stdin.pause();

Expand Down Expand Up @@ -865,6 +868,94 @@ to the appropriate URLs.
}
}

this.slowTask.once("close", resolver);
this.slowTask.once("exit", resolver);
this.slowTask.once("error", resolver);
} else if (
this.editorMode === EditorMode.vim &&
process.platform === "darwin"
) {
let outerCommand: string;
let innerCommand =
chosenEditor +
" " +
// TODO: is this bad?
(usingDefaultFile ? specificFile : specificFile) +
" " +
editorSpecificCommands.join(" ");

let fileToWatch =
CHOSEN_TERMINAL === Terminal.alacritty
? null
: tmp.fileSync({ discardDescriptor: true, detachDescriptor: true });

switch (CHOSEN_TERMINAL) {
case Terminal.iTerm: {
outerCommand = `echo '#!/bin/bash\n${innerCommand}; echo $PPID $PID > ${fileToWatch.name}; sleep 1; exit;' > /tmp/git-peek.sh; chmod a+x /tmp/git-peek.sh; open -F -W -a iTerm /tmp/git-peek.sh`;
break;
}

case Terminal.alacritty: {
outerCommand = `echo '#!/bin/bash\ncd ${
tmpobj.name
};\n${innerCommand};' > /tmp/git-peek.sh; chmod a+x /tmp/git-peek.sh; ${
process.env.ALACRITTY_PATH ||
"/Applications/Alacritty.app/Contents/MacOS/alacritty"
} -e /tmp/git-peek.sh`;
break;
}

// Hyper doesn't support this!
// case Terminal.Hyper: {
// outerCommand = `open -a "${which.sync(
// "hyper"
// )}" . -W -n --args ${innerCommand}`;
// break;
// }

default: {
outerCommand = `echo '#!/bin/bash\n${innerCommand}; echo $PPID $PID > ${fileToWatch.name}; sleep 1; exit' > /tmp/git-peek.sh; chmod a+x /tmp/git-peek.sh; open -n -W -F -a Terminal /tmp/git-peek.sh`;
break;
}
}

this.slowTask = childProcess.spawn(outerCommand, {
env: process.env,
shell: true,
windowsHide: true,
stdio: "ignore",
// This line is important! If detached is true, nothing ever happens.
detached: false,
// Windows will refuse to delete if there is an active process in the folder
});

if (process.env.VERBOSE) console.log("RUN", outerCommand);

let watcher;
let pid = this.slowTask.pid;
let didResolve = false;
const resolver = () => {
if (!didResolve) {
if (watcher) {
fs.unwatchFile(fileToWatch.name);
watcher = null;
}

if (!this.slowTask.killed && this.slowTask.connected)
this.slowTask.kill("SIGTERM");

if (process?.stdin?.setRawMode) process.stdin.setRawMode(false);
if (process?.stdin?.resume) process.stdin.resume();

resolve();
didResolve = true;
}
};

if (fileToWatch) {
watcher = fs.watchFile(fileToWatch.name, {}, resolver);
}

this.slowTask.once("close", resolver);
this.slowTask.once("exit", resolver);
this.slowTask.once("error", resolver);
Expand Down Expand Up @@ -1005,6 +1096,16 @@ ENVIRONMENT VARIABLES:
: "api.github.com"
}
$OPEN_IN_TERMINAL: ${
process.env.OPEN_IN_TERMINAL?.length
? process.env.OPEN_IN_TERMINAL
: "not set"
}
$ALACRITTY_PATH: ${
process.env.ALACRITTY_PATH?.length ? process.env.ALACRITTY_PATH : "not set"
}
`;
}

Expand All @@ -1015,6 +1116,7 @@ if (DOTENV_EXISTS) {
dotenv.config({ path: GIT_PEEK_ENV_PATH });
}

const CHOSEN_TERMINAL: Terminal = require("./terminal").default;
const GITHUB_BASE_DOMAIN = process.env.GITHUB_BASE_DOMAIN || "github.com";
const GITHUB_API_DOMAIN = process.env.GITHUB_API_DOMAIN || "api.github.com";
let ALLOW_JSDELIVR = GITHUB_API_DOMAIN === "api.github.com";
Expand Down
30 changes: 24 additions & 6 deletions src/registerProtocol.mac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import tmp from "tmp";
const APP_DIR = path.join(process.env.HOME, "Applications", "git-peek.app");

import { PROTOCOL } from "./PROTOCOL";
import terminal from "src/terminal";

export function execSync(cmd) {
console.log("$ ", chalk.gray(cmd));
Expand All @@ -19,6 +20,7 @@ export async function register(editor: string) {
try {
await which("duti");
} catch (exception) {
``;
const installCommand = `HOMEBREW_NO_AUTO_UPDATE=1 HOMEBREW_NO_INSTALL_CLEANUP=1 brew install duti`;
console.log(`duti not installed. Installing with homebrew.`);
execSync(installCommand);
Expand Down Expand Up @@ -74,8 +76,11 @@ export PATH=$PATH:${JSON.stringify(process.env.PATH) || ""}
export EDITOR=${JSON.stringify(editor)}
export HOME=${JSON.stringify(process.env.HOME) || ""}
export USER=${JSON.stringify(process.env.USER) || ""}
export OPEN_IN_TERMINAL=${JSON.stringify(terminal)}
.${JSON.stringify(await which("git-peek"))} --fromscript $1 $2 $3 $4 & disown
OPEN_IN_TERMINAL=${JSON.stringify(terminal)} .${JSON.stringify(
await which("git-peek")
)} --fromscript $1 $2 $3 $4 & disown
`;

console.log(
Expand All @@ -87,6 +92,12 @@ export USER=${JSON.stringify(process.env.USER) || ""}
fs.writeFileSync(gitPeekShim, shim, "utf8");
execSync("chmod +x " + gitPeekShim);

if (fs.existsSync("/Applications/git-peek.app")) {
try {
fs.rmSync("/Applications/git-peek.app", { force: true });
} catch (exception) {}
}

console.log("Registering URL handler...");
execSync(`duti -s com.apple.ScriptEditor.id.git-peek ${PROTOCOL}`);

Expand All @@ -102,16 +113,23 @@ export USER=${JSON.stringify(process.env.USER) || ""}
console.warn("Failed to add protocol to Google Chrome. Its okay.");
}
}
console.log(`To unregister, just delete "${APP_DIR}".`);

console.log(chalk.green("✅ Registered git-peek:// protocol successfully."));
console.log(`To unregister, just delete "${APP_DIR}".`);
console.log("To test it, run this:");
console.log(" " + chalk.blue(`open git-peek://Jarred-Sumner/git-peek`));

if (editor.includes("vi")) {
console.warn(
"vim/vi not supported (no terminal window will be open), but if you know a way to run a terminal window from the AppleScript please do submit a PR!"
console.log(
`Defaulting to ${chalk.blue(editor)} in ${chalk.blue(
terminal
)}\n${chalk.gray("To change the editor, set")} EDITOR= ${chalk.gray(
"in"
)} ${process.env.HOME}/.git-peek.\n${chalk.gray(
"To change the terminal, set"
)} OPEN_IN_TERMINAL= iterm apple alacritty.`
);
}
console.log("To test it, run this:");
console.log(" " + chalk.blue(`open git-peek://Jarred-Sumner/git-peek`));
}

export async function generateAppleScript(shimLocation: string) {
Expand Down

0 comments on commit 63c7f1e

Please sign in to comment.