-
Notifications
You must be signed in to change notification settings - Fork 252
fix(scully): fix Could not connect to server #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,180 +1,98 @@ | ||
| #!/usr/bin/env node | ||
| // the above line is needed for node bin running | ||
|
|
||
| import {spawn} from 'child_process'; | ||
| import {existsSync} from 'fs-extra'; | ||
| import {readFileSync} from 'fs-extra'; | ||
| import {join} from 'path'; | ||
| import * as yargs from 'yargs'; | ||
| import './pluginManagement/systemPlugins'; | ||
| import {routeContentRenderer} from './renderPlugins/routeContentRenderer'; | ||
| import {startBackgroundServer} from './startBackgroundServer'; | ||
| import {loadConfig} from './utils/config'; | ||
| import {checkChangeAngular, existDistAngular, moveDistAngular} from './utils/fsAngular'; | ||
| import {checkStaticFolder} from './utils/fsFolder'; | ||
| import {moveDistAngular} from './utils/fsAngular'; | ||
| import {httpGetJson} from './utils/httpGetJson'; | ||
| import {RouteTypes, ScullyConfig} from './utils/interfacesandenums'; | ||
| import {RouteTypes} from './utils/interfacesandenums'; | ||
| import {isPortTaken} from './utils/isPortTaken'; | ||
| import {logError} from './utils/log'; | ||
| import {startScully} from './utils/startup'; | ||
| import {closeExpress, staticServer} from './utils/staticServer'; | ||
| 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; | ||
|
|
||
| let port; | ||
| // tslint:disable-next-line:variable-name | ||
| let _options = {}; | ||
| export let _options = {}; | ||
|
|
||
| const {argv: options} = yargs | ||
| .option('path', { | ||
| alias: 'p', | ||
| type: 'string', | ||
| description: 'The path to generate', | ||
| }) | ||
| .option('type', { | ||
| alias: 't', | ||
| type: 'string', | ||
| description: 'The type to generate', | ||
| }) | ||
| .option('port', { | ||
| alias: 'p', | ||
| type: 'number', | ||
| description: 'The port to run on', | ||
| }) | ||
| .option('folder', { | ||
| type: 'string', | ||
| description: 'home folder', | ||
| }); | ||
|
|
||
| if (process.argv.includes('version')) { | ||
| const {version} = JSON.parse(readFileSync(join(__dirname, './package.json')).toString()); | ||
| console.log('version:', version); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| (async () => { | ||
| if (process.argv.includes('killServer')) { | ||
| await httpGetJson('http://localhost:1864/killMe', {suppressErrors: true}); | ||
| process.exit(0); | ||
| return; | ||
| } | ||
| /** make sure not to do something before the config is ready */ | ||
| const scullyConfig = await loadConfig; | ||
| await isBuildThere(scullyConfig); | ||
|
|
||
| const {argv: options} = yargs | ||
| .option('path', { | ||
| alias: 'p', | ||
| type: 'string', | ||
| description: 'The path to generate', | ||
| }) | ||
| .option('type', { | ||
| alias: 't', | ||
| type: 'string', | ||
| description: 'The type to generate', | ||
| }) | ||
| .option('port', { | ||
| alias: 'p', | ||
| type: 'number', | ||
| description: 'The port to run on', | ||
| }); | ||
|
|
||
| if (process.argv.includes('serve')) { | ||
| port = options.path; | ||
| console.log('starting static server...'); | ||
| process.title = 'ScullyServer'; | ||
| checkChangeAngular(options.path); | ||
| restartStaticServer(); | ||
| await bootServe(scullyConfig); | ||
| } else { | ||
| const folder = join(scullyConfig.homeFolder, scullyConfig.distFolder); | ||
| /** copy in current buildfile */ | ||
| await moveDistAngular(folder, scullyConfig.outDir, {removeStaticDist: true, reset: false}); | ||
|
|
||
| /** server already up and running? */ | ||
| const isTaken = await isPortTaken(scullyConfig.staticport); | ||
| if (!isTaken) { | ||
| startBackgroundServer(scullyConfig); | ||
| } else { | ||
| // debug only | ||
| console.log(`Background servers already running.`); | ||
| } | ||
|
|
||
| if (!existDistAngular(scullyConfig.homeFolder)) { | ||
| if (!(await waitForServerToBeAvailable().catch(e => false))) { | ||
| logError('Could not connect to server'); | ||
| process.exit(15); | ||
| } | ||
|
|
||
| await moveDistAngular(folder, scullyConfig.outDir, {removeStaticDist: true, reset: false}); | ||
| console.log('servers available'); | ||
| await startScully(); | ||
|
|
||
| if (options.path && options.type) { | ||
| routeContentRenderer({ | ||
| route: options.path, | ||
| type: (options.type as unknown) as RouteTypes, | ||
| }); | ||
| if (process.argv.includes('watch')) { | ||
| _options = options; | ||
| watchMode(); | ||
| } | ||
| if (process.argv.includes('watch')) { | ||
| _options = options; | ||
| watchMode(); | ||
| } else { | ||
| /** server already up and running? */ | ||
| const isTaken = await isPortTaken(scullyConfig.staticport); | ||
| if (!isTaken) { | ||
| spawn('node', [join(scullyConfig.homeFolder, './node_modules/.bin/scully'), 'serve'], { | ||
| detached: true, | ||
| }).on('close', err => { | ||
| if (+err > 0) { | ||
| spawn( | ||
| 'node', | ||
| [join(scullyConfig.homeFolder, './node_modules/@scullyio/scully/scully.js'), 'serve'], | ||
| { | ||
| detached: true, | ||
| } | ||
| ).on('close', err2 => { | ||
| if (+err2 > 0) { | ||
| spawn('node', [join(scullyConfig.homeFolder, '/dist/scully/scully'), 'serve'], { | ||
| detached: true, | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| console.log('started servers in background'); | ||
| } else { | ||
| // debug only | ||
| console.log(`Background servers already running.`); | ||
| } | ||
|
|
||
| if (!(await waitForServerToBeAvailable().catch(e => false))) { | ||
| logError('Could not connect to server'); | ||
| process.exit(15); | ||
| // kill serve ports | ||
| await httpGetJson('http://localhost:1864/killMe', {suppressErrors: true}); | ||
| } | ||
|
|
||
| console.log('servers available'); | ||
| await startScully(); | ||
|
|
||
| if (process.argv.includes('watch')) { | ||
| _options = options; | ||
| watchMode(); | ||
| } else { | ||
| if (!isTaken) { | ||
| // kill serve ports | ||
| await httpGetJson('http://localhost:1864/killMe', {suppressErrors: true}); | ||
| } | ||
| /** done, stop the program */ | ||
| process.exit(0); | ||
| } | ||
| } | ||
| } | ||
| })(); | ||
|
|
||
| // TODO : we need rewrite this to observables for don't have memory leaks | ||
| async function watchMode() { | ||
| await checkStaticFolder(); | ||
| // g for generate and the q for quit | ||
| checkForManualRestart(); | ||
| // @ts-ignore | ||
| await checkChangeAngular(_options.path, false, true); | ||
| } | ||
|
|
||
| export function checkForManualRestart() { | ||
| const readline = require('readline').createInterface({ | ||
| input: process.stdin, | ||
| output: process.stdout, | ||
| }); | ||
|
|
||
| readline.question(`Press g for manual regenerate, or q for close the server. \n`, command => { | ||
| if (command.toLowerCase() === 'g') { | ||
| startScully().then(() => checkForManualRestart()); | ||
| } else if (command.toLowerCase() === 'q') { | ||
| readline.close(); | ||
| /** done, stop the program */ | ||
| process.exit(0); | ||
| } else { | ||
| readline.close(); | ||
| checkForManualRestart(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| export function startScullyWatchMode() { | ||
| startScully(); | ||
| } | ||
|
|
||
| function startStaticServer() { | ||
| staticServer(); | ||
| } | ||
|
|
||
| let restartTimer: NodeJS.Timer; | ||
| export function restartStaticServer() { | ||
| // tslint:disable-next-line: no-unused-expression | ||
| restartTimer && clearTimeout(restartTimer); | ||
| restartTimer = setTimeout(() => { | ||
| closeExpress(); | ||
| startStaticServer(); | ||
| }, 500); | ||
| } | ||
|
|
||
| export async function isBuildThere(config: ScullyConfig) { | ||
| const dist = join(config.homeFolder, config.distFolder); | ||
| if (existsSync(dist) && existsSync(join(dist, 'index.html'))) { | ||
| return true; | ||
| } | ||
| logError(`Angular distribution files not found, run "ng build" first`); | ||
| process.exit(15); | ||
| } | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import {spawn} from 'child_process'; | ||
| import {existsSync} from 'fs-extra'; | ||
| import {join} from 'path'; | ||
| import {ScullyConfig} from './utils/interfacesandenums'; | ||
| import {logError, log, green} from './utils/log'; | ||
|
|
||
| export function startBackgroundServer(scullyConfig: ScullyConfig) { | ||
| const binary = ['/dist/scully/scully', '/node_modules/.bin/scully', '/node_modules/@scullyio/scully/scully'] | ||
| .map(p => join(scullyConfig.homeFolder, p + '.js')) | ||
| .find(p => existsSync(p)); | ||
|
|
||
| if (!binary) { | ||
| logError('Could not find scully binaries'); | ||
| process.exit(15); | ||
| return; | ||
| } | ||
| spawn('node', [binary, 'serve'], { | ||
| detached: true, | ||
| // stdio: 'inherit', | ||
| }).on('close', err => { | ||
| if (+err > 0) { | ||
| logError('Problem starting background servers', err); | ||
| process.exit(15); | ||
| } | ||
| }); | ||
| log(` ${green('☺')} Started servers in background`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.