本文整理匯總了TypeScript中fs.Stats類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Stats類的具體用法?TypeScript Stats怎麽用?TypeScript Stats使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Stats類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: walk
export function walk(path: string, options: WalkOptions) {
let stats: Stats
try {
stats = (options.follow !== false ? statSync : lstatSync)(path)
} catch (e) {
options.error && options.error(e, path)
return
}
if (stats.isFile()) {
options.file && options.file(path, stats)
} else if (stats.isDirectory()) {
if (!options.dir || options.dir(path, stats) !== false) {
let entries: string[]
try {
entries = readdirSync(path)
} catch (e) {
options.error && options.error(e, path)
return
}
if (!options.walkDir || options.walkDir(path, entries, stats) !== false) {
for (const entry of entries) {
walk(join(path, entry), options)
}
}
}
} else {
options.other && options.other(path, stats)
}
}
示例2: findFile
function findFile(dir: string, fileName: string): string {
'use strict';
let children: string[] = fs.readdirSync(dir);
for (let i: number = 0; i < children.length; ++i) {
let path: string = dir + '/' + children[i];
let stat: Stats = fs.statSync(path);
if (stat.isFile()) {
if (children[i] === fileName) {
return path;
}
} else if (stat.isDirectory()) {
let result: string = findFile(path, fileName);
if (result) {
return result;
}
}
}
return undefined;
}
示例3: saveCachedResolution
async function saveCachedResolution (): Promise<FetchedPackage> {
const target = path.join(modules, spec.name)
const stat: Stats = await fs.lstat(target)
if (stat.isSymbolicLink()) {
const linkPath = await fs.readlink(target)
return save(path.resolve(linkPath, target))
}
return save(target)
async function save (fullpath: string): Promise<FetchedPackage> {
const data = await requireJson(path.join(fullpath, 'package.json'))
return {
fetchingPkg: Promise.resolve(data),
fetchingFiles: Promise.resolve(),
id: path.basename(fullpath),
fromCache: true,
path: fullpath,
abort: () => Promise.resolve(),
}
}
}
示例4:
export const getFileType = (stats: Stats): FileType => {
return stats.isSymbolicLink() ? FileType.SymbolicLink :
stats.isFile() ? FileType.File :
stats.isDirectory() ? FileType.Directory :
stats.isBlockDevice() ? FileType.BlockDevice :
stats.isCharacterDevice() ? FileType.CharacterDevice :
stats.isFIFO() ? FileType.FIFO :
stats.isSocket() ? FileType.Socket :
FileType.Other;
};
示例5: safeLink
async function safeLink(existingPath: string, newPath: string, stat: Stats) {
try {
await fs.link(existingPath, newPath)
} catch (err) {
// shouldn't normally happen, but if the file was already somehow linked,
// the installation should not fail
if (err.code === 'EEXIST') {
return
}
// might happen if package contains a broken symlink, we don't fail on this
if (err.code === 'ENOENT' && stat.isSymbolicLink()) {
logger.warn({
message: `Broken symlink found: ${existingPath}`,
})
return
}
throw err
}
}
示例6: get_listing
export async function get_listing(
path: string,
hidden: boolean = false
): Promise<ListingEntry[]> {
const dir = HOME + "/" + path;
const files: ListingEntry[] = [];
let name: string;
for (name of await callback(readdir, dir)) {
if (!hidden && name[0] === ".") {
continue;
}
let entry: ListingEntry;
try {
// I don't actually know if file can fail to be JSON-able with node.js -- is there
// even a string in Node.js that cannot be dumped to JSON? With python
// this definitely was a problem, but I can't find the examples now. Users
// sometimes create "insane" file names via bugs in C programs...
JSON.stringify(name);
entry = { name };
} catch (err) {
entry = { name: "????", error: "Cannot display bad binary filename. " };
}
let stats: Stats;
try {
stats = await callback(lstat, dir + "/" + entry.name);
if (stats.isSymbolicLink()) {
entry.issymlink = true;
try {
stats = await callback(stat, dir + "/" + entry.name);
} catch (err) {
// broken link -- just report info about the link itself...
}
}
entry.mtime = stats.mtime.valueOf() / 1000;
if (stats.isDirectory()) {
entry.isdir = true;
try {
const v = await callback(readdir, dir + "/" + entry.name);
if (hidden) {
entry.size = v.length;
} else {
// only count non-hidden files
entry.size = 0;
for (let x of v) {
if (x[0] != ".") {
entry.size += 1;
}
}
}
} catch (err) {
// just ignore -- no size info.
}
} else {
entry.size = stats.size;
}
} catch (err) {
entry.error = `${entry.error ? entry.error : ""}${err}`;
}
files.push(entry);
}
return files;
}