import * as vscode from "vscode" ; import * as fs from "fs" ; import * as path from "path" ; import * as commandExists from "command-exists" ; import * as cp from "child_process" ; import { debugMode } from "./consts" ; export enum LogLevel { info , warning , error } export type Accidentals = `sharps` | `flats` ; export const getBinPath = ( ) => { const config = getConfiguration ( ) ; return config . general . pathToLilypond ; } ; export const logger = ( msg , logLevel , mute = false ) => { const getLogger = ( ) => { switch ( logLevel ) { case LogLevel . info : return mute ? debugMode ? console . log : ( ) => undefined : vscode . window . showInformationMessage ; case LogLevel . warning : return mute ? debugMode ? console . warn : ( ) => undefined : vscode . window . showWarningMessage ; case LogLevel . error : return mute ? debugMode ? console . error : ( ) => undefined : vscode . window . showErrorMessage ; } } ; getLogger ( ) ( msg ) ; } ; export const lilypondExists = ( ) : boolean => { const binPath = getBinPath ( ) ; const exists = commandExists . sync ( binPath ) ; return exists ; } ; const getLilypondVersion = async ( binPath ) : Promise < string | undefined > => { try { const versionOutput = cp . execSync ( `${ binPath } -v` , { encoding : "utf-8" } ) ; return versionOutput ; } catch ( err ) { logger ( `Can't get LilyPond version: ${ err }` , LogLevel . error , true ) ; return undefined ; } } ; const getExtensionVersion = async ( context ) : Promise < string | undefined > => { const extensionpath = path . join ( context . extensionPath , "package.json" ) ; const packageFileContents = JSON . parse ( fs . readFileSync ( extensionpath , `utf-8` ) ) ; if ( packageFileContents ) { return packageFileContents . version ; } return undefined ; } ; export const setLilypondVersionInStatusBar = async ( context ) : Promise < void > => { const binPath = getBinPath ( ) ; const lilypondVersion = await getLilypondVersion ( binPath ) ; const extensionVersion = await getExtensionVersion ( context ) ; if ( lilypondVersion ) { const lilypondVersionShort = lilypondVersion . split ( `\n` ) [ 0 ] ; const lilypondVersionStatusBarItem = vscode . window . createStatusBarItem ( vscode . StatusBarAlignment . Right , 10 ) ; lilypondVersionStatusBarItem . text = lilypondVersionShort ; lilypondVersionStatusBarItem . tooltip = `VSLilyPond ${ extensionVersion }\n\n` + `LilyPond path: ${ binPath }\n\n` + `${ lilypondVersion }` ; lilypondVersionStatusBarItem . show ( ) ; } } ; export const ensureDirectoryExists = ( dirName ) => { if ( ! fs . existsSync ( dirName ) ) { fs . mkdirSync ( dirName , { recursive : true } ) ; } } ; export const stripFileExtension = ( fileName ) : string => { return fileName . substring ( 0 , fileName . lastIndexOf ( "." ) ) || fileName ; } ; export const notUndefined = < T > ( x ) : x is T => { return x !== undefined ; } ; export const getConfiguration = ( doc = undefined ) => { if ( doc ) { return vscode . workspace . getConfiguration ( `vslilypond` , doc . uri ) ; } if ( vscode . window . activeTextEditor ) { const workspaceFolder = vscode . workspace . getWorkspaceFolder ( vscode . window . activeTextEditor . document . uri ) ; if ( workspaceFolder ) { return vscode . workspace . getConfiguration ( `vslilypond` , workspaceFolder . uri ) ; } } if ( vscode . workspace . workspaceFolders ?. length ) { const workspaceFolder = vscode . workspace . workspaceFolders [ 0 ] ; return vscode . workspace . getConfiguration ( `vslilypond` , workspaceFolder . uri ) ; } return vscode . workspace . getConfiguration ( `vslilypond` ) ; } ; export const errMsgRegex = new RegExp ( [ `([^\\n\\r]+):` , `(\\d+):(\\d+):` , ` (error|warning):` , ` ([^\\n\\r]+)` , ] . join ( `` ) , `gm` ) ;