Skip to content

Commit 9eac48d

Browse files
authored
Fix/watch mode (#59)
* watch(fix): fix manual restart * watch(fix): fix watchmode for angular build * watch(fix): fix angular build watch * feat(scully): get support for error message get support for error message when scully kill the app * fix(scully): rewrite watch mode change all from the watch mode for support the new config, and some hotfix fix #57 #40 * fix(fsfolder): fix how to read the routes config * fix(sccully): remove old code
1 parent 6f3b204 commit 9eac48d

4 files changed

Lines changed: 48 additions & 34 deletions

File tree

scully/scully.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {join} from 'path';
77
import * as yargs from 'yargs';
88
import './pluginManagement/systemPlugins';
99
import {routeContentRenderer} from './renderPlugins/routeContentRenderer';
10-
import {loadConfig} from './utils/config';
10+
import {angularRoot, loadConfig} from './utils/config';
1111
import {checkChangeAngular, existDistAngular, moveDistAngular} from './utils/fsAngular';
1212
import {httpGetJson} from './utils/httpGetJson';
1313
import {RouteTypes, ScullyConfig} from './utils/interfacesandenums';
@@ -17,6 +17,8 @@ import {startScully} from './utils/startup';
1717
import {closeExpress, staticServer} from './utils/staticServer';
1818
import {waitForServerToBeAvailable} from './utils/waitForServerToBeAvailable';
1919
import {checkStaticFolder} from './utils/fsFolder';
20+
import * as os from 'os';
21+
import {readFileSync, writeFileSync} from 'fs';
2022

2123
/** the default of 10 is too shallow for generating pages. */
2224
require('events').defaultMaxListeners = 100;
@@ -50,6 +52,7 @@ let _options = {};
5052
if (process.argv.includes('serve')) {
5153
port = options.path;
5254
console.log('starting static server...');
55+
process.title = 'ScullyServer';
5356
checkChangeAngular(options.path);
5457
restartStaticServer();
5558
} else {
@@ -73,7 +76,6 @@ let _options = {};
7376
} else {
7477
/** server already up and running? */
7578
const isTaken = await isPortTaken(scullyConfig.staticport);
76-
7779
if (!isTaken) {
7880
spawn('node', [join(scullyConfig.homeFolder, './node_modules/.bin/scully'), 'serve'], {
7981
detached: true,
@@ -125,37 +127,36 @@ let _options = {};
125127

126128
// TODO : we need rewrite this to observables for dont have memory leaks
127129
async function watchMode() {
128-
checkStaticFolder();
130+
await checkStaticFolder();
129131
// g for generate and the q for quit
130132
checkForManualRestart();
131133
// @ts-ignore
132-
checkChangeAngular(_options.path, false, true).then(() => {
133-
startScully();
134-
});
134+
await checkChangeAngular(_options.path, false, true);
135135
}
136136

137-
function checkForManualRestart() {
137+
export function checkForManualRestart() {
138138
const readline = require('readline').createInterface({
139139
input: process.stdin,
140140
output: process.stdout,
141141
});
142142

143-
readline.question(`Press g for manual regenerate, or q for close the server`, command => {
143+
readline.question(`Press g for manual regenerate, or q for close the server. \n`, command => {
144144
if (command.toLowerCase() === 'g') {
145-
startScully();
145+
startScully().then(
146+
() => checkForManualRestart()
147+
);
146148
} else if (command.toLowerCase() === 'q') {
147149
readline.close();
148150
process.exit(0);
151+
} else {
152+
readline.close();
153+
checkForManualRestart();
149154
}
150155
});
151156
}
152157

153158
export function startScullyWatchMode() {
154159
startScully();
155-
// @ts-ignore
156-
checkChangeAngular(_options.path, false, true).then(() => {
157-
startScully();
158-
});
159160
}
160161

161162
function startStaticServer() {

scully/utils/fsAngular.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import {copy, remove} from 'fs-extra';
44
import {join} from 'path';
55
import {Observable} from 'rxjs';
66
import {debounceTime, filter, tap} from 'rxjs/operators';
7-
import {restartStaticServer} from '../scully';
7+
import {restartStaticServer, startScullyWatchMode} from '../scully';
88
import {green, log, logWarn, red} from '../utils/log';
99
import {scullyConfig} from './config';
1010
import {createFolderFor} from './createFolderFor';
1111

12+
1213
export async function checkChangeAngular(
1314
folder = join(scullyConfig.homeFolder, scullyConfig.distFolder) ||
1415
join(scullyConfig.homeFolder, './dist/browser'),
1516
reset = true,
17+
// tslint:disable-next-line:no-shadowed-variable
1618
watch = false
1719
) {
1820
reWatch(folder, reset, watch);
@@ -31,7 +33,7 @@ function reWatch(folder, reset = true, watch = false) {
3133
/** give the CLI some time to finnish */
3234
debounceTime(1000),
3335
// tap(console.log),
34-
tap(() => moveDistAngular(folder, scullyConfig.outFolder, {reset}))
36+
tap(() => moveDistAngular(folder, scullyConfig.outFolder, {reset}, watch))
3537
// take(2)
3638
)
3739
.subscribe({
@@ -69,7 +71,7 @@ export function existDistAngular(src) {
6971
}
7072

7173
// tslint:disable-next-line:no-shadowed-variable
72-
export async function moveDistAngular(src, dest, {reset = true, removeStaticDist = false}) {
74+
export async function moveDistAngular(src, dest, {reset = true, removeStaticDist = false}, watch = false) {
7375
try {
7476
// delete files
7577
if (removeStaticDist) {
@@ -82,6 +84,8 @@ export async function moveDistAngular(src, dest, {reset = true, removeStaticDist
8284
log(`${green(` ☺ `)} new Angular build imported`);
8385
if (reset) {
8486
restartStaticServer();
87+
} else if (watch) {
88+
startScullyWatchMode();
8589
}
8690
} catch (e) {
8791
/**

scully/utils/fsFolder.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {existsSync, readFileSync} from 'fs';
22
import {join} from 'path';
33
import {Observable} from 'rxjs';
4-
import {throttleTime, filter} from 'rxjs/operators';
4+
import {throttleTime, filter, tap} from 'rxjs/operators';
55
import {log, red} from '../utils/log';
66
import {watch} from 'chokidar';
77
import {scullyConfig} from './config';
@@ -12,26 +12,30 @@ import {startScullyWatchMode} from '../scully';
1212
// tslint:disable-next-line:no-shadowed-variable
1313
export async function checkStaticFolder() {
1414

15-
// read scully.json for check plugin contentFolder
16-
const scullyJsonRAW = readFileSync(join(scullyConfig.homeFolder, 'scully.json')).toString();
17-
const scullyJson = jsonc.parse(scullyJsonRAW);
18-
const folder = [];
19-
// @ts-ignore
20-
for (const property in scullyJson.routes) {
21-
// @ts-ignore
22-
if (scullyJson.routes[property].type === 'contentFolder') {
23-
// @ts-ignore
24-
const fileName = scullyJson.routes[property].slug.folder.replace('./', '');
25-
if (!folder.find(f => f === fileName)) {
26-
folder.push(fileName);
27-
if (existFolder(fileName)) {
28-
reWatch(fileName);
29-
} else {
30-
log(`${red(`${fileName} folder not found`)}.`);
15+
try {
16+
const config = scullyConfig.routes; // require(join(scullyConfig.homeFolder, 'scully.config.js'));
17+
const folder = [];
18+
// tslint:disable-next-line:forin
19+
for (const property in config) {
20+
if (config[property].type === 'contentFolder') {
21+
// @ts-ignore
22+
const fileName = config[property].slug.folder.replace('./', '');
23+
console.log(fileName);
24+
if (!folder.find(f => f === fileName)) {
25+
folder.push(fileName);
26+
if (existFolder(fileName)) {
27+
reWatch(fileName);
28+
} else {
29+
log(`${red(`${fileName} folder not found`)}.`);
30+
}
3131
}
3232
}
3333
}
34+
} catch (e) {
35+
console.log('error into read the config', e);
3436
}
37+
38+
3539
}
3640

3741
function reWatch(folder) {
@@ -46,6 +50,9 @@ function reWatch(folder) {
4650
.subscribe({
4751
next: (v) => {
4852
if (v.eventType !== 'addDir') {
53+
console.log('--------------------------------------------------');
54+
console.log(`New ${v.eventType} in ${v.fileName}, re run scully.`);
55+
console.log('--------------------------------------------------');
4956
startScullyWatchMode();
5057
}
5158
}

scully/utils/staticServer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export async function staticServer(port?: number) {
4444
});
4545
angularDistServer.get('/killMe', (req, res) => {
4646
closeExpress();
47-
process.exit(0);
47+
try {
48+
process.exit(0);
49+
} catch (e) { }
4850
});
4951
angularDistServer.use(express.static(distFolder, options));
5052
// send the indexHTML on 404

0 commit comments

Comments
 (0)