-
Notifications
You must be signed in to change notification settings - Fork 116
/
deployCkEditorBundle.js
134 lines (115 loc) · 3.92 KB
/
deployCkEditorBundle.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// @ts-check
/**
* Usage: yarn migrate up|down|pending|executed [dev|staging|prod] [forumType]
*
* If no environment is specified, you can use the environment variables PG_URL
* and SETTINGS_FILE
*/
require("ts-node/register");
const { getDatabaseConfig, startSshTunnel } = require("./scripts/startup/buildUtil");
const { exec } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);
const initGlobals = (args, isProd) => {
global.bundleIsServer = true;
global.bundleIsTest = false;
global.bundleIsE2E = false;
global.bundleIsProduction = isProd;
global.bundleIsMigrations = true;
global.defaultSiteAbsoluteUrl = "";
global.serverPort = 5001;
global.estrellaPid = -1;
const { getInstanceSettings } = require("./packages/lesswrong/lib/executionEnvironment");
getInstanceSettings(args); // These args will be cached for later
}
const fetchImports = (args, isProd) => {
initGlobals(args, isProd);
const { ckEditorApi: { checkEditorBundle, uploadEditorBundle } } = require('./packages/lesswrong/server/ckEditor/ckEditorApi');
const { ckEditorBundleVersion } = require('./packages/lesswrong/lib/wrapCkEditor')
return { ckEditorBundleVersion, checkEditorBundle, uploadEditorBundle };
}
const credentialsPath = (forumType) => {
const memorizedBases = {
lw: "..",
ea: "..",
};
const base = process.env.GITHUB_WORKSPACE ?? memorizedBases[forumType] ?? ".";
const memorizedRepoNames = {
lw: 'LessWrong-Credentials',
ea: 'ForumCredentials',
};
const repoName = memorizedRepoNames[forumType];
if (!repoName) {
return base;
}
return `${base}/${repoName}`;
}
const settingsFilePath = (fileName, forumType) => {
return `${credentialsPath(forumType)}/${fileName}`;
}
const databaseConfig = (mode, forumType) => {
if (!mode) {
return {};
}
const memorizedConfigPaths = {
lw: {
db: `${credentialsPath(forumType)}/connectionConfigs/${mode}.json`,
},
ea: {
postgresUrlFile: `${credentialsPath(forumType)}/${mode}-pg-conn.txt`,
},
};
const configPath = memorizedConfigPaths[forumType] || {
postgresUrlFile: `${credentialsPath(forumType)}/${mode}-pg-conn.txt`,
};
return getDatabaseConfig(configPath);
};
const settingsFileName = (mode, forumType) => {
if (!mode) {
// With the state of the code when this comment was written, this indicates
// an error condition, but it will be handled later, around L60
return '';
}
if (forumType === 'lw') {
if (mode === 'prod') {
return 'settings-production-lesswrong.json';
}
return 'settings-local-dev-devdb.json'
}
return `settings-${mode}.json`;
};
(async () => {
let mode = process.argv[2];
if (mode === "development") {
mode = "dev";
} else if (mode === "production") {
mode = "prod";
}
const forumType = process.argv[3];
const dbConf = databaseConfig(mode, forumType);
if (dbConf.postgresUrl) {
process.env.PG_URL = dbConf.postgresUrl;
}
const args = {
postgresUrl: process.env.PG_URL,
settingsFileName: settingsFilePath(settingsFileName(mode, forumType), forumType),
shellMode: false,
};
await startSshTunnel(databaseConfig(mode, forumType).sshTunnelCommand);
const { ckEditorBundleVersion, checkEditorBundle, uploadEditorBundle } = fetchImports(args, mode === "prod");
const {initServer} = require("./packages/lesswrong/server/serverStartup");
await initServer(args);
let exitCode = 0;
try {
const { exists } = await checkEditorBundle(ckEditorBundleVersion);
if (!exists) {
console.log(`ckEditor bundle version ${ckEditorBundleVersion} not yet uploaded; building now`);
await execAsync(`cd public/lesswrong-editor && yarn && yarn build`);
await uploadEditorBundle(ckEditorBundleVersion);
}
} catch (e) {
console.error("An error occurred while checking, building, or uploading the ckEditor bundle version:", e);
exitCode = 1;
}
process.exit(exitCode);
})();