-
Notifications
You must be signed in to change notification settings - Fork 258
/
scully.ts
102 lines (93 loc) · 3.25 KB
/
scully.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env node
/**
* The above line is needed to be able to run in npx and CI.
*/
import {readFileSync} from 'fs-extra';
import {join} from 'path';
import * as yargs from 'yargs';
import './pluginManagement/systemPlugins';
import {startBackgroundServer} from './startBackgroundServer';
import {loadConfig} from './utils/config';
import {moveDistAngular} from './utils/fsAngular';
import {httpGetJson} from './utils/httpGetJson';
import {isPortTaken} from './utils/isPortTaken';
import {logError, logWarn, yellow} from './utils/log';
import {startScully} from './utils/startup';
import {waitForServerToBeAvailable} from './utils/waitForServerToBeAvailable';
import {bootServe, isBuildThere, watchMode} from './watchMode';
/** the default of 10 is too shallow for generating pages. */
require('events').defaultMaxListeners = 100;
const {argv: options} = yargs.option('port', {
alias: 'p',
type: 'number',
description: 'The port to run on',
});
const {watch} = yargs
.boolean('wm')
.default('wm', false)
.alias('wm', 'watch')
.describe('wm', 'Use this flag for use the watch mode into scully').argv;
const {removeStaticDist} = yargs
.boolean('RSD')
.default('RSD', false)
.alias('RSD', 'removeStaticDist')
.describe('RSD', 'Use this flag to remove the Scully outfolder before starting').argv;
if (process.argv.includes('version')) {
const {version} = JSON.parse(readFileSync(join(__dirname, './package.json')).toString());
console.log('version:', version);
process.exit(0);
}
(async () => {
/** make sure not to do something before the config is ready */
const scullyConfig = await loadConfig;
if (process.argv.includes('killServer')) {
await httpGetJson(`http://${scullyConfig.hostName}:${scullyConfig.appPort}/killMe`, {
suppressErrors: true,
});
process.exit(0);
return;
}
await isBuildThere(scullyConfig);
if (process.argv.includes('serve')) {
await bootServe(scullyConfig);
} else {
const folder = join(scullyConfig.homeFolder, scullyConfig.distFolder);
/** copy in current build artifacts */
await moveDistAngular(folder, scullyConfig.outDir, {removeStaticDist, reset: false});
const isTaken = await isPortTaken(scullyConfig.staticport);
if (typeof scullyConfig.hostUrl === 'string') {
logWarn(`
You are using "${yellow(scullyConfig.hostUrl)}" as server.
`);
} else {
/** server ports already in use? */
if (!isTaken) {
startBackgroundServer(scullyConfig);
} else {
// debug only
console.log(`Background servers already running.`);
}
if (!(await waitForServerToBeAvailable().catch(e => false))) {
logError('Could not connect to server');
process.exit(15);
}
}
if (watch) {
watchMode(
join(scullyConfig.homeFolder, scullyConfig.distFolder) ||
join(scullyConfig.homeFolder, './dist/browser')
);
} else {
// console.log('servers available');
await startScully();
if (!isTaken && typeof scullyConfig.hostUrl !== 'string') {
// kill serve ports
await httpGetJson(`http://${scullyConfig.hostName}:${scullyConfig.appPort}/killMe`, {
suppressErrors: true,
});
}
/** done, stop the program */
process.exit(0);
}
}
})();