This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
/
index.js
65 lines (60 loc) · 1.73 KB
/
index.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
// @flow
import cosmiconfig from "cosmiconfig";
import serve from "serve";
import flattenConfiguration from "./configuration/flattenConfiguration.js";
import start from "./commands/start.js";
import build from "./commands/build.js";
import log from "./utils/log.js";
const handleError = error => {
if (error.message) log.error(error.message);
if (error.stack) log.error(error.stack);
process.exit(1);
};
function normalizeConfiguration(
config?: PhenomicInputConfig
): Promise<PhenomicConfig> {
const configExplorer = cosmiconfig("phenomic", { cache: false });
return configExplorer
.load(process.cwd())
.then(result => {
if (result === null) {
throw new Error(
"No configuration file found. Please add a 'phenomic' section in package.json or " +
"create a file named .phenomicrc(.json|.yaml)? or phenomic.config.js." +
"\nSee https://phenomic.io/docs/configuration/"
);
}
return flattenConfiguration({
...result.config,
...(config || {})
});
})
.catch(handleError);
}
export default {
start(inputConfig?: PhenomicInputConfig) {
normalizeConfiguration(inputConfig)
.then(start)
.catch(handleError);
},
build(inputConfig?: PhenomicInputConfig) {
normalizeConfiguration(inputConfig)
.then(build)
.catch(handleError);
},
async preview(inputConfig?: PhenomicInputConfig) {
try {
const config = await normalizeConfiguration(inputConfig);
await build(config);
log(
`⚡️ Serving on http://localhost:${config.port}` +
config.baseUrl.pathname
);
serve(config.outdir, {
port: config.port
});
} catch (e) {
handleError(e);
}
}
};