@@ -26,7 +26,7 @@ THE SOFTWARE.
26
26
27
27
---------------------------------------------------------------------------*/
28
28
29
- import { resolve , join } from 'path'
29
+ import { resolve , join , isAbsolute , normalize } from 'path'
30
30
import { readdirSync , statSync , readFileSync , existsSync } from 'fs'
31
31
import { render } from './render'
32
32
@@ -52,34 +52,45 @@ function flatMap<T>(array: T[][]): T[] {
52
52
return buffer
53
53
}
54
54
55
- function readFiles ( rootPath : string , directoryPath : string ) : FileEntry [ ] {
55
+ function fixWindowsPath ( path : string ) : string {
56
+ return path . replace ( new RegExp ( / \\ / g) , '/' )
57
+ }
58
+
59
+ /** Reads file entries, returning the absolute path + file data encoded in base64 */
60
+ function readFileEntries ( directoryPath : string ) : FileEntry [ ] {
56
61
if ( ! existsSync ( directoryPath ) || ! statSync ( directoryPath ) . isDirectory ( ) ) {
57
62
return [ ]
58
63
}
64
+
65
+ // reads inner contents, map to absolute if nessasary.
59
66
const contents = readdirSync ( directoryPath )
67
+ . map ( path => ! isAbsolute ( path ) ? join ( directoryPath , path ) : path )
68
+ . map ( path => fixWindowsPath ( path ) )
69
+
60
70
return [
61
71
...flatMap (
62
72
contents
63
- . map ( path => join ( directoryPath , path ) )
64
73
. filter ( path => statSync ( path ) . isDirectory ( ) )
65
- . map ( path => readFiles ( rootPath , path ) )
74
+ . map ( path => readFileEntries ( path ) )
66
75
) ,
67
76
...contents
68
- . map ( path => join ( directoryPath , path ) )
69
77
. filter ( path => statSync ( path ) . isFile ( ) )
70
78
. map ( path => {
71
79
const data = readFileSync ( path ) . toString ( 'base64' )
72
- const trim = path . replace ( rootPath + '/' , '' )
73
- return { path : trim , data }
80
+ return { path, data }
74
81
} )
75
82
]
76
83
}
77
84
78
85
export function packDirectory ( directoryPath : string ) : { [ path : string ] : string } {
79
- const absolutePath = resolve ( directoryPath )
80
- const entries = readFiles ( absolutePath , absolutePath )
81
- return entries . reduce ( ( acc : { [ path : string ] : string } , c ) => {
82
- acc [ c . path ] = c . data
86
+ const absoluteDirectoryPath = fixWindowsPath ( resolve ( directoryPath ) )
87
+ const entries = readFileEntries ( absoluteDirectoryPath )
88
+ return entries . reduce ( ( acc : { [ path : string ] : string } , entry ) => {
89
+ // note: because file entries are given in absolute form, we
90
+ // need to truncate the beginning of the path to produce
91
+ // the object key.
92
+ const key = entry . path . replace ( absoluteDirectoryPath + '/' , '' )
93
+ acc [ key ] = entry . data
83
94
return acc
84
95
} , { } )
85
96
}
0 commit comments