-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathfirst-run.ts
More file actions
82 lines (68 loc) · 1.96 KB
/
first-run.ts
File metadata and controls
82 lines (68 loc) · 1.96 KB
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
import fs from 'node:fs';
import path from 'node:path';
import { app, dialog } from 'electron';
import { APPLICATION } from '../../shared/constants';
import { logError, toError } from '../../shared/logger';
import { isMacOS } from '../../shared/platform';
import { isDevMode } from '../utils';
/**
* On first launch, write the first-run marker file and prompt macOS users
* to move the app to the Applications folder. No-ops on subsequent launches.
*/
export async function onFirstRunMaybe() {
if (checkAndMarkFirstRun()) {
await promptMoveToApplicationsFolder();
}
}
/**
* Ask user if the app should be moved to the applications folder (macOS).
*/
async function promptMoveToApplicationsFolder() {
if (!isMacOS()) {
return;
}
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();
}
}
/**
* Returns the absolute path to the first-run marker file in the user data directory.
*/
const getConfigPath = () => {
const userDataPath = app.getPath('userData');
return path.join(userDataPath, 'FirstRun', APPLICATION.FIRST_RUN_FOLDER);
};
/**
* Determine if this is the first run of the application by checking for the existence of a specific file.
*
* @returns true if this is the first run, false otherwise
*/
function checkAndMarkFirstRun(): boolean {
const configPath = getConfigPath();
try {
if (fs.existsSync(configPath)) {
return false;
}
const firstRunFolder = path.dirname(configPath);
if (!fs.existsSync(firstRunFolder)) {
fs.mkdirSync(firstRunFolder);
}
fs.writeFileSync(configPath, '');
} catch (err) {
logError(
'checkAndMarkFirstRun',
'Unable to write firstRun file',
toError(err),
);
}
return true;
}