From 6db86ec3595dd4934903d0974a0bd29416cec036 Mon Sep 17 00:00:00 2001 From: PiEgg Date: Sun, 14 Apr 2019 15:41:48 +0800 Subject: [PATCH] :sparkles: Feature: add commandline argvs support for picgo app for example: picgo.exe ./xxx.jpg --- src/main/index.js | 91 ++++++++++++++++++++++++------------ src/main/utils/handleArgv.js | 29 ++++++++++++ 2 files changed, 90 insertions(+), 30 deletions(-) create mode 100644 src/main/utils/handleArgv.js diff --git a/src/main/index.js b/src/main/index.js index eb98adb..e9c528a 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -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 @@ -210,6 +211,7 @@ const createWindow = () => { window.on('blur', () => { window.hide() }) + return window } const createMiniWidow = () => { @@ -250,6 +252,7 @@ const createMiniWidow = () => { miniWindow.on('closed', () => { miniWindow = null }) + return miniWindow } const createSettingWindow = () => { @@ -289,6 +292,7 @@ const createSettingWindow = () => { }) createMenu() createMiniWidow() + return settingWindow } const createMenu = () => { @@ -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) { @@ -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 @@ -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) => { @@ -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') { @@ -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', () => { diff --git a/src/main/utils/handleArgv.js b/src/main/utils/handleArgv.js new file mode 100644 index 0000000..dc74ead --- /dev/null +++ b/src/main/utils/handleArgv.js @@ -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 +}