Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions scully/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions scully/scully.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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',
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 7 additions & 3 deletions scully/utils/fsAngular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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({
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
/**
Expand Down
41 changes: 24 additions & 17 deletions scully/utils/fsFolder.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) {
Expand All @@ -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();
}
}
Expand Down
4 changes: 3 additions & 1 deletion scully/utils/staticServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down