Skip to content

Commit 4dd239a

Browse files
authored
Config load with require (#125)
* improvement(require in scully cofig): better support for node_modules in scully config * improvement(compileconfig.ts): add more complete nodeJS evrionment to config's scope * improvement(scully.config): show how to "self-register" * improvement(compileconfig): drop VM, use import instead This removes the protective VM context, to make creating plugins a bit simpler closes #122 re #107
1 parent 8410508 commit 4dd239a

File tree

10 files changed

+391
-331
lines changed

10 files changed

+391
-331
lines changed

extraPlugin/extra-plugin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const {configValidator, routeSplit} = require('../scully/bin');
1+
const {configValidator, routeSplit, registerPlugin} = require('../scully/bin');
22

33
console.log(__dirname);
44

55
const extraRoutesPlugin = (route, options) => {
66
const {createPath} = routeSplit(route);
77
if (options.numberOfPages) {
88
return Array.from({length: options.numberOfPages}, (_v, k) => k).map(n => ({
9-
route: `${createPath(`${n}`)}`,
9+
route: createPath(n.toString()),
1010
title: `page number ${n}`,
1111
}));
1212
}
@@ -44,5 +44,5 @@ extraRoutesPlugin[configValidator] = async options => {
4444
return errors;
4545
};
4646

47-
exports.extraRoutesPlugin = extraRoutesPlugin;
48-
/** the validator is mandatory */
47+
registerPlugin('router', 'extra', extraRoutesPlugin);
48+
module.exports.extraRoutesPlugin = extraRoutesPlugin;

package-lock.json

Lines changed: 346 additions & 248 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"zone.js": "~0.10.2"
4444
},
4545
"devDependencies": {
46-
"@angular-devkit/build-angular": "~0.900.0-rc.5",
46+
"@angular-devkit/build-angular": "^0.900.0-rc.7",
4747
"@angular-devkit/build-ng-packagr": "~0.900.0-rc.5",
4848
"@angular/cli": "~9.0.0-rc.5",
4949
"@angular/compiler-cli": "~9.0.0-rc.6",

projects/sampleBlog/src/app/app-routing.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const routes: Routes = [
2020
loadChildren: () => import('./user/user.module').then(m => m.UserModule),
2121
},
2222
{path: 'demo', loadChildren: () => import('./demo/demo.module').then(m => m.DemoModule)},
23-
{path: '', redirectTo: '/home', pathMatch: 'full'}
23+
// {path: '', redirectTo: '/home/', pathMatch: 'full'}
2424
];
2525

2626
@NgModule({

scully.config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** load the plugin */
2-
const {extraRoutesPlugin} = require('./extraPlugin/extra-plugin.js');
3-
/** register the plugin */
4-
registerPlugin('router', 'extra', extraRoutesPlugin);
2+
require('./extraPlugin/extra-plugin.js')
3+
54

65
exports.config = {
76
/** projectRoot is mandatory! */

scully/package-lock.json

Lines changed: 16 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.45",
3+
"version": "0.0.49",
44
"description": "Scully CLI",
55
"repository": {
66
"type": "GIT",

scully/renderPlugins/launchedBrowser.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import {Browser, launch, LaunchOptions} from 'puppeteer';
22
import {Observable} from 'rxjs';
33
import {shareReplay, take} from 'rxjs/operators';
4+
import * as yargs from 'yargs';
5+
6+
const {showBrowser} = yargs
7+
.boolean('sb')
8+
.alias('sb', 'showBrowser')
9+
.describe('sb', 'Shows the puppeteer controlled browser').argv;
410

511
/** use shareReplay so the browser will stay in memory during the lifetime of the program */
612
const launched = obsBrowser().pipe(shareReplay({refCount: false, bufferSize: 1}));
@@ -9,7 +15,7 @@ let browser: Browser;
915

1016
function obsBrowser(
1117
options: LaunchOptions = {
12-
headless: true,
18+
headless: !showBrowser,
1319
// dumpio: true,
1420
args: ['--no-sandbox', '--disable-setuid-sandbox'],
1521
}

scully/utils/compileConfig.ts

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,24 @@
1-
import {readFileSync} from 'fs';
21
import {pathExists} from 'fs-extra';
32
import {join} from 'path';
4-
// import {create, CreateOptions} from 'ts-node';
5-
import {createContext, runInContext} from 'vm';
3+
import * as yargs from 'yargs';
64
import {ScullyConfig} from '..';
7-
import {configValidator, registerPlugin} from '../pluginManagement/pluginRepository';
85
import {angularRoot, scullyConfig} from './config';
9-
import {routeSplit} from './routeSplit';
6+
7+
export const {configFile: configFileName} = yargs
8+
.string('cf')
9+
.alias('cf', 'configFile')
10+
.default('cf', 'scully.config.js')
11+
.describe('cf', 'provide name for config file').argv;
1012

1113
export const compileConfig = async (): Promise<ScullyConfig> => {
1214
try {
13-
const filename = 'scully.config.js';
14-
const path = join(angularRoot, filename);
15+
const path = join(angularRoot, configFileName);
1516
if (!(await pathExists(path))) {
1617
/** no ts config, nothing to do. */
1718
return ({} as unknown) as ScullyConfig;
1819
}
19-
const runtimeEnvironment = createContext({
20-
exports: {},
21-
console,
22-
registerPlugin,
23-
configValidator,
24-
routeSplit,
25-
global,
26-
require: (requirePath: string) => (requirePath.startsWith('@') ? require(requirePath) : require(join(angularRoot, requirePath))),
27-
});
28-
// const tsCompilerConfig: CreateOptions = {
29-
// logError: true,
30-
// compilerOptions: {
31-
// sourceMap: true,
32-
// target: 'es2018',
33-
// module: 'commonjs',
34-
// lib: ['dom', 'es2018'],
35-
// types: ['../types.d.ts']
36-
// // typeRoots: ['node', '/scully/bin/utils/routeSplit.d.ts'],
37-
// },
38-
// };
39-
// const ts = create(tsCompilerConfig);
40-
const tsCode = readFileSync(path).toString();
41-
// const jsCode = ts.compile(tsCode, filename, 0);
42-
const jsCode = tsCode;
43-
runInContext(jsCode, runtimeEnvironment, {filename, displayErrors: true});
44-
return runtimeEnvironment.exports.config;
20+
const {config} = await import(path);
21+
return config;
4522
} catch (e) {
4623
console.error(e);
4724
return ({} as unknown) as ScullyConfig;

scully/utils/validateConfig.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {configValidator, plugins} from '../pluginManagement/pluginRepository';
44
import {angularRoot} from './config';
55
import {ScullyConfig} from './interfacesandenums';
66
import {logError, logWarn, yellow} from './log';
7+
import {configFileName} from './compileConfig';
78

89
// TODO: make sure all route options are validated.
910
let hasErrors = false;
@@ -24,7 +25,7 @@ export async function validateConfig(config: ScullyConfig) {
2425
result.projectRoot = config.projectRoot;
2526
} else {
2627
// TODO define a constant for the config file name string
27-
error(`projectRoot missing in "${yellow('scully.config.js')}"`);
28+
error(`projectRoot missing in "${yellow(configFileName)}"`);
2829
}
2930
if (config.routes) {
3031
await Promise.all(
@@ -51,7 +52,7 @@ export async function validateConfig(config: ScullyConfig) {
5152
})
5253
);
5354
} else {
54-
logWarn('No routes defined in "scully.config.js"');
55+
logWarn(`No routes defined in "${yellow(configFileName)}"`);
5556
}
5657
if (hasErrors) {
5758
/** stop everything if there are errors in the config. */

0 commit comments

Comments
 (0)