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
6 changes: 4 additions & 2 deletions libs/scully/src/lib/routerPlugins/addOptionalRoutesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -93,7 +95,7 @@ async function routePluginHandler(route: string): Promise<HandledRoute[]> {
route,
conf
) as unknown)) as HandledRoute[];
generatedRoutes.forEach(handledRoute => {
generatedRoutes.forEach((handledRoute) => {
if (!handledRoute.route.startsWith('/')) {
logWarn(
`The plugin '${
Expand Down
21 changes: 13 additions & 8 deletions libs/scully/src/lib/routerPlugins/traverseAppRoutesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> => {
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()
Expand Down Expand Up @@ -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) {
Expand All @@ -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 */
Expand Down Expand Up @@ -111,7 +116,7 @@ export async function addExtraRoutes(): Promise<string[]> {
}
workList.push(...outerResult);
} else if (Array.isArray(extraRoutes)) {
extraRoutes.forEach(r => {
extraRoutes.forEach((r) => {
if (workList.includes(r)) {
/** don't add duplicates */
return;
Expand All @@ -137,7 +142,7 @@ export async function addExtraRoutes(): Promise<string[]> {
result.push(x);
}
if (Array.isArray(x)) {
x.forEach(s => {
x.forEach((s) => {
if (typeof s === 'string') {
result.push(s);
}
Expand Down
22 changes: 15 additions & 7 deletions libs/scully/src/lib/systemPlugins/storeRoutes.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -13,32 +13,40 @@ 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 */
files.push(
/** 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)}"`
)}
`);
Expand Down
38 changes: 19 additions & 19 deletions libs/scully/src/lib/utils/compileConfig.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -53,11 +53,13 @@ export const compileConfig = async (): Promise<ScullyConfig> => {
---------
`);
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(`
Expand All @@ -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!
);
Expand Down