From fdd23a861d50f01fad37267a1a02abc488c3d791 Mon Sep 17 00:00:00 2001 From: sanderelias Date: Thu, 11 Jun 2020 09:24:19 +0200 Subject: [PATCH] refactor(scully): move the cached routeData to its proper place better cache, prep for better speed --- .../routerPlugins/addOptionalRoutesPlugin.ts | 6 ++- .../routerPlugins/traverseAppRoutesPlugin.ts | 21 ++++++---- .../src/lib/systemPlugins/storeRoutes.ts | 22 +++++++---- libs/scully/src/lib/utils/compileConfig.ts | 38 +++++++++---------- 4 files changed, 51 insertions(+), 36 deletions(-) diff --git a/libs/scully/src/lib/routerPlugins/addOptionalRoutesPlugin.ts b/libs/scully/src/lib/routerPlugins/addOptionalRoutesPlugin.ts index be1274ed1..039ac452d 100644 --- a/libs/scully/src/lib/routerPlugins/addOptionalRoutesPlugin.ts +++ b/libs/scully/src/lib/routerPlugins/addOptionalRoutesPlugin.ts @@ -16,7 +16,7 @@ export const addOptionalRoutes = async ( ? config.postRenderers : undefined; /** adding in the postrenderes. Note that the plugin might choose to overwrite the ones that come from the config */ - const r = (await routePluginHandler(cur)).map(row => + const r = (await routePluginHandler(cur)).map((row) => postRenderers ? { postRenderers, ...row, config } : { ...row, config } ); x.push(...r); @@ -68,6 +68,8 @@ export interface HandledRoute { postRenderers?: string[]; /** the path to the file for a content file */ templateFile?: string; + /** optional title, if data holds a title, that will be used instead */ + title?: string; /** * additional data that will end up in scully.routes.json * the frontMatter data will be added here too. @@ -93,7 +95,7 @@ async function routePluginHandler(route: string): Promise { route, conf ) as unknown)) as HandledRoute[]; - generatedRoutes.forEach(handledRoute => { + generatedRoutes.forEach((handledRoute) => { if (!handledRoute.route.startsWith('/')) { logWarn( `The plugin '${ diff --git a/libs/scully/src/lib/routerPlugins/traverseAppRoutesPlugin.ts b/libs/scully/src/lib/routerPlugins/traverseAppRoutesPlugin.ts index 0570dc3cb..68599dc70 100644 --- a/libs/scully/src/lib/routerPlugins/traverseAppRoutesPlugin.ts +++ b/libs/scully/src/lib/routerPlugins/traverseAppRoutesPlugin.ts @@ -5,19 +5,23 @@ import { scanRoutes, sge } from '../utils/cli-options'; import { scullyConfig } from '../utils/config'; import { existFolder } from '../utils/fsFolder'; import { green, log, logError, logWarn, yellow } from '../utils/log'; +import { createFolderFor } from '../utils/createFolderFor'; export const traverseAppRoutes = async ( - appRootFolder = scullyConfig.projectRoot + forceScan = scanRoutes ): Promise => { + const appRootFolder = scullyConfig.projectRoot; const routesPath = join( - __dirname, + scullyConfig.homeFolder, + 'node_modules/.cache/@scullyio', `${scullyConfig.projectName}.unhandledRoutes.json` ); const extraRoutes = await addExtraRoutes(); let routes = [] as string[]; if (!scullyConfig.bareProject) { - if (scanRoutes === false && existFolder(routesPath)) { + /** read from cache when exists and not forced to scan. */ + if (forceScan === false && existFolder(routesPath)) { try { const result = JSON.parse( readFileSync(routesPath).toString() @@ -54,10 +58,10 @@ Using stored unhandled routes!. )}". Using the apps source folder as source. This might lead to unpredictable results` ); routes = parseAngularRoutes(appRootFolder, excludedFiles).map( - r => r.path + (r) => r.path ); } else { - routes = parseAngularRoutes(file, excludedFiles).map(r => r.path); + routes = parseAngularRoutes(file, excludedFiles).map((r) => r.path); } } catch (e) { if (sge) { @@ -77,11 +81,12 @@ ${green( `); } // process.exit(15); - if (routes.findIndex(r => r.trim() === '' || r.trim() === '/') === -1) { + if (routes.findIndex((r) => r.trim() === '' || r.trim() === '/') === -1) { /** make sure the root Route is always rendered. */ routes.push('/'); } /** cache the scanned routes */ + createFolderFor(routesPath); writeFileSync(routesPath, JSON.stringify(routes)); } /** de-duplicate routes */ @@ -111,7 +116,7 @@ export async function addExtraRoutes(): Promise { } workList.push(...outerResult); } else if (Array.isArray(extraRoutes)) { - extraRoutes.forEach(r => { + extraRoutes.forEach((r) => { if (workList.includes(r)) { /** don't add duplicates */ return; @@ -137,7 +142,7 @@ export async function addExtraRoutes(): Promise { result.push(x); } if (Array.isArray(x)) { - x.forEach(s => { + x.forEach((s) => { if (typeof s === 'string') { result.push(s); } diff --git a/libs/scully/src/lib/systemPlugins/storeRoutes.ts b/libs/scully/src/lib/systemPlugins/storeRoutes.ts index e093b2473..9eb30b2b8 100644 --- a/libs/scully/src/lib/systemPlugins/storeRoutes.ts +++ b/libs/scully/src/lib/systemPlugins/storeRoutes.ts @@ -1,10 +1,10 @@ import { writeFileSync } from 'fs'; import { join } from 'path'; import { HandledRoute } from '../routerPlugins/addOptionalRoutesPlugin'; +import { watch } from '../utils/cli-options'; import { scullyConfig } from '../utils/config'; import { createFolderFor } from '../utils/createFolderFor'; -import { log, logError, yellow } from '../utils/log'; -import { watch } from '../utils/cli-options'; +import { log, logError, logWarn, yellow } from '../utils/log'; const routesFileName = '/assets/scully-routes.json'; @@ -13,7 +13,7 @@ export async function storeRoutes(routes: HandledRoute[]) { /** in the scully outfolder */ join(scullyConfig.outDir, routesFileName), /** in the angular dist folder */ - join(scullyConfig.homeFolder, scullyConfig.distFolder, routesFileName) + join(scullyConfig.homeFolder, scullyConfig.distFolder, routesFileName), ]; if (!watch) { /** in the angular source folder */ @@ -21,24 +21,32 @@ export async function storeRoutes(routes: HandledRoute[]) { /** in the angular source folder */ join(scullyConfig.homeFolder, scullyConfig.sourceRoot, routesFileName) ); + } else { + logWarn( + `running in watch-mode, routefile in source assets will not be updated` + ); } try { const jsonResult = JSON.stringify( - routes.map(r => ({ route: r.route || '/', title: r['title'], ...r.data })) + routes.map((r) => ({ + route: r.route || '/', + title: r.title, + ...r.data, + })) ); - const write = file => { + const write = (file) => { createFolderFor(file); writeFileSync(file, jsonResult); }; files.forEach(write); log(`Route list created in files:${files.map( - f => ` + (f) => ` "${yellow(f)}"` )} `); } catch { logError(`Could not write routes to files:${files.map( - f => ` + (f) => ` "${yellow(f)}"` )} `); diff --git a/libs/scully/src/lib/utils/compileConfig.ts b/libs/scully/src/lib/utils/compileConfig.ts index 452f9c21f..eef013c1c 100644 --- a/libs/scully/src/lib/utils/compileConfig.ts +++ b/libs/scully/src/lib/utils/compileConfig.ts @@ -1,22 +1,22 @@ +import { unlinkSync } from 'fs'; import { - pathExists, - statSync, existsSync, + pathExists, readFileSync, - writeFileSync + statSync, + writeFileSync, } from 'fs-extra'; import { join } from 'path'; +import { + flattenDiagnosticMessageText, + transpileModule, + TranspileOutput, +} from 'typescript'; import { configFileName, project } from './cli-options'; import { findAngularJsonPath } from './findAngularJsonPath'; import { ScullyConfig } from './interfacesandenums'; import { logError, logWarn, yellow } from './log'; import { readAngularJson } from './read-anguar-json'; -import { checkFolderExists } from './validateConfig'; -import { - transpileModule, - TranspileOutput, - flattenDiagnosticMessageText -} from 'typescript'; const angularRoot = findAngularJsonPath(); @@ -53,11 +53,13 @@ export const compileConfig = async (): Promise => { --------- `); return ({ - projectName: project || defaFaultProjectName + projectName: project || defaFaultProjectName, } as unknown) as ScullyConfig; } await compileTsIfNeeded(path); const { config } = await import(getJsName(path)); + /** dispose of the temporary JS file */ + unlinkSync(getJsName(path)); return { projectName: project || defaFaultProjectName, ...config }; } catch (e) { logError(` @@ -79,23 +81,21 @@ async function compileTsIfNeeded(path) { const source = readFileSync(path).toString('utf8'); const js: TranspileOutput = transpileModule(source, { fileName: path, - reportDiagnostics: true + reportDiagnostics: true, }); if (js.diagnostics.length > 0) { logError( - `----------------------------------------------------------------------------------------` - ); - logError( - ` Error${ - js.diagnostics.length === 1 ? '' : 's' - } while typescript compiling "${yellow(path)}"` + `---------------------------------------------------------------------------------------- + Error${ + js.diagnostics.length === 1 ? '' : 's' + } while typescript compiling "${yellow(path)}"` ); - js.diagnostics.forEach(diagnostic => { + js.diagnostics.forEach((diagnostic) => { if (diagnostic.file) { // tslint:disable-next-line: no-non-null-assertion const { line, - character + character, } = diagnostic.file.getLineAndCharacterOfPosition( diagnostic.start! );