Skip to content

Commit 0468494

Browse files
committed
Issues #3 and #4.
Bumped version to 1.2.0
1 parent b6948af commit 0468494

File tree

2 files changed

+88
-20
lines changed

2 files changed

+88
-20
lines changed

extension.js

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const parseConfig = require('parse-git-config');
77
const gitBranch = require('git-branch');
88
const githubUrlFromGit = require('github-url-from-git');
99
const copyPaste = require("copy-paste");
10+
const fs = require("fs");
1011

1112
const extensionName = 'Github-File-Url';
1213

@@ -20,21 +21,45 @@ const TYPE = {
2021
function activate(context) {
2122

2223
// The command has been defined in the package.json file
23-
const commandName1 = 'extension.github-file-folder-url.copyGithubUrlWithSelection';
24-
context.subscriptions.push(vscode.commands.registerCommand(commandName1, (fileUri) => {
25-
executeCommand1(commandName1, fileUri, true);
26-
}));
27-
const commandName2 = 'extension.github-file-folder-url.copyGithubUrl';
28-
context.subscriptions.push(vscode.commands.registerCommand(commandName2, (fileUri) => {
29-
executeCommand1(commandName2, fileUri, false);
30-
}));
31-
const commandName3 = 'extension.github-file-folder-url.copyGithubUrlForAllOpenFiles';
32-
context.subscriptions.push(vscode.commands.registerCommand(commandName3, () => {
33-
executeCommandAllTextEditors(commandName3);
34-
}));
24+
{
25+
const commandName = 'extension.github-file-folder-url.copyGithubUrlWithSelection';
26+
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri) => {
27+
executeCommand1(commandName, fileUri, true, false);
28+
}));
29+
}
30+
{
31+
const commandName = 'extension.github-file-folder-url.copyGithubUrlWithSelection-simple';
32+
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri) => {
33+
executeCommand1(commandName, fileUri, true, true);
34+
}));
35+
}
36+
{
37+
const commandName = 'extension.github-file-folder-url.copyGithubUrl';
38+
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri) => {
39+
executeCommand1(commandName, fileUri, false, false);
40+
}));
41+
}
42+
{
43+
const commandName = 'extension.github-file-folder-url.copyGithubUrl-simple';
44+
context.subscriptions.push(vscode.commands.registerCommand(commandName, (fileUri) => {
45+
executeCommand1(commandName, fileUri, false, true);
46+
}));
47+
}
48+
{
49+
const commandName = 'extension.github-file-folder-url.copyGithubUrlForAllOpenFiles';
50+
context.subscriptions.push(vscode.commands.registerCommand(commandName, (args) => {
51+
executeCommandAllTextEditors(commandName, false);
52+
}));
53+
}
54+
{
55+
const commandName = 'extension.github-file-folder-url.copyGithubUrlForAllOpenFiles-simple';
56+
context.subscriptions.push(vscode.commands.registerCommand(commandName, (args) => {
57+
executeCommandAllTextEditors(commandName, true);
58+
}));
59+
}
3560
}
3661

