forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstateFile.ts
More file actions
72 lines (60 loc) · 1.64 KB
/
Copy pathstateFile.ts
File metadata and controls
72 lines (60 loc) · 1.64 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
import { constants } from '../constants';
import { fs } from '../scripts';
import type { EnvironmentConfig } from './environment';
type AppParams = {
id: string;
port: number;
serverUrl: string;
pid?: number;
appDir: string;
env: ReturnType<EnvironmentConfig['toJson']>;
};
type StandaloneAppParams = {
port: number;
serverUrl: string;
};
type StateFile = Partial<{
standaloneApp: StandaloneAppParams;
longRunningApps: Record<string, AppParams>;
}>;
const createStateFile = () => {
const remove = () => {
return fs.removeSync(constants.APPS_STATE_FILE);
};
const read = () => {
fs.ensureFileSync(constants.APPS_STATE_FILE);
const contents = fs.readJsonSync(constants.APPS_STATE_FILE, { throws: false });
return (contents || {}) as StateFile;
};
const write = (json: Record<string, unknown>) => {
fs.ensureFileSync(constants.APPS_STATE_FILE);
fs.writeJsonSync(constants.APPS_STATE_FILE, json, { spaces: 2 });
};
const setStandAloneApp = (params: StandaloneAppParams) => {
const json = read();
json.standaloneApp = params;
write(json);
};
const getStandAloneApp = () => {
const json = read();
return json.standaloneApp;
};
const addLongRunningApp = (params: AppParams) => {
const json = read();
json.longRunningApps = json.longRunningApps || {};
json.longRunningApps[params.id] = params;
write(json);
};
const getLongRunningApps = () => {
const json = read();
return json.longRunningApps;
};
return {
remove,
setStandAloneApp,
getStandAloneApp,
addLongRunningApp,
getLongRunningApps,
};
};
export const stateFile = createStateFile();