UNPKG

9.66 kBTypeScriptView Raw
1import { FileSystemAccess } from './file-system-access';
2
3/**
4 * Returns FileSystemAccess, a shared singleton utility class to provide methods to access and work with the file system. This is used under the hood of all the file system apis in @nativescript/core and provided as a lower level convenience if needed.
5 * @returns FileSystemAccess
6 */
7export function getFileAccess(): FileSystemAccess;
8
9/**
10 * Represents a single entity on the file system.
11 */
12export class FileSystemEntity {
13 /**
14 * Gets the Date object specifying the last time this entity was modified.
15 */
16 lastModified: Date;
17
18 /**
19 * Gets the name of the entity.
20 */
21 name: string;
22
23 /**
24 * Gets the fully-qualified path (including the extension for a File) of the entity.
25 */
26 path: string;
27
28 /**
29 * Gets the Folder object representing the parent of this entity.
30 * Will be null for a root folder like Documents or Temporary.
31 * This property is readonly.
32 */
33 parent: Folder;
34
35 /**
36 * Removes (deletes) the current Entity from the file system.
37 */
38 remove(): Promise<any>;
39
40 /**
41 * Removes (deletes) the current Entity from the file system synchronously.
42 */
43 removeSync(onError?: (error: any) => any): void;
44
45 /**
46 * Renames the current entity using the specified name.
47 * @param newName The new name to be applied to the entity.
48 */
49 rename(newName: string): Promise<any>;
50
51 /**
52 * Renames the current entity synchronously, using the specified name.
53 * @param newName The new name to be applied to the entity.
54 */
55 renameSync(newName: string, onError?: (error: any) => any): void;
56}
57
58/**
59 * Represents a File entity on the file system.
60 */
61export class File extends FileSystemEntity {
62 /**
63 * Checks whether a File with the specified path already exists.
64 * @param path The path to check for.
65 */
66 static exists(path: string): boolean;
67
68 /**
69 * Gets the extension of the file.
70 */
71 extension: string;
72
73 /**
74 * Gets the size in bytes of the file.
75 */
76 size: number;
77
78 /**
79 * Gets a value indicating whether the file is currently locked, meaning a background operation associated with this file is running.
80 */
81 isLocked: boolean;
82
83 /**
84 * Gets or creates a File entity at the specified path.
85 * @param path The path to get/create the file at.
86 */
87 static fromPath(path: string): File;
88
89 /**
90 * Reads the content of the file as a string using the specified encoding (defaults to UTF-8).
91 * @param encoding An optional value specifying the preferred encoding (defaults to UTF-8).
92 */
93 readText(encoding?: string): Promise<string>;
94
95 /**
96 * Reads the content of the file as a string synchronously, using the specified encoding (defaults to UTF-8).
97 * @param onError An optional function to be called if some IO-error occurs.
98 * @param encoding An optional value specifying the preferred encoding (defaults to UTF-8).
99 */
100 readTextSync(onError?: (error: any) => any, encoding?: string): string;
101
102 /**
103 * Reads the binary content of the file asynchronously.
104 */
105 read(): Promise<any>;
106
107 /**
108 * Reads the binary content of the file synchronously.
109 * @param onError An optional function to be called if some IO-error occurs.
110 */
111 readSync(onError?: (error: any) => any): any;
112
113 /**
114 * Writes the provided string to the file, using the specified encoding (defaults to UTF-8).
115 * @param content The content to be saved to the file.
116 * @param encoding An optional value specifying the preferred encoding (defaults to UTF-8).
117 */
118 writeText(content: string, encoding?: string): Promise<any>;
119
120 /**
121 * Writes the provided string to the file synchronously, using the specified encoding (defaults to UTF-8).
122 * @param content The content to be saved to the file.
123 * @param onError An optional function to be called if some IO-error occurs.
124 * @param encoding An optional value specifying the preferred encoding (defaults to UTF-8).
125 */
126 writeTextSync(content: string, onError?: (error: any) => any, encoding?: string): void;
127
128 /**
129 * Writes the provided binary content to the file.
130 * @param content The binary content to be saved to the file.
131 */
132 write(content: any): Promise<void>;
133
134 /**
135 * Writes the provided binary content to the file synchronously.
136 * @param content The binary content to be saved to the file.
137 * @param onError An optional function to be called if some IO-error occurs.
138 */
139 writeSync(content: any, onError?: (error: any) => any): void;
140}
141
142/**
143 * Represents a Folder (directory) entity on the file system.
144 */
145export class Folder extends FileSystemEntity {
146 /**
147 * Determines whether this instance is a KnownFolder (accessed through the KnownFolders object).
148 */
149 isKnown: boolean;
150
151 /**
152 * Gets or creates a Folder entity at the specified path.
153 * @param path The path to get/create the folder at.
154 */
155 static fromPath(path: string): Folder;
156
157 /**
158 * Checks whether a Folder with the specified path already exists.
159 * @param path The path to check for.
160 */
161 static exists(path: string): boolean;
162
163 /**
164 * Checks whether this Folder contains an Entity with the specified name.
165 * The path of the folder is added to the name to resolve the complete path to check for.
166 * @param name The name of the entity to check for.
167 */
168 contains(name: string): boolean;
169
170 /**
171 * Deletes all the files and folders (recursively), contained within this Folder.
172 */
173 clear(): Promise<any>;
174
175 /**
176 * Deletes all the files and folders (recursively), contained within this Folder synchronously.
177 * @param onError An optional function to be called if some error occurs.
178 */
179 clearSync(onError?: (error: any) => void): void;
180
181 /**
182 * Gets or creates a File entity with the specified name within this Folder.
183 * @param name The name of the file to get/create.
184 */
185 getFile(name: string): File;
186
187 /**
188 * Gets or creates a Folder entity with the specified name within this Folder.
189 * @param name The name of the folder to get/create.
190 */
191 getFolder(name: string): Folder;
192
193 /**
194 * Gets all the top-level entities residing within this folder.
195 */
196 getEntities(): Promise<Array<FileSystemEntity>>;
197
198 /**
199 * Gets all the top-level entities residing within this folder synchronously.
200 * @param onError An optional function to be called if some error occurs.
201 */
202 getEntitiesSync(onError?: (error: any) => any): Array<FileSystemEntity>;
203
204 /**
205 * Enumerates all the top-level FileSystem entities residing within this folder.
206 * @param onEntity A callback that receives the current entity. If the callback returns false this will mean for the iteration to stop.
207 */
208 eachEntity(onEntity: (entity: FileSystemEntity) => boolean);
209}
210
211/**
212 * Provides access to the top-level Folders instances that are accessible from the application. Use these as entry points to access the FileSystem.
213 */
214export module knownFolders {
215 /**
216 * Gets the Documents folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
217 */
218 export function documents(): Folder;
219
220 /**
221 * Gets the Documents folder available for the current application on an external storage. This Folder is private for the application and not accessible from Users/External apps.
222 * On android this requires READ_EXTERNAL_STORAGE/WRITE_EXTERNAL_STORAGE permissions
223 * There is no external storage on iOS os it is the same as `documents()`
224 */
225 export function externalDocuments(): Folder;
226
227 /**
228 * Gets the Temporary (Caches) folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
229 */
230 export function temp(): Folder;
231
232 /**
233 * Gets the root folder for the current application. This Folder is private for the application and not accessible from Users/External apps.
234 * iOS - this folder is read-only and contains the app and all its resources.
235 */
236 export function currentApp(): Folder;
237
238 /**
239 * Contains iOS-specific known folders.
240 */
241 module ios {
242 /**
243 * Gets the NSLibraryDirectory. Note that the folder will not be created if it did not exist.
244 */
245 export function library(): Folder;
246
247 /**
248 * Gets the NSDeveloperDirectory. Note that the folder will not be created if it did not exist.
249 */
250 export function developer(): Folder;
251
252 /**
253 * Gets the NSDesktopDirectory. Note that the folder will not be created if it did not exist.
254 */
255 export function desktop(): Folder;
256
257 /**
258 * Gets the NSDownloadsDirectory. Note that the folder will not be created if it did not exist.
259 */
260 export function downloads(): Folder;
261
262 /**
263 * Gets the NSMoviesDirectory. Note that the folder will not be created if it did not exist.
264 */
265 export function movies(): Folder;
266
267 /**
268 * Gets the NSMusicDirectory. Note that the folder will not be created if it did not exist.
269 */
270 export function music(): Folder;
271
272 /**
273 * Gets the NSPicturesDirectory. Note that the folder will not be created if it did not exist.
274 */
275 export function pictures(): Folder;
276
277 /**
278 * Gets the NSSharedPublicDirectory. Note that the folder will not be created if it did not exist.
279 */
280 export function sharedPublic(): Folder;
281 }
282}
283
284/**
285 * Enables path-specific operations like join, extension, etc.
286 */
287export module path {
288 /**
289 * Normalizes a path, taking care of occurrances like ".." and "//".
290 * @param path The path to be normalized.
291 */
292 export function normalize(path: string): string;
293
294 /**
295 * Joins all the provided string components, forming a valid and normalized path.
296 * @param paths An array of string components to be joined.
297 */
298 export function join(...paths: string[]): string;
299
300 /**
301 * Gets the string used to separate file paths.
302 */
303 export const separator: string;
304}