1+ import * as fs from "fs" ;
12import * as vscode from "vscode" ;
23import { Language } from "../graphql-api/types" ;
34const pathModule = require ( "path" ) ;
@@ -29,6 +30,7 @@ const EXTENSION_TO_LANGUAGE: Record<string, Language> = {
2930 ".php4" : Language . Php ,
3031 ".php5" : Language . Php ,
3132 ".php" : Language . Php ,
33+ ".ipynb" : Language . Python ,
3234 ".py" : Language . Python ,
3335 ".py3" : Language . Python ,
3436 ".pm" : Language . Perl ,
@@ -51,6 +53,22 @@ const EXTENSION_TO_LANGUAGE: Record<string, Language> = {
5153 ".yaml" : Language . Yaml ,
5254} ;
5355
56+ /**
57+ * converts the EXTENSION_TO_LANGUAGE object into:
58+ * { [Language]: extension-strings[] }
59+ * used to get all extensions for a language
60+ */
61+ export const LANGUAGE_TO_EXTENSION = Object . entries (
62+ EXTENSION_TO_LANGUAGE
63+ ) . reduce ( ( acc , [ extension , language ] ) => {
64+ if ( acc [ language ] ) {
65+ acc [ language ] . push ( extension ) ;
66+ } else {
67+ acc [ language ] = [ extension ] ;
68+ }
69+ return acc ;
70+ } , { } as { [ key in Language ] : string [ ] } ) ;
71+
5472export function getBasename ( filename : string ) : string | undefined {
5573 const parsedFilename : any = pathModule . parse ( filename ) ;
5674 const basename : string | undefined = parsedFilename . base ;
@@ -190,3 +208,33 @@ export function insertIndentSize(size: number, tabSize: number) {
190208 . fill ( "\t" )
191209 . join ( "" ) ;
192210}
211+
212+ /**
213+ * creates a Uri for the given file, if workspaces are present
214+ * @param fileLocation string - from root
215+ * @returns vscode.Uri | null
216+ */
217+ export default function getFileUri ( fileLocation : string ) : vscode . Uri | null {
218+ const workspaceFolder = vscode . workspace . workspaceFolders ;
219+ /**
220+ * if no workspaces are open or the length is zero,
221+ * then the file cannot exist, so we return null
222+ */
223+ if ( ! workspaceFolder || workspaceFolder . length === 0 ) {
224+ return null ;
225+ }
226+ return vscode . Uri . joinPath ( workspaceFolder [ 0 ] . uri , fileLocation ) ;
227+ }
228+
229+ /**
230+ * checks if a file exists in the location given
231+ * @param fileLocation string - from root
232+ * @returns boolean
233+ */
234+ export function doesFileExist ( fileLocation : string ) : boolean {
235+ const fileUri = getFileUri ( fileLocation ) ;
236+ if ( ! fileUri ) {
237+ return false ;
238+ }
239+ return fs . existsSync ( fileUri . fsPath ) ;
240+ }
0 commit comments