Skip to content

Commit 7cd43e0

Browse files
committed
server.js: simplified code.
1 parent 7cc1805 commit 7cd43e0

1 file changed

Lines changed: 7 additions & 10 deletions

File tree

utils/server.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,20 @@ function createHandler( rootDirectory ) {
7272
// Show directory listing
7373
const files = readdirSync( filePath )
7474
.filter( f => ! f.startsWith( '.' ) )
75+
.map( f => ( { name: f, isDir: statSync( path.join( filePath, f ) ).isDirectory() } ) )
7576
.sort( ( a, b ) => {
7677

77-
const aIsDir = statSync( path.join( filePath, a ) ).isDirectory();
78-
const bIsDir = statSync( path.join( filePath, b ) ).isDirectory();
79-
if ( aIsDir && ! bIsDir ) return - 1;
80-
if ( ! aIsDir && bIsDir ) return 1;
81-
return a.localeCompare( b );
78+
if ( a.isDir && ! b.isDir ) return - 1;
79+
if ( ! a.isDir && b.isDir ) return 1;
80+
return a.name.localeCompare( b.name );
8281

8382
} );
8483

8584
const base = pathname.endsWith( '/' ) ? pathname : pathname + '/';
86-
const items = files.map( file => {
85+
const items = files.map( ( { name, isDir } ) => {
8786

88-
const fullPath = path.join( filePath, file );
89-
const isDir = statSync( fullPath ).isDirectory();
90-
const safeFile = escapeHtml( file );
91-
const safeHref = escapeHtml( base + file + ( isDir ? '/' : '' ) );
87+
const safeFile = escapeHtml( name );
88+
const safeHref = escapeHtml( base + name + ( isDir ? '/' : '' ) );
9289
const icon = isDir ? '📁' : '📄';
9390
return `<a href="${safeHref}"><span class="i">${icon}</span>${safeFile}</a>`;
9491

0 commit comments

Comments
 (0)