Skip to content

Commit 8152928

Browse files
committed
mkdirp-ring
1 parent 83439b3 commit 8152928

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const createFile = require('./')
22

3-
const st = createFile('folder/hello-world.txt')
3+
const st = createFile('/some/folder/hello-world.txt')
44
var missing = 2
55

66
st.write(0, Buffer.from('hello '), done)

index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function createFile (name, opts) {
7171
if (err) return onerror(err)
7272
requestFileSystem(window.PERSISTENT, granted, function (res) {
7373
fs = res
74-
mkdir(folder(name), function () {
74+
mkdirp(parentFolder(name), function () {
7575
fs.root.getFile(name, {create: true}, function (e) {
7676
entry = e
7777
entry.file(function (f) {
@@ -83,9 +83,13 @@ function createFile (name, opts) {
8383
}, onerror)
8484
})
8585

86-
function mkdir (name, ondone) {
86+
function mkdirp (name, ondone) {
8787
if (!name) return ondone()
88-
fs.root.getDirectory(name, {create: true}, ondone, ondone)
88+
fs.root.getDirectory(name, {create: true}, ondone, function () {
89+
mkdirp(parentFolder(name), function () {
90+
fs.root.getDirectory(name, {create: true}, ondone, ondone)
91+
})
92+
})
8993
}
9094

9195
function onerror (err) {
@@ -95,10 +99,11 @@ function createFile (name, opts) {
9599
}
96100
}
97101

98-
function folder (path) {
99-
const i = path.indexOf('/')
100-
const j = path.indexOf('\\')
101-
return path.slice(0, Math.max(0, i, j))
102+
function parentFolder (path) {
103+
const i = path.lastIndexOf('/')
104+
const j = path.lastIndexOf('\\')
105+
const p = path.slice(0, Math.max(0, i, j))
106+
return /^\w:$/.test(p) ? '' : p
102107
}
103108

104109
function WriteRequest (pool, file, entry, mutex) {

0 commit comments

Comments
 (0)