Skip to content

Commit

Permalink
refactor: createDir() return created path
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Dec 27, 2018
1 parent 6ac4970 commit 6ee8cab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ function isDirFileExists(path: string, type: 'DIR' | 'FILE'): Promise<boolean> {
}


// create directories recursively
export async function createDir(path: string): Promise<void> {
/** create directories recursively */
export async function createDir(path: string): Promise<string> {
if (! path) {
throw new Error('value of path param invalid')
}
else {
path = normalize(path) // ! required for '.../.myca' under win32
const target = normalize(path) // ! required for '.../.myca' under win32
/* istanbul ignore else */
if (!await isDirExists(path)) {
await path.split(sep).reduce(
if (!await isDirExists(target)) {
await target.split(sep).reduce(
async (parentDir: Promise<string>, childDir: string) => {
const curDir = pathResolve(await parentDir, childDir)

Expand All @@ -94,6 +94,8 @@ export async function createDir(path: string): Promise<void> {
Promise.resolve(sep),
)
}

return target
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/10_shared_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ describe(filename, () => {
const randomPath = `${tmpDir}/${pathPrefix}-${random}`

try {
await createDir(randomPath)
const path = await createDir(randomPath)
assert(path === normalize(randomPath))
}
catch (ex) {
return assert(false, ex)
Expand All @@ -91,7 +92,8 @@ describe(filename, () => {
const randomPath = `${tmpDir}/${pathPrefix}-${random}/.test/0ab`

try {
await createDir(randomPath)
const path = await createDir(randomPath)
assert(path === normalize(randomPath))
}
catch (ex) {
return assert(false, ex)
Expand Down

0 comments on commit 6ee8cab

Please sign in to comment.