Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify mkdirP implementation #444

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions packages/io/__tests__/io.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {promises as fs} from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as io from '../src/io'
import * as ioUtil from '../src/io-util'

describe('cp', () => {
it('copies file with no flags', async () => {
Expand Down Expand Up @@ -813,31 +812,6 @@ describe('mkdirP', () => {
(await fs.lstat(path.join(realDirPath, 'sub_dir'))).isDirectory()
).toBe(true)
})

it('breaks if mkdirP loop out of control', async () => {
const testPath = path.join(
getTestTemp(),
'mkdirP_failsafe',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10'
)

expect.assertions(1)

try {
await ioUtil.mkdirP(testPath, 10)
} catch (err) {
expect(err.code).toBe('ENOENT')
}
})
})

describe('which', () => {
Expand Down
47 changes: 0 additions & 47 deletions packages/io/src/io-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {ok} from 'assert'
import * as fs from 'fs'
import * as path from 'path'

Expand Down Expand Up @@ -59,52 +58,6 @@ export function isRooted(p: string): boolean {
return p.startsWith('/')
}

/**
* Recursively create a directory at `fsPath`.
*
* This implementation is optimistic, meaning it attempts to create the full
* path first, and backs up the path stack from there.
*
* @param fsPath The path to create
* @param maxDepth The maximum recursion depth
* @param depth The current recursion depth
*/
export async function mkdirP(
fsPath: string,
maxDepth: number = 1000,
depth: number = 1
): Promise<void> {
ok(fsPath, 'a path argument must be provided')

fsPath = path.resolve(fsPath)

if (depth >= maxDepth) return mkdir(fsPath)

try {
await mkdir(fsPath)
return
} catch (err) {
switch (err.code) {
case 'ENOENT': {
await mkdirP(path.dirname(fsPath), maxDepth, depth + 1)
await mkdir(fsPath)
return
}
default: {
let stats: fs.Stats

try {
stats = await stat(fsPath)
} catch (err2) {
throw err
}

if (!stats.isDirectory()) throw err
}
}
}
}

/**
* Best effort attempt to determine whether a file exists and is executable.
* @param filePath file path to check
Expand Down
4 changes: 3 additions & 1 deletion packages/io/src/io.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {ok} from 'assert'
import * as childProcess from 'child_process'
import * as path from 'path'
import {promisify} from 'util'
Expand Down Expand Up @@ -161,7 +162,8 @@ export async function rmRF(inputPath: string): Promise<void> {
* @returns Promise<void>
*/
export async function mkdirP(fsPath: string): Promise<void> {
await ioUtil.mkdirP(fsPath)
ok(fsPath, 'a path argument must be provided')
await ioUtil.mkdir(fsPath, {recursive: true})
}

/**
Expand Down