Skip to content

Commit 3e230de

Browse files
authored
✨ Feature: add support for clipboard in wsl (#87)
1 parent 456b81c commit 3e230de

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"globby": "^8.0.2",
9999
"image-size": "^0.8.3",
100100
"inquirer": "^6.0.0",
101+
"is-wsl": "^2.2.0",
101102
"lodash-id": "^0.14.0",
102103
"lowdb": "^1.0.0",
103104
"md5": "^2.2.1",

src/utils/clipboard/wsl.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
# grab the paths
3+
scriptPath=$(echo $0 | awk '{ print substr( $0, 1, length($0)-6 ) }')"windows10.ps1"
4+
imagePath=$(echo $1 | awk '{ print substr( $0, 1, length($0)-18 ) }')
5+
imageName=$(echo $1 | awk '{ print substr( $0, length($0)-17, length($0) ) }')
6+
7+
# run the powershell script
8+
res=$(powershell.exe -noprofile -noninteractive -nologo -sta -executionpolicy unrestricted -file $(wslpath -w $scriptPath) $(wslpath -w $imagePath)"\\"$imageName)
9+
10+
# note that there is a return symbol in powershell result
11+
noImage=$(echo "no image\r")
12+
13+
# check whether image exists
14+
if [ "$res" = "$noImage" ] ;then
15+
echo "no image"
16+
else
17+
echo $1
18+
fi

src/utils/getClipboardImage.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import { spawn } from 'child_process'
33
import dayjs from 'dayjs'
44
import os from 'os'
55
import fs from 'fs-extra'
6+
import isWsl from 'is-wsl'
67
import { IPicGo, IClipboardImage } from '../types'
78
import { IBuildInEvent } from './enum'
89

910
const getCurrentPlatform = (): string => {
1011
const platform = process.platform
12+
if (isWsl) {
13+
return 'wsl'
14+
}
1115
if (platform !== 'win32') {
1216
return platform
1317
} else {
@@ -34,7 +38,8 @@ const getClipboardImage = async (ctx: IPicGo): Promise<IClipboardImage> => {
3438
darwin: env ? path.join(ctx.baseDir, 'mac.applescript') : './clipboard/mac.applescript',
3539
win32: env ? path.join(ctx.baseDir, 'windows.ps1') : './clipboard/windows.ps1',
3640
win10: env ? path.join(ctx.baseDir, 'windows10.ps1') : './clipboard/windows10.ps1',
37-
linux: env ? path.join(ctx.baseDir, 'linux.sh') : './clipboard/linux.sh'
41+
linux: env ? path.join(ctx.baseDir, 'linux.sh') : './clipboard/linux.sh',
42+
wsl: env ? path.join(ctx.baseDir, 'wsl.sh') : './clipboard/wsl.sh'
3843
}
3944
const scriptPath = env ? platformPaths[platform] : path.join(__dirname, platformPaths[platform])
4045
if (platform === 'darwin') {

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3234,6 +3234,11 @@ is-directory@^0.3.1:
32343234
resolved "https://registry.yarnpkg.com/is-directory/-/is-directory-0.3.1.tgz#61339b6f2475fc772fd9c9d83f5c8575dc154ae1"
32353235
integrity sha1-YTObbyR1/Hcv2cnYP1yFddwVSuE=
32363236

3237+
is-docker@^2.0.0:
3238+
version "2.2.1"
3239+
resolved "https://registry.yarnpkg.com/is-docker/-/is-docker-2.2.1.tgz#33eeabe23cfe86f14bde4408a02c0cfb853acdaa"
3240+
integrity sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==
3241+
32373242
is-extendable@^0.1.0, is-extendable@^0.1.1:
32383243
version "0.1.1"
32393244
resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89"
@@ -3380,6 +3385,13 @@ is-windows@^1.0.1, is-windows@^1.0.2:
33803385
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"
33813386
integrity sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==
33823387

3388+
is-wsl@^2.2.0:
3389+
version "2.2.0"
3390+
resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-2.2.0.tgz#74a4c76e77ca9fd3f932f290c17ea326cd157271"
3391+
integrity sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==
3392+
dependencies:
3393+
is-docker "^2.0.0"
3394+
33833395
isarray@0.0.1:
33843396
version "0.0.1"
33853397
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"

0 commit comments

Comments
 (0)