Skip to content

Commit a9dcaf4

Browse files
authored
feat(scully): make contentRenderPlugin skip unknown file extensions (#620)
* feat(scully): make contentRenderPlugin skip unknown file extensions * fix(scully): better handling of `.` on hasContentPlugin
1 parent bcdbac2 commit a9dcaf4

2 files changed

Lines changed: 45 additions & 20 deletions

File tree

libs/scully/src/lib/routerPlugins/contentFolderPlugin.ts

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { readdir, lstatSync, readFileSync } from 'fs';
2-
import { basename, extname, join, sep } from 'path';
3-
import { registerPlugin } from '../pluginManagement/pluginRepository';
1+
import { lstatSync, readdir, readFileSync } from 'fs';
2+
import { basename, extname, join } from 'path';
3+
import {
4+
AlternateExtensionsForFilePlugin,
5+
FilePlugin,
6+
plugins,
7+
registerPlugin,
8+
} from '../pluginManagement/pluginRepository';
49
import { readFileAndCheckPrePublishSlug } from '../renderPlugins/content-render-utils/readFileAndCheckPrePublishSlug';
510
import { scullyConfig } from '../utils/config';
611
import { RouteTypeContentFolder } from '../utils/interfacesandenums';
712
import { log, logWarn, yellow } from '../utils/log';
813
import { HandledRoute } from './addOptionalRoutesPlugin';
14+
import { Extension } from 'typescript';
915

1016
let basePath: string;
1117

@@ -15,7 +21,9 @@ export async function contentFolderPlugin(
1521
): Promise<HandledRoute[]> {
1622
const parts = angularRoute.split('/');
1723
/** for now, just handle the First parameter. Not sure if/how we can handle multiple ones. */
18-
const param = parts.filter(p => p.startsWith(':')).map(id => id.slice(1))[0];
24+
const param = parts
25+
.filter((p) => p.startsWith(':'))
26+
.map((id) => id.slice(1))[0];
1927
const paramConfig = conf[param];
2028
if (!paramConfig) {
2129
console.error(
@@ -30,20 +38,30 @@ export async function contentFolderPlugin(
3038
}
3139

3240
async function checkSourceIsDirectoryAndRun(path, baseRoute, conf) {
33-
const files = await new Promise<string[]>(resolve =>
41+
const files = await new Promise<string[]>((resolve) =>
3442
readdir(path, (err, data) => resolve(data))
3543
);
3644
const handledRoutes: HandledRoute[] = [];
3745
for (const sourceFile of files) {
3846
const ext = extname(sourceFile);
47+
// const ext = sourceFile.split('.').pop();
3948
const templateFile = join(path, sourceFile);
49+
4050
if (lstatSync(templateFile).isDirectory()) {
4151
handledRoutes.push(
4252
...(await checkSourceIsDirectoryAndRun(templateFile, baseRoute, conf))
4353
);
4454
} else {
4555
if (checkIfEmpty(templateFile)) {
46-
logWarn(`The file ${templateFile} is empty, scully will ignore.`);
56+
logWarn(
57+
`The file ${yellow(templateFile)} is empty, scully will ignore.`
58+
);
59+
} else if (!hasContentPlugin(ext)) {
60+
logWarn(
61+
`The file ${yellow(templateFile)} has extension ${yellow(
62+
ext
63+
)} that has no plugin defined, scully will skip this file.`
64+
);
4765
} else {
4866
handledRoutes.push(
4967
...(await addHandleRoutes(
@@ -60,6 +78,20 @@ async function checkSourceIsDirectoryAndRun(path, baseRoute, conf) {
6078
return handledRoutes;
6179
}
6280

81+
function hasContentPlugin(extension: string) {
82+
const availAblePlugins = plugins.fileHandler;
83+
extension = extension.toLowerCase().trim();
84+
extension = extension.startsWith('.') ? extension.slice(1) : extension;
85+
return (
86+
Object.entries(availAblePlugins).find(
87+
([name, plugin]: [string, FilePlugin]) =>
88+
extension === name.toLowerCase() ||
89+
(Array.isArray(plugin[AlternateExtensionsForFilePlugin]) &&
90+
plugin[AlternateExtensionsForFilePlugin].includes(extension))
91+
) !== undefined
92+
);
93+
}
94+
6395
function checkIfEmpty(templateFile: string) {
6496
try {
6597
const file = readFileSync(templateFile).toString();
@@ -73,15 +105,15 @@ async function addHandleRoutes(sourceFile, baseRoute, templateFile, conf, ext) {
73105
const handledRoutes = [];
74106
const base = basename(sourceFile, ext);
75107
// if a subfolder we need add a route for this folder
76-
let routify = frag => `${baseRoute}${slugify(frag)}`;
108+
let routify = (frag) => `${baseRoute}${slugify(frag)}`;
77109
// replace \ for / for windows
78110
const newTemplateFile = templateFile.split('\\').join('/');
79111
if (!newTemplateFile.endsWith(`${basePath}/${sourceFile}`)) {
80112
/** get the 'path' part of as a route partial */
81113
const routePartial = newTemplateFile
82114
.substr(basePath.length + 1)
83115
.replace(sourceFile, '');
84-
routify = frag => `${baseRoute}${routePartial}${slugify(frag)}`;
116+
routify = (frag) => `${baseRoute}${routePartial}${slugify(frag)}`;
85117
}
86118
const { meta, prePublished } = await readFileAndCheckPrePublishSlug(
87119
templateFile
@@ -91,34 +123,27 @@ async function addHandleRoutes(sourceFile, baseRoute, templateFile, conf, ext) {
91123
route: routify(meta.slug || base),
92124
type: conf.type,
93125
templateFile,
94-
data: { name, ...meta, sourceFile }
126+
data: { name, ...meta, sourceFile },
95127
};
96128
handledRoutes.push(handledRoute);
97129
if (!prePublished && Array.isArray(meta.slugs)) {
98130
/** also add routes for all available slugs */
99131
meta.slugs
100-
.filter(slug => typeof slug === 'string')
132+
.filter((slug) => typeof slug === 'string')
101133
.map(routify)
102-
.forEach(route => handledRoutes.push({ ...handledRoute, route }));
134+
.forEach((route) => handledRoutes.push({ ...handledRoute, route }));
103135
}
104136
return handledRoutes;
105137
}
106138

107139
export function slugify(frag: string): string {
108140
return encodeURIComponent(
109-
frag
110-
.trim()
111-
.split('/')
112-
.join('_')
113-
.split(' ')
114-
.join('_')
115-
.split('?')
116-
.join('_')
141+
frag.trim().split('/').join('_').split(' ').join('_').split('?').join('_')
117142
);
118143
}
119144

120145
// TODO actual validation of the config
121-
const configValidator = async conf => {
146+
const configValidator = async (conf) => {
122147
// return [yellow('all seems ok')];
123148
return [];
124149
};
12.6 KB
Loading

0 commit comments

Comments
 (0)