37-
function executeCommandAllTextEditors(commandName) {
62+
function executeCommandAllTextEditors(commandName, simpleFormat) {
3863

3964
try {
4065
const workspaceRootPath = vscode.workspace.rootPath;
@@ -47,20 +72,39 @@ function executeCommandAllTextEditors(commandName) {
4772
return;
4873
}
4974

50-
textEditors.forEach(p => uniquePaths[p.fileName] = true);
75+
textEditors.forEach(p => {
76+
uniquePaths[p.fileName] = p;
77+
});
78+
79+
const allFilePaths = Object.keys(uniquePaths);
5180

5281
const allPaths = [];
5382
const allErrors = [];
83+
const allWarnings = [];
5484

5585
for (let filePath in uniquePaths) {
86+
if (!fs.existsSync(filePath)) {
87+
const extension = path.extname(filePath);
88+
const errorMessage = `The file '${filePath}' does not exist locally, so no url was generated.`;
89+
if (allFilePaths.length > 1) {
90+
if (extension === '.rendered') {
91+
// silently skip this
92+
} else {
93+
allWarnings.push(errorMessage);
94+
}
95+
} else {
96+
allErrors.push(errorMessage);
97+
}
98+
continue;
99+
}
56100
const result = generateGithubUrl(commandName, workspaceRootPath, filePath, null);
57101
if (result) {
58102
switch (result.type) {
59103
case 'success':
60104
{
61105
const url = result.url;
62106
const relativeFilePath = result.relativePathFromGitRoot;
63-
const urlMarkdownLink = `[${relativeFilePath}](${url})`;
107+
const urlMarkdownLink = simpleFormat ? url : `[${relativeFilePath}](${url})`;
64108
allPaths.push(urlMarkdownLink);
65109
}
66110
break;
@@ -108,7 +152,7 @@ Workspace Root: ${workspaceRootPath}`;
108152
}
109153

110154
}
111-
function executeCommand1(commandName, fileUri, pullLines) {
155+
function executeCommand1(commandName, fileUri, pullLines, simpleFormat) {
112156
try {
113157
const workspaceRootPath = vscode.workspace.rootPath;
114158
let lineInfo = null;
@@ -138,14 +182,20 @@ function executeCommand1(commandName, fileUri, pullLines) {
138182
filePath = editor.document.fileName;
139183
}
140184

185+
if (!fs.existsSync(filePath)) {
186+
// we generate a warning but still generate the url
187+
const errorMessage = `The file '${filePath}' does not exist locally, so no url was generated.`;
188+
allWarnings.push(errorMessage);
189+
}
190+
141191
const result = generateGithubUrl(commandName, workspaceRootPath, filePath, lineInfo);
142192
if (result) {
143193
switch (result.type) {
144194
case 'success':
145195
{
146196
const url = result.url;
147197
const relativeFilePath = result.relativePathFromGitRoot;
148-
const urlMarkdownLink = `[${relativeFilePath}](${url})`;
198+
const urlMarkdownLink = simpleFormat ? url :`[${relativeFilePath}](${url})`;
149199
copyPaste.copy(urlMarkdownLink);
150200
}
151201
return;

package.json

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vscode-github-file-folder-url",
3-
"version": "1.1.8",
3+
"version": "1.2.0",
44
"publisher": "cycloware",
55
"displayName": "VsCode - Github-File-Folder-Url",
66
"description": "VsCode Extension that gets a Github url with optional line selection for files and folders.",
@@ -24,8 +24,11 @@
2424
],
2525
"activationEvents": [
2626
"onCommand:extension.github-file-folder-url.copyGithubUrl",
27+
"onCommand:extension.github-file-folder-url.copyGithubUrl-simple",
2728
"onCommand:extension.github-file-folder-url.copyGithubUrlWithSelection",
28-
"onCommand:extension.github-file-folder-url.copyGithubUrlForAllOpenFiles"
29+
"onCommand:extension.github-file-folder-url.copyGithubUrlWithSelection-simple",
30+
"onCommand:extension.github-file-folder-url.copyGithubUrlForAllOpenFiles",
31+
"onCommand:extension.github-file-folder-url.copyGithubUrlForAllOpenFiles-simple"
2932
],
3033
"main": "./extension",
3134
"contributes": {
@@ -34,6 +37,10 @@
3437
{
3538
"command": "extension.github-file-folder-url.copyGithubUrl",
3639
"title": "Copy GitHub URL"
40+
},
41+
{
42+
"command": "extension.github-file-folder-url.copyGithubUrl-simple",
43+
"title": "Copy RAW GitHub URL"
3744
}
3845
]
3946
},
@@ -42,15 +49,26 @@
4249
"command": "extension.github-file-folder-url.copyGithubUrl",
4350
"title": "Copy GitHub URL"
4451
},
52+
{
53+
"command": "extension.github-file-folder-url.copyGithubUrl-simple",
54+
"title": "Copy RAW GitHub URL"
55+
},
4556
{
4657
"command": "extension.github-file-folder-url.copyGithubUrlWithSelection",
4758
"title": "Copy GitHub URL with Selection"
4859
},
60+
{
61+
"command": "extension.github-file-folder-url.copyGithubUrlWithSelection-simple",
62+
"title": "Copy RAW GitHub URL with Selection"
63+
},
4964
{
5065
"command": "extension.github-file-folder-url.copyGithubUrlForAllOpenFiles",
5166
"title": "Copy GitHub URL for all open files"
67+
},
68+
{
69+
"command": "extension.github-file-folder-url.copyGithubUrlForAllOpenFiles-simple",
70+
"title": "Copy RAW GitHub URL for all open files"
5271
}
53-
5472
],
5573
"keybindings": [
5674
{

0 commit comments

Comments
 (0)