Skip to content

Commit

Permalink
feat(Filesystem): Deprecate createIntermediateDirectories in mkdir (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Sep 27, 2019
1 parent e7bd634 commit d67d460
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ public void mkdir(PluginCall call) {
String path = call.getString("path");
String directory = getDirectoryParameter(call);
boolean intermediate = call.getBoolean("createIntermediateDirectories", false).booleanValue();
if (call.getBoolean("createIntermediateDirectories") != null) {
Log.w(getLogTag(),"createIntermediateDirectories is deprecated, use recursive");
}
boolean recursive = call.getBoolean("recursive", false).booleanValue();

File fileObject = getFileObject(path, directory);

Expand All @@ -338,7 +342,7 @@ public void mkdir(PluginCall call) {
if (!isPublicDirectory(directory)
|| isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_WRITE_FOLDER_PERMISSIONS, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
boolean created = false;
if (intermediate) {
if (intermediate || recursive) {
created = fileObject.mkdirs();
} else {
created = fileObject.mkdir();
Expand Down
12 changes: 10 additions & 2 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,9 +656,16 @@ export interface MkdirOptions {
*/
directory?: FilesystemDirectory;
/**
* @deprecated - use recursive
* Whether to create any missing parent directories as well
* Defaults to false
*/
createIntermediateDirectories: boolean;
createIntermediateDirectories?: boolean;
/**
* Whether to create any missing parent directories as well.
* Defaults to false
*/
recursive?: boolean;
}

export interface RmdirOptions {
Expand All @@ -671,7 +678,8 @@ export interface RmdirOptions {
*/
directory?: FilesystemDirectory;
/**
* Whether to recursively remove the contents of the directory (defaults to false)
* Whether to recursively remove the contents of the directory
* Defaults to false
*/
recursive?: boolean;
}
Expand Down
17 changes: 11 additions & 6 deletions core/src/web/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin {
const subDirIndex = parentPath.indexOf('/', 1);
if (subDirIndex !== -1) {
const parentArgPath = parentPath.substr(subDirIndex);
await this.mkdir({path: parentArgPath, directory: options.directory, createIntermediateDirectories: true});
await this.mkdir({path: parentArgPath, directory: options.directory, recursive: true});
}
}
const now = Date.now();
Expand Down Expand Up @@ -200,7 +200,7 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin {
let parentEntry = await this.dbRequest('get', [parentPath]) as EntryObj;
if (parentEntry === undefined) {
const parentArgPath = parentPath.substr(parentPath.indexOf('/', 1));
await this.mkdir({path: parentArgPath, directory: options.directory, createIntermediateDirectories: true});
await this.mkdir({path: parentArgPath, directory: options.directory, recursive: true});
}

if (occupiedEntry !== undefined) {
Expand Down Expand Up @@ -247,6 +247,11 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin {
async mkdir(options: MkdirOptions): Promise<MkdirResult> {
const path: string = this.getPath(options.directory, options.path);
const createIntermediateDirectories = options.createIntermediateDirectories;
if (options.createIntermediateDirectories !== undefined) {
console.warn('createIntermediateDirectories is deprecated, use recursive');
}
const recursive = options.recursive;
const doRecursive = (createIntermediateDirectories || recursive);
const parentPath = path.substr(0, path.lastIndexOf('/'));

let depth = (path.match(/\//g) || []).length;
Expand All @@ -256,15 +261,15 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin {
throw Error('Cannot create Root directory');
if (occupiedEntry !== undefined)
throw Error('Current directory does already exist.');
if (!createIntermediateDirectories && depth !== 2 && parentEntry === undefined)
if (!doRecursive && depth !== 2 && parentEntry === undefined)
throw Error('Parent directory must exist');

if (createIntermediateDirectories && depth !== 2 && parentEntry === undefined) {
if (doRecursive && depth !== 2 && parentEntry === undefined) {
const parentArgPath = parentPath.substr(parentPath.indexOf('/', 1));
await this.mkdir({
path: parentArgPath,
directory: options.directory,
createIntermediateDirectories: createIntermediateDirectories
recursive: doRecursive
});
}
const now = Date.now();
Expand Down Expand Up @@ -507,7 +512,7 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin {
await this.mkdir({
path: to,
directory: toDirectory,
createIntermediateDirectories: false,
recursive: false,
});

// Copy the mtime/ctime of a renamed directory
Expand Down
6 changes: 5 additions & 1 deletion electron/src/electron/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu
if(Object.keys(this.fileLocations).indexOf(options.directory) === -1)
reject(`${options.directory} is currently not supported in the Electron implementation.`);
let lookupPath = this.fileLocations[options.directory] + options.path;
this.NodeFS.mkdir(lookupPath, { recursive: options.createIntermediateDirectories }, (err:any) => {
if (options.createIntermediateDirectories !== undefined) {
console.warn('createIntermediateDirectories is deprecated, use recursive');
}
const doRecursive = options.createIntermediateDirectories || options.recursive;
this.NodeFS.mkdir(lookupPath, { recursive: doRecursive }, (err:any) => {
if(err) {
reject(err);
return;
Expand Down
4 changes: 2 additions & 2 deletions example/src/pages/filesystem/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class FilesystemPage {
let ret = await Plugins.Filesystem.mkdir({
path: 'secrets',
directory: FilesystemDirectory.Documents,
createIntermediateDirectories: false
recursive: false
});
console.log('Made dir', ret);
} catch(e) {
Expand Down Expand Up @@ -207,7 +207,7 @@ export class FilesystemPage {
mkdirAll(paths) {
return this.doAll(paths, path => Plugins.Filesystem.mkdir({
path,
createIntermediateDirectories: true,
recursive: true,
}));
}

Expand Down
6 changes: 5 additions & 1 deletion ios/Capacitor/Capacitor/Plugins/Filesystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,18 @@ public class CAPFilesystemPlugin : CAPPlugin {
}

let createIntermediateDirectories = call.get("createIntermediateDirectories", Bool.self, false)!
if let _ = call.get("createIntermediateDirectories", Bool.self) {
CAPLog.print("createIntermediateDirectories is deprecated, use recursive")
}
let recursive = call.get("recursive", Bool.self, false)!
let directoryOption = call.get("directory", String.self, DEFAULT_DIRECTORY)!
guard let fileUrl = getFileUrl(path, directoryOption) else {
handleError(call, "Invalid path")
return
}

do {
try FileManager.default.createDirectory(at: fileUrl, withIntermediateDirectories: createIntermediateDirectories, attributes: nil)
try FileManager.default.createDirectory(at: fileUrl, withIntermediateDirectories: createIntermediateDirectories || recursive, attributes: nil)
call.success()
} catch let error as NSError {
handleError(call, error.localizedDescription, error)
Expand Down
2 changes: 1 addition & 1 deletion site/docs-md/apis/filesystem/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async mkdir() {
let ret = await Filesystem.mkdir({
path: 'secrets',
directory: FilesystemDirectory.Documents,
createIntermediateDirectories: false // like mkdir -p
recursive: false // like mkdir -p
});
} catch(e) {
console.error('Unable to make directory', e);
Expand Down

0 comments on commit d67d460

Please sign in to comment.