-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
80 lines (67 loc) · 1.68 KB
/
index.ts
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
import { resolve } from "path";
import * as ora from "ora";
interface SapperBuildOptions {
cwd: string;
src?: string;
routes?: string;
output?: string;
static?: string;
dest?: string;
}
interface Options {
skipDevelopmentBuild: boolean;
path?: string;
buildOptions: SapperBuildOptions;
}
interface PrepareMiddlewareArguments {
dev: boolean;
distDir?: string;
}
interface BuildArguments {
distDir: string;
}
export class SapperApp {
_options: Options = {
skipDevelopmentBuild: false,
path: "__sapper__/dev",
buildOptions: {
cwd: "."
}
};
constructor(options: Options) {
const { buildOptions } = options || {};
this._options = {
...this._options,
...(options || {}),
buildOptions: {
...this._options.buildOptions,
...buildOptions
}
};
}
async prepareMiddleware(args: PrepareMiddlewareArguments) {
const { dev, distDir } = args;
const dir = dev ? this._options.path : distDir;
if (dev) {
if (!this._options.skipDevelopmentBuild) {
const spinner = ora().start("Building Sapper application");
try {
await this.build({ distDir: dir });
spinner.succeed();
} catch (err) {
spinner.fail(err);
}
}
}
const serverPath = resolve(dir, "server/server.js");
return require(serverPath)();
}
build(args: BuildArguments): Promise<void> {
const { distDir } = args;
const options: SapperBuildOptions = {
dest: distDir,
...this._options.buildOptions
};
return require("sapper/api").build(options);
}
}