Skip to content

Commit

Permalink
✨ Feature: add commandline argvs support for picgo app
Browse files Browse the repository at this point in the history
for example: picgo.exe ./xxx.jpg
  • Loading branch information
Molunerfinn committed Apr 14, 2019
1 parent c64959a commit 6db86ec
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 30 deletions.
91 changes: 61 additions & 30 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getPicBeds } from './utils/getPicBeds'
import pkg from '../../package.json'
import picgoCoreIPC from './utils/picgoCoreIPC'
import fixPath from 'fix-path'
import { getUploadFiles } from './utils/handleArgv'
/**
* Set `__static` path to static files in production
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
Expand Down Expand Up @@ -210,6 +211,7 @@ const createWindow = () => {
window.on('blur', () => {
window.hide()
})
return window
}

const createMiniWidow = () => {
Expand Down Expand Up @@ -250,6 +252,7 @@ const createMiniWidow = () => {
miniWindow.on('closed', () => {
miniWindow = null
})
return miniWindow
}

const createSettingWindow = () => {
Expand Down Expand Up @@ -289,6 +292,7 @@ const createSettingWindow = () => {
})
createMenu()
createMiniWidow()
return settingWindow
}

const createMenu = () => {
Expand Down Expand Up @@ -337,7 +341,7 @@ const uploadClipboardFiles = async () => {
if (miniWindow.isVisible()) {
win = miniWindow
} else {
win = settingWindow || window
win = settingWindow || window || createSettingWindow()
}
let img = await new Uploader(undefined, win.webContents).upload()
if (img !== false) {
Expand Down Expand Up @@ -367,6 +371,33 @@ const uploadClipboardFiles = async () => {
}
}

const uploadChoosedFiles = async (webContents, files) => {
const input = files.map(item => item.path)
const imgs = await new Uploader(input, webContents).upload()
if (imgs !== false) {
const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown'
let pasteText = ''
for (let i in imgs) {
const url = imgs[i].url || imgs[i].imgUrl
pasteText += pasteTemplate(pasteStyle, url) + '\r\n'
const notification = new Notification({
title: '上传成功',
body: imgs[i].imgUrl,
icon: files[i].path
})
setTimeout(() => {
notification.show()
}, i * 100)
db.read().get('uploaded').insert(imgs[i]).write()
}
clipboard.writeText(pasteText)
window.webContents.send('uploadFiles', imgs)
if (settingWindow) {
settingWindow.webContents.send('updateGallery')
}
}
}

picgoCoreIPC(app, ipcMain)

// from macOS tray
Expand Down Expand Up @@ -397,30 +428,7 @@ ipcMain.on('uploadClipboardFilesFromUploadPage', () => {
})

ipcMain.on('uploadChoosedFiles', async (evt, files) => {
const input = files.map(item => item.path)
const imgs = await new Uploader(input, evt.sender).upload()
if (imgs !== false) {
const pasteStyle = db.read().get('settings.pasteStyle').value() || 'markdown'
let pasteText = ''
for (let i in imgs) {
const url = imgs[i].url || imgs[i].imgUrl
pasteText += pasteTemplate(pasteStyle, url) + '\r\n'
const notification = new Notification({
title: '上传成功',
body: imgs[i].imgUrl,
icon: files[i].path
})
setTimeout(() => {
notification.show()
}, i * 100)
db.read().get('uploaded').insert(imgs[i]).write()
}
clipboard.writeText(pasteText)
window.webContents.send('uploadFiles', imgs)
if (settingWindow) {
settingWindow.webContents.send('updateGallery')
}
}
return uploadChoosedFiles(evt, files)
})

ipcMain.on('updateShortKey', (evt, oldKey) => {
Expand Down Expand Up @@ -491,12 +499,25 @@ const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
if (settingWindow) {
if (settingWindow.isMinimized()) {
settingWindow.restore()
app.on('second-instance', (event, commandLine, workingDirectory) => {
let files = getUploadFiles(commandLine, workingDirectory)
if (files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
let win
if (miniWindow && miniWindow.isVisible()) {
win = miniWindow
} else {
win = settingWindow || window || createSettingWindow()
}
uploadChoosedFiles(win.webContents, files)
} else {
if (settingWindow) {
if (settingWindow.isMinimized()) {
settingWindow.restore()
}
settingWindow.focus()
}
}
settingWindow.focus()
}
})
}

if (process.platform === 'win32') {
Expand All @@ -519,6 +540,16 @@ app.on('ready', () => {
globalShortcut.register(db.read().get('settings.shortKey.upload').value(), () => {
uploadClipboardFiles()
})
let files = getUploadFiles()
if (files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
let win
if (miniWindow && miniWindow.isVisible()) {
win = miniWindow
} else {
win = settingWindow || window || createSettingWindow()
}
uploadChoosedFiles(win.webContents, files)
}
})

app.on('window-all-closed', () => {
Expand Down
29 changes: 29 additions & 0 deletions src/main/utils/handleArgv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from 'path'
import fs from 'fs-extra'
const getUploadFiles = (argv = process.argv, cwd = process.cwd()) => {
let files = argv.slice(1)
let result = []
if (files.length > 0) {
result = files.map(item => {
if (path.isAbsolute(item)) {
return {
path: item
}
} else {
let tempPath = path.join(cwd, item)
if (fs.existsSync(tempPath)) {
return {
path: tempPath
}
} else {
return null
}
}
}).filter(item => item !== null)
}
return result
}

export {
getUploadFiles
}

0 comments on commit 6db86ec

Please sign in to comment.