本文整理汇总了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;
}