diff --git a/scully/package-lock.json b/scully/package-lock.json index bf2b8cea9..5fe55274c 100644 --- a/scully/package-lock.json +++ b/scully/package-lock.json @@ -920,6 +920,16 @@ "js-yaml": "^3.13.1" } }, + "fs-extra": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", + "integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==", + "requires": { + "graceful-fs": "^4.2.0", + "jsonfile": "^4.0.0", + "universalify": "^0.1.0" + } + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -1247,6 +1257,14 @@ "strip-json-comments": "^3.0.1" } }, + "jsonfile": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", + "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", + "requires": { + "graceful-fs": "^4.1.6" + } + }, "jsprim": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz", @@ -2069,6 +2087,11 @@ "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.2.tgz", "integrity": "sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==" }, + "universalify": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", + "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==" + }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/scully/scully.ts b/scully/scully.ts index 799a8d98c..91be2520c 100644 --- a/scully/scully.ts +++ b/scully/scully.ts @@ -7,7 +7,7 @@ import {join} from 'path'; import * as yargs from 'yargs'; import './pluginManagement/systemPlugins'; import {routeContentRenderer} from './renderPlugins/routeContentRenderer'; -import {loadConfig} from './utils/config'; +import {angularRoot, loadConfig} from './utils/config'; import {checkChangeAngular, existDistAngular, moveDistAngular} from './utils/fsAngular'; import {httpGetJson} from './utils/httpGetJson'; import {RouteTypes, ScullyConfig} from './utils/interfacesandenums'; @@ -17,6 +17,8 @@ import {startScully} from './utils/startup'; import {closeExpress, staticServer} from './utils/staticServer'; import {waitForServerToBeAvailable} from './utils/waitForServerToBeAvailable'; import {checkStaticFolder} from './utils/fsFolder'; +import * as os from 'os'; +import {readFileSync, writeFileSync} from 'fs'; /** the default of 10 is too shallow for generating pages. */ require('events').defaultMaxListeners = 100; @@ -50,6 +52,7 @@ let _options = {}; if (process.argv.includes('serve')) { port = options.path; console.log('starting static server...'); + process.title = 'ScullyServer'; checkChangeAngular(options.path); restartStaticServer(); } else { @@ -73,12 +76,10 @@ let _options = {}; } 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 => { - console.log(err); if (+err > 0) { spawn( 'node', @@ -126,37 +127,36 @@ let _options = {}; // TODO : we need rewrite this to observables for dont have memory leaks async function watchMode() { - checkStaticFolder(); + await checkStaticFolder(); // g for generate and the q for quit checkForManualRestart(); // @ts-ignore - checkChangeAngular(_options.path, false, true).then(() => { - startScully(); - }); + await checkChangeAngular(_options.path, false, true); } -function checkForManualRestart() { +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`, command => { + readline.question(`Press g for manual regenerate, or q for close the server. \n`, command => { if (command.toLowerCase() === 'g') { - startScully(); + startScully().then( + () => checkForManualRestart() + ); } else if (command.toLowerCase() === 'q') { readline.close(); process.exit(0); + } else { + readline.close(); + checkForManualRestart(); } }); } export function startScullyWatchMode() { startScully(); - // @ts-ignore - checkChangeAngular(_options.path, false, true).then(() => { - startScully(); - }); } function startStaticServer() { diff --git a/scully/utils/fsAngular.ts b/scully/utils/fsAngular.ts index 05b010167..a472cdc18 100644 --- a/scully/utils/fsAngular.ts +++ b/scully/utils/fsAngular.ts @@ -4,15 +4,17 @@ import {copy, remove} from 'fs-extra'; import {join} from 'path'; import {Observable} from 'rxjs'; import {debounceTime, filter, tap} from 'rxjs/operators'; -import {restartStaticServer} from '../scully'; +import {restartStaticServer, startScullyWatchMode} from '../scully'; import {green, log, logWarn, red} from '../utils/log'; import {scullyConfig} from './config'; import {createFolderFor} from './createFolderFor'; + export async function checkChangeAngular( folder = join(scullyConfig.homeFolder, scullyConfig.distFolder) || join(scullyConfig.homeFolder, './dist/browser'), reset = true, + // tslint:disable-next-line:no-shadowed-variable watch = false ) { reWatch(folder, reset, watch); @@ -31,7 +33,7 @@ function reWatch(folder, reset = true, watch = false) { /** give the CLI some time to finnish */ debounceTime(1000), // tap(console.log), - tap(() => moveDistAngular(folder, scullyConfig.outFolder, {reset})) + tap(() => moveDistAngular(folder, scullyConfig.outFolder, {reset}, watch)) // take(2) ) .subscribe({ @@ -69,7 +71,7 @@ export function existDistAngular(src) { } // tslint:disable-next-line:no-shadowed-variable -export async function moveDistAngular(src, dest, {reset = true, removeStaticDist = false}) { +export async function moveDistAngular(src, dest, {reset = true, removeStaticDist = false}, watch = false) { try { // delete files if (removeStaticDist) { @@ -82,6 +84,8 @@ export async function moveDistAngular(src, dest, {reset = true, removeStaticDist log(`${green(` ☺ `)} new Angular build imported`); if (reset) { restartStaticServer(); + } else if (watch) { + startScullyWatchMode(); } } catch (e) { /** diff --git a/scully/utils/fsFolder.ts b/scully/utils/fsFolder.ts index 21ce2ae2a..cceb635c9 100644 --- a/scully/utils/fsFolder.ts +++ b/scully/utils/fsFolder.ts @@ -1,7 +1,7 @@ import {existsSync, readFileSync} from 'fs'; import {join} from 'path'; import {Observable} from 'rxjs'; -import {throttleTime, filter} from 'rxjs/operators'; +import {throttleTime, filter, tap} from 'rxjs/operators'; import {log, red} from '../utils/log'; import {watch} from 'chokidar'; import {scullyConfig} from './config'; @@ -12,26 +12,30 @@ import {startScullyWatchMode} from '../scully'; // tslint:disable-next-line:no-shadowed-variable export async function checkStaticFolder() { - // read scully.json for check plugin contentFolder - const scullyJsonRAW = readFileSync(join(scullyConfig.homeFolder, 'scully.json')).toString(); - const scullyJson = jsonc.parse(scullyJsonRAW); - const folder = []; - // @ts-ignore - for (const property in scullyJson.routes) { - // @ts-ignore - if (scullyJson.routes[property].type === 'contentFolder') { - // @ts-ignore - const fileName = scullyJson.routes[property].slug.folder.replace('./', ''); - if (!folder.find(f => f === fileName)) { - folder.push(fileName); - if (existFolder(fileName)) { - reWatch(fileName); - } else { - log(`${red(`${fileName} folder not found`)}.`); + try { + const config = scullyConfig.routes; // require(join(scullyConfig.homeFolder, 'scully.config.js')); + const folder = []; + // tslint:disable-next-line:forin + for (const property in config) { + if (config[property].type === 'contentFolder') { + // @ts-ignore + const fileName = config[property].slug.folder.replace('./', ''); + console.log(fileName); + if (!folder.find(f => f === fileName)) { + folder.push(fileName); + if (existFolder(fileName)) { + reWatch(fileName); + } else { + log(`${red(`${fileName} folder not found`)}.`); + } } } } + } catch (e) { + console.log('error into read the config', e); } + + } function reWatch(folder) { @@ -46,6 +50,9 @@ function reWatch(folder) { .subscribe({ next: (v) => { if (v.eventType !== 'addDir') { + console.log('--------------------------------------------------'); + console.log(`New ${v.eventType} in ${v.fileName}, re run scully.`); + console.log('--------------------------------------------------'); startScullyWatchMode(); } } diff --git a/scully/utils/staticServer.ts b/scully/utils/staticServer.ts index 31d157e16..7b6f74ea2 100644 --- a/scully/utils/staticServer.ts +++ b/scully/utils/staticServer.ts @@ -44,7 +44,9 @@ export async function staticServer(port?: number) { }); angularDistServer.get('/killMe', (req, res) => { closeExpress(); - process.exit(0); + try { + process.exit(0); + } catch (e) { } }); angularDistServer.use(express.static(distFolder, options)); // send the indexHTML on 404