8000 Merge pull request #1888 from NativeScript/bektchiev/fix-create-file · hbcodeXCI/NativeScript@d88e675 · GitHub
[go: up one dir, main page]

Skip to content

Commit d88e675

Browse files
author
Vladimir Enchev
committed
Merge pull request NativeScript#1888 from NativeScript/bektchiev/fix-create-file
Ensure parent directory when creating a new file
2 parents f768fda + dcdd0a6 commit d88e675

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

apps/tests/file-system-tests.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,14 @@ export function test_UnlockAfterWrite(done) {
638638
return file.remove();
639639
}).then(() => done())
640640
.catch(done);
641+
}
642+
643+
export function test_CreateParentOnNewFile(done) {
644+
var documentsFolderName = fs.knownFolders.documents().path;
645+
var tempFileName = fs.path.join(documentsFolderName, "folder1", "folder2", "Test_File_Create_Parent.txt");
646+
var file = fs.File.fromPath(tempFileName);
647+
file.writeText("Hello World!").then(() => {
648+
return fs.knownFolders.documents().getFolder("folder1").remove();
649+
}).then(() => done())
650+
.catch(done);
641651
}

file-system/file-system-access.android.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ export class FileSystemAccess {
327327
if (isFolder) {
328328
created = javaFile.mkdirs();
329329
} else {
330+
javaFile.getParentFile().mkdirs();
330331
created = javaFile.createNewFile();
331332
}
332333

file-system/file-system-access.ios.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ export class FileSystemAccess {
5252
var exists = fileManager.fileExistsAtPath(path);
5353

5454
if (!exists) {
55-
if (!fileManager.createFileAtPathContentsAttributes(path, null, null)) {
55+
var parentPath = this.getParent(path, onError).path;
56+
if (!fileManager.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(parentPath, true, null) ||
57+
!fileManager.createFileAtPathContentsAttributes(path, null, null)) {
5658
if (onError) {
57-
onError(new Error("Failed to create folder at path '" + path + "'"));
59+
onError(new Error("Failed to create file at path '" + path + "'"));
5860
}
59-
6061
return undefined;
6162
}
6263
}
@@ -406,4 +407,4 @@ export class FileSystemAccess {
406407
var nsString = NSString.stringWithString(NSString.pathWithComponents(nsArray));
407408
return nsString.stringByStandardizingPath;
408409
}
409-
}
410+
}

0 commit comments

Comments
 (0)
0