import path from 'path' ; import { spawn } from 'child_process' ; import dayjs from 'dayjs' ; import os from 'os' ; import fs from 'fs-extra' ; import isWsl from 'is-wsl' ; import { IPicGo , IClipboardImage } from '../types' ; import { IBuildInEvent } from './enum' ; import macClipboardScript from './clipboard/mac.applescript' ; import windowsClipboardScript from './clipboard/windows.ps1' ; import windows10ClipboardScript from './clipboard/windows10.ps1' ; import linuxClipboardScript from './clipboard/linux.sh' ; import wslClipboardScript from './clipboard/wsl.sh' ; export type Platform = 'darwin' | 'win32' | 'win10' | 'linux' | 'wsl' ; const getCurrentPlatform = ( ) : Platform => { const platform = process . platform ; if ( isWsl ) { return 'wsl' ; } if ( platform === 'win32' ) { const currentOS = os . release ( ) . split ( '.' ) [ 0 ] ; if ( currentOS === '10' ) { return 'win10' ; } else { return 'win32' ; } } else if ( platform === 'darwin' ) { return 'darwin' ; } else { return 'linux' ; } } ; const platform2ScriptContent = { darwin : macClipboardScript , win32 : windowsClipboardScript , win10 : windows10ClipboardScript , linux : linuxClipboardScript , wsl : wslClipboardScript } ; /**
 * powershell will report error if file does not have a '.ps1' extension,
 * so we should keep the extension name consistent with corresponding shell
 */ const platform2ScriptFilename = { darwin : 'mac.applescript' , win32 : 'windows.ps1' , win10 : 'windows10.ps1' , linux : 'linux.sh' , wsl : 'wsl.sh' } ; const getClipboardImage = async ( ctx ) : Promise < IClipboardImage > => { const imagePath = path . join ( ctx . baseDir , `${ dayjs ( ) . format ( 'YYYYMMDDHHmmss' ) }.png` ) ; return await new Promise < IClipboardImage > ( ( resolve , reject ) : void => { const platform = getCurrentPlatform ( ) ; const scriptPath = path . join ( __dirname , platform2ScriptFilename [ platform ] ) ; if ( ! fs . existsSync ( scriptPath ) ) { fs . writeFileSync ( scriptPath , platform2ScriptContent [ platform ] , 'utf8' ) ; } let execution ; if ( platform === 'darwin' ) { execution = spawn ( 'osascript' , [ scriptPath , imagePath ] ) ; } else if ( platform === 'win32' || platform === 'win10' ) { execution = spawn ( 'powershell' , [ '-noprofile' , '-noninteractive' , '-nologo' , '-sta' , '-executionpolicy' , 'unrestricted' , '-file' , scriptPath , imagePath ] ) ; } else { execution = spawn ( 'sh' , [ scriptPath , imagePath ] ) ; } execution . stdout . on ( 'data' , ( data ) => { if ( platform === 'linux' ) { if ( data . toString ( ) . trim ( ) === 'no xclip' ) { ctx . emit ( IBuildInEvent . NOTIFICATION , { title : 'xclip not found' , body : 'Please install xclip before run picgo' } ) ; return reject ( new Error ( 'Please install xclip before run picgo' ) ) ; } } const imgPath = data . toString ( ) . trim ( ) ; let shouldKeepAfterUploading = false ; if ( path . basename ( imgPath ) !== path . basename ( imagePath ) ) { if ( fs . existsSync ( imgPath ) ) { shouldKeepAfterUploading = true ; } } if ( imgPath !== 'no image' && ! fs . existsSync ( imgPath ) ) { return reject ( new Error ( `Can't find ${ imgPath }` ) ) ; } resolve ( { imgPath , shouldKeepAfterUploading } ) ; } ) ; } ) ; } ; export default getClipboardImage ;