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
97 lines (83 loc) · 2.46 KB
/
Copy pathstateFile.ts
File metadata and controls
97 lines (83 loc) · 2.46 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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<{
/**
* This prop describes a running application started manually by the
* e2e suite user by providing the E2E_APP_URL, E2E_APP_ID, E2E_APP_PK, E2E_APP_SK variables
**/
standaloneApp: StandaloneAppParams;
/**
* This prop describes all long-running apps started by the e2e suite itself
**/
longRunningApps: Record<string, AppParams>;
/**
* This prop describes the pid of the http server that serves the clerk-js hotloaded lib.
* The http-server replaces the production clerk-js delivery mechanism.
* The PID is used to teardown the http-server after the tests are done.
*/
clerkJsHttpServerPid: number;
}>;
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;
};
const setClerkJsHttpServerPid = (pid: number) => {
const json = read();
json.clerkJsHttpServerPid = pid;
write(json);
};
const getClerkJsHttpServerPid = () => {
return read().clerkJsHttpServerPid;
};
return {
remove,
setStandAloneApp,
getStandAloneApp,
setClerkJsHttpServerPid,
getClerkJsHttpServerPid,
addLongRunningApp,
getLongRunningApps,
};
};
export const stateFile = createStateFile();