-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathgetClipboardImage.ts
102 lines (95 loc) · 3.45 KB
/
getClipboardImage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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'
const getCurrentPlatform = (): string => {
const platform = process.platform
if (isWsl) {
return 'wsl'
}
if (platform !== 'win32') {
return platform
} else {
const currentOS = os.release().split('.')[0]
if (currentOS === '10') {
return 'win10'
} else {
return 'win32'
}
}
}
// Thanks to vs-picgo: https://github.com/Spades-S/vs-picgo/blob/master/src/extension.ts
const getClipboardImage = async (ctx: IPicGo): Promise<IClipboardImage> => {
const imagePath = path.join(ctx.baseDir, `${dayjs().format('YYYYMMDDHHmmss')}.png`)
return await new Promise<IClipboardImage>((resolve: Function, reject: Function): void => {
const platform: string = getCurrentPlatform()
let execution
// for PicGo GUI
const env = ctx.getConfig('PICGO_ENV') === 'GUI'
const platformPaths: {
[index: string]: string
} = {
darwin: env ? path.join(ctx.baseDir, 'mac.applescript') : './clipboard/mac.applescript',
win32: env ? path.join(ctx.baseDir, 'windows.ps1') : './clipboard/windows.ps1',
win10: env ? path.join(ctx.baseDir, 'windows10.ps1') : './clipboard/windows10.ps1',
linux: env ? path.join(ctx.baseDir, 'linux.sh') : './clipboard/linux.sh',
wsl: env ? path.join(ctx.baseDir, 'wsl.sh') : './clipboard/wsl.sh'
}
const scriptPath = env ? platformPaths[platform] : path.join(__dirname, platformPaths[platform])
if (platform === 'darwin') {
execution = spawn('osascript', [scriptPath, imagePath])
} else if (platform === 'win32' || platform === 'win10') {
execution = spawn('powershell', [
'-noprofile',
'-noninteractive',
'-nologo',
'-sta',
'-executionpolicy', 'unrestricted',
// fix windows 10 native cmd crash bug when "picgo upload"
// https://github.com/PicGo/PicGo-Core/issues/32
// '-windowstyle','hidden',
// '-noexit',
'-file', scriptPath,
imagePath
])
} else {
execution = spawn('sh', [scriptPath, imagePath])
}
execution.stdout.on('data', (data: Buffer) => {
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()
// if the filePath is the real file in system
// we should keep it instead of removing
let shouldKeepAfterUploading = false
// in macOS if your copy the file in system, it's basename will not equal to our default basename
if (path.basename(imgPath) !== path.basename(imagePath)) {
// if the path is not generate by picgo
// but the path exists, we should keep it
if (fs.existsSync(imgPath)) {
shouldKeepAfterUploading = true
}
}
// if the imgPath is invalid
if (imgPath !== 'no image' && !fs.existsSync(imgPath)) {
return reject(new Error(`Can't find ${imgPath}`))
}
resolve({
imgPath,
shouldKeepAfterUploading
})
})
})
}
export default getClipboardImage