Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 1638a07

Browse files
committed
Support web editors
1 parent a7cee08 commit 1638a07

8 files changed

Lines changed: 89 additions & 72 deletions

File tree

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
"vscode": "^1.53.0"
1414
},
1515
"extensionKind": [
16-
"workspace",
17-
"web"
16+
"web",
17+
"workspace"
1818
],
1919
"capabilities": {
20-
"untrustedWorkspaces": {
21-
"supported": true
22-
},
2320
"virtualWorkspaces": true
2421
},
2522
"categories": [
@@ -30,7 +27,8 @@
3027
"onView:settings",
3128
"workspaceContains:**/.github/workflows/**"
3229
],
33-
"main": "./dist/extension",
30+
"main": "./dist/extension-node.js",
31+
"browser": "./dist/extension-web.js",
3432
"contributes": {
3533
"configuration": {
3634
"title": "GitHub Actions",
@@ -364,8 +362,7 @@
364362
},
365363
"dependencies": {
366364
"@octokit/rest": "18.6.7",
367-
"atob": "^2.1.2",
368-
"btoa": "^1.2.1",
365+
"abab": "^2.0.5",
369366
"github-actions-parser": "0.22.0",
370367
"js-yaml": "^3.14.0",
371368
"ssh-config": "^3.0.0",

src/external/ssh.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { parse as parseConfig } from "ssh-config";
2-
import { readFileSync } from "fs";
3-
import { join } from "path";
4-
import { homedir } from "os";
52

63
const SSH_URL_RE = /^(?:([^@:]+)@)?([^:/]+):?(.+)$/;
74
const URL_SCHEME_RE = /^([a-z-]+):\/\//;
@@ -55,7 +52,7 @@ export const resolve = (url: string, resolveConfig = Resolvers.current) => {
5552
};
5653

5754
export class Resolvers {
58-
static default = chainResolvers(baseResolver, resolverFromConfigFile());
55+
static default = chainResolvers(baseResolver /*, resolverFromConfigFile()*/);
5956

6057
static fromConfig(conf: string) {
6158
return chainResolvers(baseResolver, resolverFromConfig(conf));
@@ -89,16 +86,17 @@ function baseResolver(config: Config) {
8986
};
9087
}
9188

92-
function resolverFromConfigFile(
93-
configPath = join(homedir(), ".ssh", "config")
94-
): ConfigResolver | undefined {
95-
try {
96-
const config = readFileSync(configPath).toString();
97-
return resolverFromConfig(config);
98-
} catch (error) {
99-
// Logger.appendLine(`${configPath}: ${error.message}`);
100-
}
101-
}
89+
// Temporarily disable this to remove `fs` dependency
90+
// function resolverFromConfigFile(
91+
// configPath = join(homedir(), ".ssh", "config")
92+
// ): ConfigResolver | undefined {
93+
// try {
94+
// const config = readFileSync(configPath).toString();
95+
// return resolverFromConfig(config);
96+
// } catch (error) {
97+
// // Logger.appendLine(`${configPath}: ${error.message}`);
98+
// }
99+
// }
102100

103101
export function resolverFromConfig(text: string): ConfigResolver {
104102
const config = parseConfig(text);

src/git/repository.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import * as vscode from "vscode";
2-
3-
import { API, GitExtension, RefType } from "../typings/git";
4-
51
import { Octokit } from "@octokit/rest";
6-
import { Protocol } from "../external/protocol";
2+
import * as vscode from "vscode";
73
import { getClient } from "../api/api";
84
import { getSession } from "../auth/auth";
5+
import { Protocol } from "../external/protocol";
6+
import { API, GitExtension, RefType } from "../typings/git";
97

108
async function getGitExtension(): Promise<API | undefined> {
119
const gitExtension =
@@ -45,7 +43,6 @@ export async function getGitHead(): Promise<string | undefined> {
4543
}
4644
}
4745
}
48-
4946
export async function getGitHubUrls(): Promise<
5047
| {
5148
workspaceUri: vscode.Uri;
@@ -55,7 +52,6 @@ export async function getGitHubUrls(): Promise<
5552
| null
5653
> {
5754
const git = await getGitExtension();
58-
5955
if (git && git.repositories.length > 0) {
6056
return git.repositories
6157
.map((r) => {
@@ -82,6 +78,32 @@ export async function getGitHubUrls(): Promise<
8278
.filter((x) => !!x) as any;
8379
}
8480

81+
// If we cannot find the git extension, assume for now that we are running a web context,
82+
// for instance, github.dev. I think ideally we'd check the workspace URIs first, but this
83+
// works for now. We'll revisit later.
84+
if (!git) {
85+
// Support for virtual workspaces
86+
const isVirtualWorkspace =
87+
vscode.workspace.workspaceFolders &&
88+
vscode.workspace.workspaceFolders.every((f) => f.uri.scheme !== "file");
89+
if (isVirtualWorkspace) {
90+
const ghFolder = vscode.workspace.workspaceFolders?.find(
91+
(x) => x.uri.scheme === "vscode-vfs" && x.uri.authority === "github"
92+
);
93+
if (ghFolder) {
94+
const url = `https://github.com/${ghFolder.uri.path}`;
95+
96+
return [
97+
{
98+
workspaceUri: ghFolder.uri,
99+
url: url,
100+
protocol: new Protocol(url),
101+
},
102+
];
103+
}
104+
}
105+
}
106+
85107
return null;
86108
}
87109

src/tracker/workflowDocumentTracker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { extname } from "path";
21
import * as vscode from "vscode";
2+
3+
import { extname } from "path";
34
import { getContextStringForWorkflow } from "../workflow/workflow";
45

56
export function initWorkflowDocumentTracking(context: vscode.ExtensionContext) {
@@ -11,7 +12,7 @@ export function initWorkflowDocumentTracking(context: vscode.ExtensionContext) {
1112
onDidChangeActiveTextEditor(vscode.window.activeTextEditor);
1213
}
1314

14-
function onDidChangeActiveTextEditor(editor?: vscode.TextEditor) {
15+
async function onDidChangeActiveTextEditor(editor?: vscode.TextEditor) {
1516
if (!editor || !isTextEditor(editor)) {
1617
return;
1718
}
@@ -30,7 +31,7 @@ function onDidChangeActiveTextEditor(editor?: vscode.TextEditor) {
3031
vscode.commands.executeCommand(
3132
"setContext",
3233
"githubActions:activeFile",
33-
getContextStringForWorkflow(editor.document.fileName)
34+
await getContextStringForWorkflow(editor.document.fileName)
3435
);
3536
}
3637

src/workflow/workflow.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { GitHubRepoContext } from "../git/repository";
44
import { Workflow } from "github-actions-parser/dist/lib/workflow";
55
import { basename } from "path";
66
import { parse } from "github-actions-parser";
7-
import { readFileSync } from "fs";
87
import { safeLoad } from "js-yaml";
98

109
interface On {
@@ -51,9 +50,13 @@ function getEvents(doc: any): On[] {
5150
return on;
5251
}
5352

54-
export function getContextStringForWorkflow(path: string): string {
53+
export async function getContextStringForWorkflow(
54+
path: string
55+
): Promise<string> {
5556
try {
56-
const doc = safeLoad(readFileSync(path, "utf8"));
57+
const content = await vscode.workspace.fs.readFile(vscode.Uri.file(path));
58+
const file = Buffer.from(content).toString("utf8");
59+
const doc = safeLoad(file);
5760
if (doc) {
5861
let context = "";
5962

@@ -75,22 +78,6 @@ export function getContextStringForWorkflow(path: string): string {
7578
return "";
7679
}
7780

78-
export function getRepositoryDispatchTypes(path: string): string[] {
79-
try {
80-
const doc = safeLoad(readFileSync(path, "utf8"));
81-
if (doc) {
82-
const rdispatch = getEvents(doc).find(
83-
(t) => t.event.toLowerCase() == "repository_dispatch"
84-
);
85-
return rdispatch?.types || [];
86-
}
87-
} catch (e) {
88-
// Ignore
89-
}
90-
91-
return [];
92-
}
93-
9481
/**
9582
* Try to get Uri to workflow in currently open workspace folders
9683
*

tsconfig.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"module": "commonjs",
44
"target": "es6",
55
"outDir": "dist",
6-
"lib": [
7-
"es6"
8-
],
6+
"lib": ["ES2019", "DOM"],
97
"sourceMap": true,
108
"rootDir": "src",
119
"typeRoots": [

webpack.config.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@ const path = require("path");
66

77
/**@type {import('webpack').Configuration}*/
88
const config = {
9-
target: "node", // vscode extensions run in a Node.js-context 📖 -> https://webpack.js.org/configuration/node/
10-
119
entry: "./src/extension.ts", // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
12-
output: {
13-
// the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/
14-
path: path.resolve(__dirname, "dist"),
15-
filename: "extension.js",
16-
libraryTarget: "commonjs2",
17-
devtoolModuleFilenameTemplate: "../[resource-path]"
18-
},
1910
devtool: "source-map",
2011
externals: {
21-
vscode: "commonjs vscode", // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
12+
vscode: "commonjs vscode",
2213
},
2314
resolve: {
24-
// support reading TypeScript and JavaScript files, 📖 -> https://github.com/TypeStrong/ts-loader
2515
extensions: [".ts", ".js"],
2616
alias: {
2717
"universal-user-agent$": "universal-user-agent/dist-node/index.js"
@@ -45,4 +35,27 @@ const config = {
4535
]
4636
}
4737
};
48-
module.exports = config;
38+
39+
const nodeConfig = {
40+
...config,
41+
target: "node",
42+
output: {
43+
path: path.resolve(__dirname, "dist"),
44+
filename: "extension-node.js",
45+
libraryTarget: "commonjs2",
46+
devtoolModuleFilenameTemplate: "../[resource-path]",
47+
},
48+
};
49+
50+
const webConfig = {
51+
...config,
52+
target: "webworker",
53+
output: {
54+
path: path.resolve(__dirname, "dist"),
55+
filename: "extension-web.js",
56+
libraryTarget: "commonjs2",
57+
devtoolModuleFilenameTemplate: "../[resource-path]",
58+
},
59+
};
60+
61+
module.exports = [nodeConfig, webConfig];

0 commit comments

Comments
 (0)