Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 1x 1x 1x 1x 1x 1x 7x 7x 8x 8x 1x 2x 2x 2x 1x 1x 1x 7x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /* eslint-disable node/no-deprecated-api */
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { exists, rmdir, readdir, mkdir, writeFile } from "@azure-tools/async-io";
import { dirname, extname } from "path";
import { lstatSync } from "fs";
import { ensureIsFolderUri, resolveUri } from "./uri-manipulation";
import { fileURLToPath } from "url";
import { ExistsUri } from "./data-acquisition";
/***********************
* OS abstraction (writing files, enumerating files)
***********************/
function isAccessibleFile(localPath: string) {
try {
return lstatSync(localPath).isFile();
} catch (e) {
return false;
}
}
function fileUriToLocalPath(fileUri: string): string {
Iif (!fileUri.startsWith("file:///")) {
throw new Error(
`Cannot write data to '${fileUri}'. ` +
(!fileUri.startsWith("file://")
? `File protocol '${fileUri}' not supported for writing.`
: "UNC paths not supported for writing.") +
" Make sure to specify a local, absolute path as target file/folder.",
);
}
return decodeURI(fileURLToPath(fileUri));
}
export async function enumerateFiles(folderUri: string, probeFiles: Array<string> = []): Promise<Array<string>> {
const results = new Array<string>();
folderUri = ensureIsFolderUri(folderUri);
if (folderUri.startsWith("file:")) {
let files: Array<string> = [];
try {
files = await readdir(fileUriToLocalPath(folderUri));
} catch (e) {
// noop
}
results.push(...files.map((f) => resolveUri(folderUri, f)).filter((f) => isAccessibleFile(fileUriToLocalPath(f))));
} else {
for (const candid of probeFiles.map((f) => resolveUri(folderUri, f))) {
Eif (await ExistsUri(candid)) {
results.push(candid);
}
}
}
return results;
}
async function createDirectoryFor(filePath: string): Promise<void> {
const dir: string = dirname(filePath);
if (!(await exists(dir))) {
await createDirectoryFor(dir);
try {
await mkdir(dir);
} catch (e) {
// mkdir throws if directory already exists - which happens occasionally due to race conditions
}
}
}
async function writeDataInternal(fileName: string, data: string | Buffer): Promise<void> {
await createDirectoryFor(fileName);
await writeFile(fileName, data);
}
/**
* Writes string to local file system.
* @param fileUri Target file uri.
* @param data String to write (encoding: UTF8).
*/
export function writeString(fileUri: string, data: string): Promise<void> {
return writeDataInternal(fileUriToLocalPath(fileUri), data);
}
/**
* Writes binary to local file system.
* @param fileUri Target file uri.
* @param data String to write (encoding - base64 encoded UTF8).
*/
export function writeBinary(fileUri: string, data: string): Promise<void> {
return writeDataInternal(fileUriToLocalPath(fileUri), Buffer.from(data, "base64"));
}
/**
* Clears a folder on the local file system.
* @param folderUri Folder uri.
*/
export async function clearFolder(folderUri: string, exceptions?: Array<string>): Promise<void> {
return await rmdir(
fileUriToLocalPath(folderUri),
new Set((exceptions || []).map((each) => fileUriToLocalPath(each).toLowerCase())),
);
}
export function fileUriToPath(fileUri: string): string {
return fileURLToPath(fileUri);
}
export function getExtension(name: string) {
const ext = extname(name);
if (ext) {
return ext.substr(1).toLowerCase();
}
return ext;
}
//------------------------------------------
// Legacy names, will be removed in next major version
//------------------------------------------
/**
* @deprecated use enumerateFiles instead.
*/
export const EnumerateFiles = enumerateFiles;
/**
* @deprecated use writeString instead.
*/
export const WriteString = writeString;
/**
* @deprecated use writeBinary instead.
*/
export const WriteBinary = writeBinary;
/**
* @deprecated use clearFolder instead.
*/
export const ClearFolder = clearFolder;
/**
* @deprecated use fileUriToPath instead.
*/
export const FileUriToPath = fileUriToPath;
/**
* @deprecated use getExtension instead.
*/
export const GetExtension = getExtension;
|