From 3be89997862b051cf2f8288d15fb817576cb6163 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Fri, 19 Jun 2020 09:43:07 -0700 Subject: [PATCH] feat: move to app folder on first run --- first-run.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.js | 8 ++++++-- package.json | 3 ++- tsconfig.json | 2 +- 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 first-run.js diff --git a/first-run.js b/first-run.js new file mode 100644 index 000000000..249171211 --- /dev/null +++ b/first-run.js @@ -0,0 +1,54 @@ +const { app, dialog } = require('electron'); + +const fs = require('fs-extra'); +const path = require('path'); + +async function onFirstRunMaybe() { + if (isFirstRun()) { + await promptMoveToApplicationsFolder(); + } +} + +// Ask user if the app should be moved to the applications folder. +async function promptMoveToApplicationsFolder() { + if (process.platform !== 'darwin') return; + + const isDevMode = !!process.defaultApp; + if (isDevMode || app.isInApplicationsFolder()) return; + + const { response } = await dialog.showMessageBox({ + type: 'question', + buttons: ['Move to Applications Folder', 'Do Not Move'], + defaultId: 0, + message: 'Move to Applications Folder?', + }); + + if (response === 0) { + app.moveToApplicationsFolder(); + } +} + +const getConfigPath = () => { + const userDataPath = app.getPath('userData'); + return path.join(userDataPath, 'FirstRun', 'gitify-first-run'); +}; + +// Whether or not the app is being run for the first time. +function isFirstRun() { + const configPath = getConfigPath(); + + try { + if (fs.existsSync(configPath)) { + return false; + } + + fs.outputFileSync(configPath, ''); + } catch (error) { + console.warn(`First run: Unable to write firstRun file`, error); + } + + return true; +} + +module.exports = { onFirstRunMaybe } + diff --git a/main.js b/main.js index feffefa88..602d6951b 100644 --- a/main.js +++ b/main.js @@ -1,11 +1,11 @@ -const { ipcMain } = require('electron'); +const { ipcMain, app } = require('electron'); const { menubar } = require('menubar'); const { autoUpdater } = require('electron-updater'); +const { onFirstRunMaybe } = require('./first-run'); const path = require('path'); const iconIdle = path.join(__dirname, 'assets', 'images', 'tray-idleTemplate.png'); const iconActive = path.join(__dirname, 'assets', 'images', 'tray-active.png'); -const isDarwin = process.platform === 'darwin'; const browserWindowOpts = { width: 500, @@ -19,6 +19,10 @@ const browserWindowOpts = { }, }; +app.on('ready', async () => { + await onFirstRunMaybe(); +}) + const menubarApp = menubar({ icon: iconIdle, index: `file://${__dirname}/index.html`, diff --git a/package.json b/package.json index cb2c28932..09828b14a 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,8 @@ "assets/**/*", "index.html", "LICENSE", - "main.js" + "main.js", + "first-run.js" ], "mac": { "category": "public.app-category.developer-tools", diff --git a/tsconfig.json b/tsconfig.json index 50a97f641..7bf1647a5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,5 +11,5 @@ "typeRoots": ["./src/types", "node_modules/@types"] }, "exclude": ["node_modules"], - "include": ["src/**/*.js", "src/**/*.ts", "src/**/*.tsx"] + "include": ["src/**/*.js", "src/**/*.ts", "src/**/*.tsx", "first-run.js", "main.js"] }