UNPKG

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