@@ -7,6 +7,7 @@ const parseConfig = require('parse-git-config');
77const gitBranch = require ( 'git-branch' ) ;
88const githubUrlFromGit = require ( 'github-url-from-git' ) ;
99const copyPaste = require ( "copy-paste" ) ;
10+ const fs = require ( "fs" ) ;
1011
1112const extensionName = 'Github-File-Url' ;
1213
@@ -20,21 +21,45 @@ const TYPE = {
2021function 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 ;
0 commit comments