Skip to content

Commit 958d581

Browse files
SanderEliasjorgeucano
authored andcommitted
feat(hostname): puts in a hostname option (#187)
* feat(hostname): puts in a hostname option on some computers, localhost isn't avaialble. This pr takes care of that by making the name to be used an setting closes #87 * docs(scully-configuration docs): update the docs to reflect new settings
1 parent 2514dd1 commit 958d581

12 files changed

+73
-53
lines changed

docs/scully-configuration.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ or teach to the community how to combine scully with others tools.
2121
- [appPort](#appport)
2222
- [staticport](#staticport)
2323
- [puppeteerLaunchOptions](#puppeteerlaunchoptions)
24+
- [hostName](#hostname)
25+
- [hostUrl](#hosturl)
2426

2527
## `ScullyConfig` Interface
2628

@@ -35,6 +37,8 @@ export interface ScullyConfig {
3537
appPort: number;
3638
staticport: number;
3739
puppeteerLaunchOptions?: LaunchOptions;
40+
hostName?: string;
41+
hostUrl?: string;
3842
}
3943
```
4044

@@ -125,4 +129,12 @@ this option can override the puppeteerLaunchOptions with settings that match thi
125129
Word of warning, some settings might interfer with the way Scully is working, creating errornous results.
126130
Follow [this link](https://pptr.dev/#?product=Puppeteer&version=v2.0.0&show=api-puppeteerlaunchoptions) for more information
127131

132+
### hostName
133+
134+
use a different name as `localhost` for the local server. Needed if doe to environmental restrictions localhost isn't usable
135+
136+
### hostUrl
137+
138+
Connect to a other server. If your app has special demands to host it, you might need to use your own server. When that is needed you can provide this setting to let scully know where to look for your running app. Make sure the server is up and running, and hosting the correct application.
139+
128140
[Full Documentation ➡️](scully.md)

scully.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ exports.config = {
88
projectRoot: './projects/sampleBlog/src/app',
99
/** outDir is where the static distribution files end up */
1010
outDir: './dist/static',
11+
hostName: '0.0.0.0',
1112
routes: {
1213
'/demo/:id': {
1314
type: 'extra',

scully/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@scullyio/scully",
3-
"version": "0.0.52",
3+
"version": "0.0.55",
44
"description": "Scully CLI",
55
"repository": {
66
"type": "GIT",

scully/renderPlugins/puppeteerRenderPlugin.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ import {logError, yellow} from '../utils/log';
99
import {launchedBrowser} from './launchedBrowser';
1010

1111
export const puppeteerRender = async (route: HandledRoute): Promise<string> => {
12-
// const {version} = JSON.parse(readFileSync(join('../package.json')).toString()) || '0.0.0';
13-
const path = scullyConfig.hostUrl === null ?
14-
`http://localhost:${scullyConfig.appPort}${route.route}`
15-
: `${scullyConfig.hostUrl}${route.route}`;
12+
let version = '0.0.0';
13+
try {
14+
const {version: pkgVersion} = JSON.parse(readFileSync(join('../package.json')).toString()) || '0.0.0';
15+
version = pkgVersion;
16+
} catch {}
17+
const path = scullyConfig.hostUrl
18+
? `${scullyConfig.hostUrl}${route.route}`
19+
: `http://${scullyConfig.hostName}:${scullyConfig.appPort}${route.route}`;
1620
let pageHtml: string;
1721
let browser: Browser;
1822
let page: Page;
@@ -30,7 +34,7 @@ export const puppeteerRender = async (route: HandledRoute): Promise<string> => {
3034
resolve();
3135
});
3236

33-
// windowSet(page, 'scullyVersion', version);
37+
windowSet(page, 'scullyVersion', version);
3438

3539
/** Inject this into the running page, runs in browser*/
3640
await page.evaluateOnNewDocument(() => {

scully/scully.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import {readFileSync} from 'fs-extra';
33
import {join} from 'path';
44
import * as yargs from 'yargs';
55
import './pluginManagement/systemPlugins';
6-
import {routeContentRenderer} from './renderPlugins/routeContentRenderer';
76
import {startBackgroundServer} from './startBackgroundServer';
87
import {loadConfig} from './utils/config';
98
import {moveDistAngular} from './utils/fsAngular';
109
import {httpGetJson} from './utils/httpGetJson';
11-
import {RouteTypes} from './utils/interfacesandenums';
1210
import {isPortTaken} from './utils/isPortTaken';
1311
import {logError} from './utils/log';
1412
import {startScully} from './utils/startup';
@@ -22,26 +20,11 @@ let port;
2220
// tslint:disable-next-line:variable-name
2321
export let _options = {};
2422

25-
const {argv: options} = yargs
26-
.option('path', {
27-
alias: 'p',
28-
type: 'string',
29-
description: 'The path to generate',
30-
})
31-
.option('type', {
32-
alias: 't',
33-
type: 'string',
34-
description: 'The type to generate',
35-
})
36-
.option('port', {
37-
alias: 'p',
38-
type: 'number',
39-
description: 'The port to run on',
40-
})
41-
.option('folder', {
42-
type: 'string',
43-
description: 'home folder',
44-
});
23+
const {argv: options} = yargs.option('port', {
24+
alias: 'p',
25+
type: 'number',
26+
description: 'The port to run on',
27+
});
4528

4629
if (process.argv.includes('version')) {
4730
const {version} = JSON.parse(readFileSync(join(__dirname, './package.json')).toString());
@@ -50,23 +33,25 @@ if (process.argv.includes('version')) {
5033
}
5134

5235
(async () => {
36+
/** make sure not to do something before the config is ready */
37+
const scullyConfig = await loadConfig;
5338
if (process.argv.includes('killServer')) {
54-
await httpGetJson('http://localhost:1864/killMe', {suppressErrors: true});
39+
await httpGetJson(`http://${scullyConfig.hostName}:${scullyConfig.appPort}/killMe`, {
40+
suppressErrors: true,
41+
});
5542
process.exit(0);
5643
return;
5744
}
58-
/** make sure not to do something before the config is ready */
59-
const scullyConfig = await loadConfig;
6045
await isBuildThere(scullyConfig);
6146

6247
if (process.argv.includes('serve')) {
6348
await bootServe(scullyConfig);
6449
} else {
6550
const folder = join(scullyConfig.homeFolder, scullyConfig.distFolder);
66-
/** copy in current buildfile */
51+
/** copy in current build artifacts */
6752
await moveDistAngular(folder, scullyConfig.outDir, {removeStaticDist: true, reset: false});
6853

69-
/** server already up and running? */
54+
/** server ports already in use? */
7055
const isTaken = await isPortTaken(scullyConfig.staticport);
7156
if (!isTaken) {
7257
startBackgroundServer(scullyConfig);
@@ -89,7 +74,9 @@ if (process.argv.includes('version')) {
8974
} else {
9075
if (!isTaken) {
9176
// kill serve ports
92-
await httpGetJson('http://localhost:1864/killMe', {suppressErrors: true});
77+
await httpGetJson(`http://${scullyConfig.hostName}:${scullyConfig.appPort}/killMe`, {
78+
suppressErrors: true,
79+
});
9380
}
9481
/** done, stop the program */
9582
process.exit(0);

scully/utils/config.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,23 @@ const loadIt = async () => {
4242

4343
if (compiledConfig.hostUrl && compiledConfig.hostUrl.endsWith('/')) {
4444
compiledConfig.hostUrl = compiledConfig.hostUrl.substr(0, compiledConfig.hostUrl.length - 1);
45-
} else if (compiledConfig.hostUrl === undefined) {
46-
compiledConfig.hostUrl = null;
4745
}
4846
// TODO: update types in interfacesandenums to force correct types in here.
4947
// tslint:disable-next-line: no-unused-expression
5048
Object.assign(
5149
scullyConfig,
50+
/** the default config */
5251
{
5352
homeFolder: angularRoot,
5453
outDir: join(angularRoot, './dist/static/'),
5554
distFolder,
5655
appPort: /** 1864 */ 'herodevs'.split('').reduce((sum, token) => (sum += token.charCodeAt(0)), 1000),
57-
staticport: /** 1771 */ 'scully'.split('').reduce((sum, token) => (sum += token.charCodeAt(0)), 1000),
56+
staticport: /** 1668 */ 'scully'.split('').reduce((sum, token) => (sum += token.charCodeAt(0)), 1000),
57+
hostName: 'localhost',
5858
defaultPostRenderers: [],
59-
// tslint:disable-next-line:no-bitwise
60-
hostUrl: compiledConfig.hostUrl,
6159
}
62-
// compiledConfig
6360
) as ScullyConfig;
61+
/** activate loaded config */
6462
await updateScullyConfig(compiledConfig);
6563
return scullyConfig;
6664
};

scully/utils/fsAngular.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import {watch} from 'chokidar';
2-
import {existsSync} from 'fs';
32
import {copy, remove} from 'fs-extra';
43
import {join} from 'path';
54
import {Observable} from 'rxjs';
65
import {debounceTime, filter, tap} from 'rxjs/operators';
76
import {restartStaticServer, startScullyWatchMode} from '../watchMode';
8-
import {green, log, logWarn, red} from './log';
97
import {scullyConfig} from './config';
108
import {createFolderFor} from './createFolderFor';
9+
import {green, log, logWarn} from './log';
1110

1211
export async function checkChangeAngular(
1312
folder = join(scullyConfig.homeFolder, scullyConfig.distFolder) ||

scully/utils/httpGetJson.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ export function httpGetJson(
2424
const contentType = res.headers['content-type'];
2525
let error: Error;
2626
if (statusCode !== 200) {
27-
error = new Error('Request Failed.\n' + `Status Code: ${statusCode}`);
27+
error = new Error(`Request Failed. Received status code: ${statusCode}
28+
on url: ${url}`);
2829
} else if (!/^application\/json/.test(contentType)) {
29-
error = new Error(
30-
'Invalid content-type.\n' + `Expected application/json but received ${contentType}`
31-
);
30+
error = new Error(`Invalid content-type.
31+
Expected application/json but received ${contentType}
32+
on url: ${url}`);
3233
}
3334
if (error) {
3435
// console.error(error.message);

scully/utils/interfacesandenums.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,29 @@ export enum RouteTypes {
77
}
88

99
export interface ScullyConfig {
10+
/** the folder where the app-source is. Can be any off the projects in a repo */
1011
projectRoot: string;
12+
/** Array with string ID's of the content-renderes that will be run on all routes */
1113
defaultPostRenderers: string[];
14+
/** the root of the project (where angular.json lives) */
1215
homeFolder: string;
16+
/** the destination off the Scully generated files */
1317
outDir?: string;
18+
/** the place where distribution files of the project are. Should be a subfolder of dist. */
1419
distFolder?: string;
20+
/** routes that needs additional processing have their configuration in here */
1521
routes: RouteConfig;
22+
/** routes that are in the application but have no route in the router */
1623
extraRoutes?: string[];
24+
/** Port-number where the original application is served */
1725
appPort: number;
26+
/** port-number where the Scully generated files are available */
1827
staticport: number;
28+
/** optional launch-options for puppeteer */
1929
puppeteerLaunchOptions?: LaunchOptions;
30+
/** hostname to use for local server, defaults to `localhost` */
31+
hostName?: string;
32+
/** optional hostURL, if this is provided, we are going to use this server instead of the build-in one. */
2033
hostUrl?: string;
2134
}
2235

scully/utils/staticServer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export async function staticServer(port?: number) {
2929
scullyServer.use(express.static(scullyConfig.outDir, options));
3030
scullyServer.get('/', (req, res) => res.sendFile(join(distFolder, '/index.html')));
3131

32-
scullyServerInstance = scullyServer.listen(port, x => {
33-
log(`Scully static server started on "${yellow(`http://localhost:${port}/`)}" `);
32+
scullyServerInstance = scullyServer.listen(port, scullyConfig.hostName, x => {
33+
log(`Scully static server started on "${yellow(`http://${scullyConfig.hostName}:${port}/`)}" `);
3434
});
3535

3636
const angularDistServer = express();
@@ -56,8 +56,12 @@ export async function staticServer(port?: number) {
5656
* // angularDistServer.get('/*', (req, res) => res.sendFile(join(scullyConfig.outDir, '/index.html')));
5757
* we are already serving all known routes an index.html. at this point a 404 is indeed just a 404, don't substitute.
5858
*/
59-
angularServerInstance = angularDistServer.listen(scullyConfig.appPort, x => {
60-
log(`Angular distribution server started on "${yellow(`http://localhost:${scullyConfig.appPort}/`)}" `);
59+
angularServerInstance = angularDistServer.listen(scullyConfig.appPort, scullyConfig.hostName, x => {
60+
log(
61+
`Angular distribution server started on "${yellow(
62+
`http://${scullyConfig.hostName}:${scullyConfig.appPort}/`
63+
)}" `
64+
);
6165
});
6266
} catch (e) {
6367
logError(`Could not start Scully serve`, e);

0 commit comments

Comments
 (0)