Skip to content

Commit a840775

Browse files
committed
fix build
1 parent 64fddf1 commit a840775

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed

build.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ logger.info('Building...');
1313
outdir: path.join(process.cwd(), 'dist'),
1414
splitting: false,
1515
write: false,
16-
minify: true,
16+
tsconfig: path.resolve(process.cwd(), 'tsconfig.json'),
17+
minify: false,
1718
entryPoints: [path.resolve(process.cwd(), 'packages/server/index.ts')],
1819
loader: {
1920
'.frontend': 'base64',
2021
'.ttf': 'base64',
2122
'.wasm': 'base64',
2223
},
24+
alias: {
25+
ws: `${path.dirname(require.resolve('ws/package.json'))}/index.js`,
26+
saslprep: path.resolve(__dirname, 'saslprep.js'),
27+
},
2328
});
2429
if (res.errors.length) console.error(res.errors);
2530
if (res.warnings.length) console.warn(res.warnings);

packages/server/config.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ const isClient = process.argv.includes('--client');
1111
const configPath = path.resolve(process.cwd(), `config.${isClient ? 'client' : 'server'}.yaml`);
1212
fs.ensureDirSync(path.resolve(process.cwd(), 'data'));
1313

14+
// eslint-disable-next-line import/no-mutable-exports
15+
export let exit: Promise<void> | null = null;
16+
1417
if (!fs.existsSync(configPath)) {
15-
const serverConfigDefault = `\
18+
// eslint-disable-next-line no-promise-executor-return
19+
exit = new Promise((resolve) => (async () => {
20+
const serverConfigDefault = `\
1621
type:
1722
viewPass: ${String.random(8)}
1823
server:
@@ -23,18 +28,19 @@ secretRoute: ${String.random(12)}
2328
seatFile: /home/icpc/Desktop/seat.txt
2429
`;
2530

26-
const clientConfigDefault = yaml.dump({
27-
server: '',
28-
balloon: '',
29-
printers: (await getPrinters()).map((p) => p.printer),
30-
token: '',
31-
});
32-
fs.writeFileSync(configPath, isClient ? clientConfigDefault : serverConfigDefault);
33-
logger.error('Config file generated, please fill in the config.yaml');
34-
process.exit(1);
31+
const clientConfigDefault = yaml.dump({
32+
server: '',
33+
balloon: '',
34+
printers: await getPrinters().then((r) => r.map((p) => p.printer)).catch(() => []),
35+
token: '',
36+
});
37+
fs.writeFileSync(configPath, isClient ? clientConfigDefault : serverConfigDefault);
38+
logger.error('Config file generated, please fill in the config.yaml');
39+
resolve();
40+
})());
41+
throw new Error();
3542
}
3643

37-
const data = yaml.load(fs.readFileSync(configPath, 'utf8').toString());
3844
const serverSchema = Schema.object({
3945
viewPass: Schema.string().default(String.random(8)),
4046
server: Schema.string().role('url').required(),
@@ -52,12 +58,12 @@ const clientSchema = Schema.object({
5258
token: Schema.string().required().description('Token generated on server'),
5359
fonts: Schema.array(Schema.string()).default([]),
5460
});
55-
export const config = (isClient ? clientSchema : serverSchema)(data as any);
5661

62+
export const config = (isClient ? clientSchema : serverSchema)(yaml.load(fs.readFileSync(configPath, 'utf8')) as any);
5763
export const saveConfig = () => {
5864
fs.writeFileSync(configPath, yaml.dump(config));
5965
};
6066

6167
logger.info(`Config loaded from ${configPath}`);
6268
logger.info(`xcpc-tools version: ${version}`);
63-
if (!isClient) logger.info(`Server View User Info: admin/${config.viewPassword}`);
69+
if (!isClient && !exit) logger.info(`Server View User Info: admin/${config.viewPassword}`);

packages/server/index.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os from 'os';
22
import path from 'path';
33
import { Context } from 'cordis';
4-
import { config } from './config';
54
import { fs, Logger } from './utils';
65

76
Logger.levels.base = 3;
@@ -15,25 +14,29 @@ const app = new Context();
1514
const tmpdir = path.resolve(os.tmpdir(), 'xcpc-tools');
1615
fs.ensureDirSync(tmpdir);
1716

17+
let config;
18+
try {
19+
config = require('./config').config;
20+
} catch (e) { }
21+
1822
async function applyServer(ctx: Context) {
19-
fs.ensureDirSync(tmpdir);
20-
await require('./service/server').apply(ctx);
21-
await require('./service/db').apply(ctx);
23+
ctx.plugin(await import('./service/server'));
24+
ctx.plugin((await import('./service/db')).default);
2225
if (config.type !== 'server') {
2326
logger.info('Fetch mode: ', config.type);
24-
await require('./service/fetcher').apply(ctx);
27+
ctx.plugin(await import('./service/fetcher'));
2528
}
26-
await require('./handler/misc').apply(ctx);
27-
await require('./handler/printer').apply(ctx);
28-
await require('./handler/monitor').apply(ctx);
29-
await require('./handler/client').apply(ctx);
30-
await require('./handler/balloon').apply(ctx);
31-
await require('./handler/commands').apply(ctx);
29+
ctx.plugin(await import('./handler/misc'));
30+
ctx.plugin(await import('./handler/printer'));
31+
ctx.plugin(await import('./handler/monitor'));
32+
ctx.plugin(await import('./handler/client'));
33+
ctx.plugin(await import('./handler/balloon'));
34+
ctx.plugin(await import('./handler/commands'));
3235
}
3336

3437
async function applyClient(ctx: Context) {
35-
if (config.printers?.length) await require('./client/printer').apply(ctx);
36-
if (config.balloon) await require('./client/balloon').apply(ctx);
38+
if (config.printers?.length) ctx.plugin(await import('./client/printer'));
39+
if (config.balloon) ctx.plugin(await import('./client/balloon'));
3740
}
3841

3942
async function apply(ctx) {
@@ -49,4 +52,4 @@ async function apply(ctx) {
4952
await ctx.parallel('app/ready');
5053
}
5154

52-
apply(app);
55+
if (config) apply(app);

packages/server/interface.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,6 @@ declare module 'cordis' {
1313

1414
export type VoidReturn = Promise<any> | any;
1515

16-
export interface ToolsConfig {
17-
type: string;
18-
endpoint: string;
19-
contestId: string;
20-
uname: string;
21-
password: string;
22-
}
23-
24-
export interface Tools {
25-
contest: {
26-
info: any,
27-
id: string,
28-
name: string,
29-
},
30-
}
31-
3216
export interface PrintCodeDoc {
3317
_id: string;
3418
tid: string;

saslprep.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default (str) => str;

0 commit comments

Comments
 (0)