1- import { readdir } from 'fs' ;
2- import { basename , extname , join } from 'path' ;
1+ import { readdir , lstatSync , readFileSync } from 'fs' ;
2+ import { basename , extname , join , sep } from 'path' ;
33import { registerPlugin } from '../pluginManagement/pluginRepository' ;
44import { readFileAndCheckPrePublishSlug } from '../renderPlugins/content-render-utils/readFileAndCheckPrePublishSlug' ;
55import { scullyConfig } from '../utils/config' ;
66import { RouteTypeContentFolder } from '../utils/interfacesandenums' ;
7- import { log , yellow } from '../utils/log' ;
7+ import { log , logWarn , yellow } from '../utils/log' ;
88import { HandledRoute } from './addOptionalRoutesPlugin' ;
99
1010export async function contentFolderPlugin (
@@ -22,32 +22,68 @@ export async function contentFolderPlugin(
2222 const baseRoute = angularRoute . split ( ':' + param ) [ 0 ] ;
2323 const path = join ( scullyConfig . homeFolder , paramConfig . folder ) ;
2424 log ( `Finding files in folder "${ yellow ( path ) } "` ) ;
25+ return await checkSourceIsDirectoryAndRun ( path , baseRoute , conf ) ;
26+ }
27+
28+ async function checkSourceIsDirectoryAndRun ( path , baseRoute , conf ) {
2529 const files = await new Promise < string [ ] > ( resolve => readdir ( path , ( err , data ) => resolve ( data ) ) ) ;
2630 const handledRoutes : HandledRoute [ ] = [ ] ;
2731 for ( const sourceFile of files ) {
2832 const ext = extname ( sourceFile ) ;
2933 const templateFile = join ( path , sourceFile ) ;
30- const base = basename ( sourceFile , ext ) ;
31- const routify = frag => `${ baseRoute } ${ slugify ( frag ) } ` ;
32- const { meta, prePublished} = await readFileAndCheckPrePublishSlug ( templateFile ) ;
33- const handledRoute : HandledRoute = {
34- route : routify ( meta . slug || base ) ,
35- type : conf . type ,
36- templateFile,
37- data : { ...meta , sourceFile} ,
38- } ;
39- handledRoutes . push ( handledRoute ) ;
40- if ( ! prePublished && Array . isArray ( meta . slugs ) ) {
41- /** also add routes for all available slugs */
42- meta . slugs
43- . filter ( slug => typeof slug === 'string' )
44- . map ( routify )
45- . forEach ( route => handledRoutes . push ( { ...handledRoute , route} ) ) ;
34+ if ( lstatSync ( templateFile ) . isDirectory ( ) ) {
35+ handledRoutes . push ( ...( await checkSourceIsDirectoryAndRun ( templateFile , baseRoute , conf ) ) ) ;
36+ } else {
37+ if ( checkIfEmpty ( templateFile ) ) {
38+ logWarn ( `The file ${ templateFile } is empty, scully will ignore.` ) ;
39+ } else {
40+ handledRoutes . push ( ...( await addHandleRoutes ( sourceFile , baseRoute , templateFile , conf , ext ) ) ) ;
41+ }
4642 }
4743 }
4844 return handledRoutes ;
4945}
5046
47+ function checkIfEmpty ( templateFile : string ) {
48+ try {
49+ const file = readFileSync ( templateFile ) . toString ( ) ;
50+ return file . length === 0 ? true : false ;
51+ } catch ( e ) {
52+ return false ;
53+ }
54+ }
55+
56+ async function addHandleRoutes ( sourceFile , baseRoute , templateFile , conf , ext ) {
57+ const handledRoutes = [ ] ;
58+ const base = basename ( sourceFile , ext ) ;
59+ // if a subfolder we need add a route for this folder
60+ let routify = frag => `${ baseRoute } ${ slugify ( frag ) } ` ;
61+ // replace \ for / for windows
62+ const newTemplateFile = templateFile . split ( '\\' ) . join ( '/' ) ;
63+ if ( ! newTemplateFile . endsWith ( `${ baseRoute } ${ sourceFile } ` ) ) {
64+ const position = newTemplateFile . search ( baseRoute ) ;
65+ const br = newTemplateFile . substr ( position ) . replace ( sourceFile , '' ) ;
66+ routify = frag => `${ br } ${ slugify ( frag ) } ` ;
67+ }
68+ // is a folder we need iterate the content in the folder
69+ const { meta, prePublished} = await readFileAndCheckPrePublishSlug ( templateFile ) ;
70+ const handledRoute : HandledRoute = {
71+ route : routify ( meta . slug || base ) ,
72+ type : conf . type ,
73+ templateFile,
74+ data : { ...meta , sourceFile} ,
75+ } ;
76+ handledRoutes . push ( handledRoute ) ;
77+ if ( ! prePublished && Array . isArray ( meta . slugs ) ) {
78+ /** also add routes for all available slugs */
79+ meta . slugs
80+ . filter ( slug => typeof slug === 'string' )
81+ . map ( routify )
82+ . forEach ( route => handledRoutes . push ( { ...handledRoute , route} ) ) ;
83+ }
84+ return handledRoutes ;
85+ }
86+
5187export function slugify ( frag : string ) : string {
5288 return encodeURIComponent (
5389 frag
0 commit comments