Add vscode provider

This commit is contained in:
Rodrigo Pombo 2019-02-18 14:53:46 -03:00
parent 8fe1e9cd08
commit e803429650
3 changed files with 70 additions and 21 deletions

View File

@ -1,12 +1,13 @@
import cliProvider from "./cli-provider";
import githubProvider from "./github-provider";
import vscodeProvider from "./vscode-provider";
export default function getGitProvider() {
switch (process.env.REACT_APP_GIT_PROVIDER) {
case "cli":
return cliProvider;
case "vscode":
return null;
return vscodeProvider;
default:
return githubProvider;
}

View File

@ -0,0 +1,38 @@
const vscode = window.vscode;
function getPath() {
return window._PATH;
}
function showLanding() {
return false;
}
function getCommits() {
const path = getPath();
return new Promise((resolve, reject) => {
window.addEventListener(
"message",
event => {
const commits = event.data;
commits.forEach(c => (c.date = new Date(c.date)));
resolve(commits);
},
{ once: true }
);
vscode.postMessage({
command: "commits",
params: {
path
}
});
});
}
export default {
showLanding,
getPath,
getCommits
};

View File

@ -43,28 +43,38 @@ function activate(context) {
);
const index = fs.readFileSync(indexPath, "utf-8");
const newIndex = index
.replace(
"<body>",
`<body><script>/*<!--*/window.vscode=acquireVsCodeApi();window._PATH='${currentPath}'/*-->*/</script>`
)
.replace(
"<head>",
`<head><base href="${vscode.Uri.file(
path.join(context.extensionPath, "site")
).with({
scheme: "vscode-resource"
})}/"/>`
);
getCommits(currentPath)
.then(commits => {
const newIndex = index
.replace(
"<script>window._CLI=null</script>",
`<script>/*<!--*/window._CLI={commits:${JSON.stringify(
commits
)},path:'${currentPath}'}/*-->*/</script>`
)
.replace(
"<head>",
`<head><base href="${vscode.Uri.file(
path.join(context.extensionPath, "site")
).with({
scheme: "vscode-resource"
})}/"/>`
);
panel.webview.html = newIndex;
panel.webview.html = newIndex;
})
.catch(console.error);
panel.webview.onDidReceiveMessage(
message => {
switch (message.command) {
case "commits":
const path = message.params.path;
getCommits(path)
.then(commits => {
console.log(path, commits);
panel.webview.postMessage(commits);
})
.catch(console.error);
}
},
undefined,
context.subscriptions
);
} catch (e) {
console.error(e);
throw e;