mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00
Add iOS-specific known folders to fs.knownFolders
This commit is contained in:
@ -8,7 +8,8 @@
|
|||||||
"noImplicitAny": false,
|
"noImplicitAny": false,
|
||||||
"noImplicitUseStrict": true,
|
"noImplicitUseStrict": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"diagnostics": true
|
"diagnostics": true,
|
||||||
|
"sourceMap": true
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"node_modules",
|
"node_modules",
|
||||||
|
@ -218,6 +218,40 @@ export var testGetKnownFolders = function () {
|
|||||||
// << file-system-known-folders
|
// << file-system-known-folders
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function _testIOSSpecificKnownFolder(knownFolderName: string){
|
||||||
|
let knownFolder: fs.Folder;
|
||||||
|
let createdFile: fs.File;
|
||||||
|
let testFunc = function testFunc(){
|
||||||
|
knownFolder = fs.knownFolders.ios[knownFolderName]();
|
||||||
|
createdFile = knownFolder.getFile("createdFile");
|
||||||
|
createdFile.writeTextSync("some text");
|
||||||
|
};
|
||||||
|
if (platform.isIOS){
|
||||||
|
testFunc();
|
||||||
|
TKUnit.assertNotNull(knownFolder, `Could not retrieve the ${knownFolderName} known folder.`);
|
||||||
|
TKUnit.assertTrue(knownFolder.isKnown, `The ${knownFolderName} folder should have its "isKnown" property set to true.`);
|
||||||
|
TKUnit.assertNotNull(createdFile, `Could not create a new file in the ${knownFolderName} known folder.`);
|
||||||
|
TKUnit.assertTrue(fs.File.exists(createdFile.path), `Could not create a new file in the ${knownFolderName} known folder.`);
|
||||||
|
TKUnit.assertEqual(createdFile.readTextSync(), "some text", `The contents of the new file created in the ${knownFolderName} known folder are not as expected.`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
TKUnit.assertThrows(testFunc,
|
||||||
|
`Trying to retrieve the ${knownFolderName} known folder on a platform different from iOS should throw!`,
|
||||||
|
`The "${knownFolderName}" known folder is available on iOS only!`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export var testIOSSpecificKnownFolders = function () {
|
||||||
|
_testIOSSpecificKnownFolder("library");
|
||||||
|
_testIOSSpecificKnownFolder("developer");
|
||||||
|
_testIOSSpecificKnownFolder("desktop");
|
||||||
|
_testIOSSpecificKnownFolder("downloads");
|
||||||
|
_testIOSSpecificKnownFolder("movies");
|
||||||
|
_testIOSSpecificKnownFolder("music");
|
||||||
|
_testIOSSpecificKnownFolder("pictures");
|
||||||
|
_testIOSSpecificKnownFolder("sharedPublic");
|
||||||
|
}
|
||||||
|
|
||||||
export var testGetEntities = function () {
|
export var testGetEntities = function () {
|
||||||
// >> file-system-folders-content
|
// >> file-system-folders-content
|
||||||
var documents = fs.knownFolders.documents();
|
var documents = fs.knownFolders.documents();
|
||||||
|
@ -8,7 +8,8 @@
|
|||||||
"noImplicitAny": false,
|
"noImplicitAny": false,
|
||||||
"noImplicitUseStrict": true,
|
"noImplicitUseStrict": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"diagnostics": true
|
"diagnostics": true,
|
||||||
|
"sourceMap": true
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"node_modules",
|
"node_modules",
|
||||||
|
@ -5,13 +5,10 @@ import * as utilsModule from "utils/utils";
|
|||||||
// TODO: Check whether we need try/catch blocks for the iOS implementation
|
// TODO: Check whether we need try/catch blocks for the iOS implementation
|
||||||
|
|
||||||
export class FileSystemAccess {
|
export class FileSystemAccess {
|
||||||
//private keyFileType = "NSFileType";
|
|
||||||
//private keyReadonly = "NSFileImmutable";
|
|
||||||
//private NSUTF8StringEncoding = 4;
|
|
||||||
private keyModificationTime = "NSFileModificationDate";
|
private keyModificationTime = "NSFileModificationDate";
|
||||||
private documentDir = 9;
|
private userDomain = NSSearchPathDirectory.NSApplicationDirectory; //1
|
||||||
private cachesDir = 13;
|
private documentDir = NSSearchPathDirectory.NSDocumentDirectory; //9
|
||||||
private userDomain = 1;
|
private cachesDir = NSSearchPathDirectory.NSCachesDirectory; //13
|
||||||
|
|
||||||
public getLastModified(path: string): Date {
|
public getLastModified(path: string): Date {
|
||||||
var fileManager = NSFileManager.defaultManager();
|
var fileManager = NSFileManager.defaultManager();
|
||||||
|
45
tns-core-modules/file-system/file-system.d.ts
vendored
45
tns-core-modules/file-system/file-system.d.ts
vendored
@ -208,6 +208,51 @@ declare module "file-system" {
|
|||||||
* iOS - this folder is read-only and contains the app and all its resources.
|
* iOS - this folder is read-only and contains the app and all its resources.
|
||||||
*/
|
*/
|
||||||
export function currentApp(): Folder;
|
export function currentApp(): Folder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains iOS-specific known folders.
|
||||||
|
*/
|
||||||
|
module ios {
|
||||||
|
/**
|
||||||
|
* Gets the NSLibraryDirectory.
|
||||||
|
*/
|
||||||
|
export function library(): Folder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the NSDeveloperDirectory.
|
||||||
|
*/
|
||||||
|
export function developer(): Folder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the NSDesktopDirectory.
|
||||||
|
*/
|
||||||
|
export function desktop(): Folder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the NSDownloadsDirectory.
|
||||||
|
*/
|
||||||
|
export function downloads(): Folder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the NSMoviesDirectory.
|
||||||
|
*/
|
||||||
|
export function movies(): Folder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the NSMusicDirectory.
|
||||||
|
*/
|
||||||
|
export function music(): Folder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the NSPicturesDirectory.
|
||||||
|
*/
|
||||||
|
export function pictures(): Folder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the NSSharedPublicDirectory.
|
||||||
|
*/
|
||||||
|
export function sharedPublic(): Folder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import file_access_module = require("file-system/file-system-access");
|
import file_access_module = require("file-system/file-system-access");
|
||||||
|
import * as platformModule from "platform";
|
||||||
|
|
||||||
// The FileSystemAccess implementation, used through all the APIs.
|
// The FileSystemAccess implementation, used through all the APIs.
|
||||||
var fileAccess;
|
var fileAccess;
|
||||||
@ -10,6 +11,13 @@ var getFileAccess = function (): file_access_module.FileSystemAccess {
|
|||||||
return fileAccess;
|
return fileAccess;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let platform: typeof platformModule;
|
||||||
|
function ensurePlatform() {
|
||||||
|
if (!platform) {
|
||||||
|
platform = require("platform");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// we are defining these as private variables within the IO scope and will use them to access the corresponding properties for each FSEntity instance.
|
// we are defining these as private variables within the IO scope and will use them to access the corresponding properties for each FSEntity instance.
|
||||||
// this allows us to encapsulate (hide) the explicit property setters and force the users go through the exposed APIs to receive FSEntity instances.
|
// this allows us to encapsulate (hide) the explicit property setters and force the users go through the exposed APIs to receive FSEntity instances.
|
||||||
var nameProperty = "_name";
|
var nameProperty = "_name";
|
||||||
@ -491,6 +499,119 @@ export module knownFolders {
|
|||||||
|
|
||||||
return _app;
|
return _app;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export module ios {
|
||||||
|
function _checkPlatform(knownFolderName: string){
|
||||||
|
ensurePlatform();
|
||||||
|
if (!platform.isIOS){
|
||||||
|
throw new Error(`The "${knownFolderName}" known folder is available on iOS only!`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _library: Folder;
|
||||||
|
export var library = function(): Folder {
|
||||||
|
_checkPlatform("library");
|
||||||
|
if (!_library) {
|
||||||
|
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSLibraryDirectory);
|
||||||
|
_library = Folder.fromPath(path);
|
||||||
|
_library[pathProperty] = path;
|
||||||
|
_library[isKnownProperty] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _library;
|
||||||
|
};
|
||||||
|
|
||||||
|
let _developer: Folder;
|
||||||
|
export var developer = function(): Folder {
|
||||||
|
_checkPlatform("developer");
|
||||||
|
if (!_developer) {
|
||||||
|
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSDeveloperDirectory);
|
||||||
|
_developer = Folder.fromPath(path);
|
||||||
|
_developer[pathProperty] = path;
|
||||||
|
_developer[isKnownProperty] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _developer;
|
||||||
|
};
|
||||||
|
|
||||||
|
let _desktop: Folder;
|
||||||
|
export var desktop = function(): Folder {
|
||||||
|
_checkPlatform("desktop");
|
||||||
|
if (!_desktop) {
|
||||||
|
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSDesktopDirectory);
|
||||||
|
_desktop = Folder.fromPath(path);
|
||||||
|
_desktop[pathProperty] = path;
|
||||||
|
_desktop[isKnownProperty] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _desktop;
|
||||||
|
};
|
||||||
|
|
||||||
|
let _downloads: Folder;
|
||||||
|
export var downloads = function(): Folder {
|
||||||
|
_checkPlatform("downloads");
|
||||||
|
if (!_downloads) {
|
||||||
|
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSDownloadsDirectory);
|
||||||
|
_downloads = Folder.fromPath(path);
|
||||||
|
_downloads[pathProperty] = path;
|
||||||
|
_downloads[isKnownProperty] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _downloads;
|
||||||
|
};
|
||||||
|
|
||||||
|
let _movies: Folder;
|
||||||
|
export var movies = function(): Folder {
|
||||||
|
_checkPlatform("movies");
|
||||||
|
if (!_movies) {
|
||||||
|
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSMoviesDirectory);
|
||||||
|
_movies = Folder.fromPath(path);
|
||||||
|
_movies[pathProperty] = path;
|
||||||
|
_movies[isKnownProperty] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _movies;
|
||||||
|
};
|
||||||
|
|
||||||
|
let _music: Folder;
|
||||||
|
export var music = function(): Folder {
|
||||||
|
_checkPlatform("music");
|
||||||
|
if (!_music) {
|
||||||
|
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSMusicDirectory);
|
||||||
|
_music = Folder.fromPath(path);
|
||||||
|
_music[pathProperty] = path;
|
||||||
|
_music[isKnownProperty] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _music;
|
||||||
|
};
|
||||||
|
|
||||||
|
let _pictures: Folder;
|
||||||
|
export var pictures = function(): Folder {
|
||||||
|
_checkPlatform("pictures");
|
||||||
|
if (!_pictures) {
|
||||||
|
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSPicturesDirectory);
|
||||||
|
_pictures = Folder.fromPath(path);
|
||||||
|
_pictures[pathProperty] = path;
|
||||||
|
_pictures[isKnownProperty] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _pictures;
|
||||||
|
};
|
||||||
|
|
||||||
|
let _sharedPublic: Folder;
|
||||||
|
export var sharedPublic = function(): Folder {
|
||||||
|
_checkPlatform("sharedPublic");
|
||||||
|
if (!_sharedPublic) {
|
||||||
|
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.NSSharedPublicDirectory);
|
||||||
|
_sharedPublic = Folder.fromPath(path);
|
||||||
|
_sharedPublic[pathProperty] = path;
|
||||||
|
_sharedPublic[isKnownProperty] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _sharedPublic;
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export module path {
|
export module path {
|
||||||
|
@ -9,7 +9,8 @@
|
|||||||
"noImplicitAny": false,
|
"noImplicitAny": false,
|
||||||
"noImplicitUseStrict": true,
|
"noImplicitUseStrict": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"diagnostics": true
|
"diagnostics": true,
|
||||||
|
"sourceMap": true
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"node_modules",
|
"node_modules",
|
||||||
|
Reference in New Issue
Block a user