Skip to content

Commit 3e79678

Browse files
committed
better path resolution strategy
1 parent 13eb521 commit 3e79678

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/bundler/resources/templates/directory.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ THE SOFTWARE.
2626
2727
---------------------------------------------------------------------------*/
2828

29-
import { resolve, join } from 'path'
29+
import { resolve, join, isAbsolute, normalize } from 'path'
3030
import { readdirSync, statSync, readFileSync, existsSync } from 'fs'
3131
import { render } from './render'
3232

@@ -52,34 +52,45 @@ function flatMap<T>(array: T[][]): T[] {
5252
return buffer
5353
}
5454

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[] {
5661
if (!existsSync(directoryPath) || !statSync(directoryPath).isDirectory()) {
5762
return []
5863
}
64+
65+
// reads inner contents, map to absolute if nessasary.
5966
const contents = readdirSync(directoryPath)
67+
.map(path => !isAbsolute(path) ? join(directoryPath, path) : path)
68+
.map(path => fixWindowsPath(path))
69+
6070
return [
6171
...flatMap(
6272
contents
63-
.map(path => join(directoryPath, path))
6473
.filter(path => statSync(path).isDirectory())
65-
.map(path => readFiles(rootPath, path))
74+
.map(path => readFileEntries(path))
6675
),
6776
...contents
68-
.map(path => join(directoryPath, path))
6977
.filter(path => statSync(path).isFile())
7078
.map(path => {
7179
const data = readFileSync(path).toString('base64')
72-
const trim = path.replace(rootPath + '/', '')
73-
return { path: trim, data }
80+
return { path, data }
7481
})
7582
]
7683
}
7784

7885
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
8394
return acc
8495
}, {})
8596
}

tasks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export async function spec() {
6969

7070
export async function install_cli () {
7171
await pack()
72-
await shell('cd ./output/pack && npm install ./*.tgz -g')
72+
await shell('cd ./output/pack && npm install ./typescript-bundle-1.0.10.tgz -g')
7373
}
7474

7575
export async function watch() {

0 commit comments

Comments
 (0)