@meteor-it/fs
Advanced tools
Comparing version 1.0.0 to 1.0.2
189
index.js
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs = require("fs"); | ||
const async_1 = require("@meteor-it/async"); | ||
const utils_1 = require("@meteor-it/utils"); | ||
const path_1 = require("path"); | ||
function readDir(dir) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return async_1.cb2promise(fs.readdir)(dir); | ||
}); | ||
const util_1 = require("util"); | ||
// TODO: Deprecate (Because node now supports promised fs) | ||
/** | ||
* Returns true if path is a valid data url | ||
* @param path path | ||
*/ | ||
function isDataUrl(path) { | ||
return /^data:.+\/.+;base64,/.test(path.substr(0, 268)); | ||
} | ||
/** | ||
* Returns mime and data of dataurl | ||
* @param path Data url | ||
*/ | ||
function parseDataUrl(path) { | ||
return { | ||
mime: path.slice(5, path.indexOf(';')), | ||
data: Buffer.from(path.slice(path.indexOf(',') + 1), 'base64') | ||
}; | ||
} | ||
/** | ||
* Get all files in directory | ||
*/ | ||
async function readDir(dir) { | ||
return await util_1.promisify(fs.readdir)(dir); | ||
} | ||
exports.readDir = readDir; | ||
function readFile(file) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return async_1.cb2promise(fs.readFile)(file); | ||
}); | ||
/** | ||
* Read file or parse data url | ||
* @param file Path to file to read | ||
*/ | ||
async function readFile(file) { | ||
if (isDataUrl(file)) | ||
return parseDataUrl(file).data; | ||
return await util_1.promisify(fs.readFile)(file); | ||
} | ||
exports.readFile = readFile; | ||
function stat(file) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return async_1.cb2promise(fs.stat)(file); | ||
}); | ||
async function stat(file) { | ||
return await util_1.promisify(fs.stat)(file); | ||
} | ||
exports.stat = stat; | ||
function writeFile(filename, text) { | ||
return async_1.cb2promise(fs.writeFile)(filename, text); | ||
async function open(file, mode, access) { | ||
return await util_1.promisify(fs.open)(file, mode, access); | ||
} | ||
exports.open = open; | ||
async function read(fd, buffer, offset, length, position) { | ||
return await util_1.promisify(fs.read)(fd, buffer, offset, length, position); | ||
} | ||
exports.read = read; | ||
async function close(fd) { | ||
return await util_1.promisify(fs.close)(fd); | ||
} | ||
exports.close = close; | ||
/** | ||
* Write text to file | ||
*/ | ||
async function writeFile(filename, text) { | ||
return await util_1.promisify(fs.writeFile)(filename, text); | ||
} | ||
exports.writeFile = writeFile; | ||
function walkDir(dir, cb) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!(yield exists(dir))) { | ||
throw new Error('No such file or directory: ' + dir); | ||
/** | ||
* Walk directory | ||
* @param dir Directory to walk | ||
* @param cb If provided, found files will returned realtime. If not - function will return all found files | ||
*/ | ||
async function walkDir(dir, cb) { | ||
if (!await exists(dir)) { | ||
throw new Error('No such file or directory: ' + dir); | ||
} | ||
let returnValue; | ||
let shouldReturn = false; | ||
if (!cb) { | ||
returnValue = []; | ||
shouldReturn = true; | ||
cb = (file, dir) => { | ||
returnValue.push(dir + path_1.sep + file); | ||
}; | ||
} | ||
let dirList = []; | ||
await utils_1.asyncEach(await readDir(dir), async (file) => { | ||
let path = dir + path_1.sep + file; | ||
if (await isFile(path)) { | ||
cb(file, dir); | ||
} | ||
let returnValue; | ||
let shouldReturn = false; | ||
if (!cb) { | ||
returnValue = []; | ||
shouldReturn = true; | ||
cb = (file, dir) => { | ||
returnValue.push(dir + path_1.sep + file); | ||
}; | ||
else if (await isDirectory(path)) { | ||
dirList.push(file); | ||
} | ||
let dirList = []; | ||
yield async_1.asyncEach(yield readDir(dir), (file) => __awaiter(this, void 0, void 0, function* () { | ||
let path = dir + path_1.sep + file; | ||
if (yield isFile(path)) { | ||
cb(file, dir); | ||
} | ||
else if (yield isDirectory(path)) { | ||
dirList.push(file); | ||
} | ||
})); | ||
yield async_1.asyncEach(dirList, (dirLevelDown) => __awaiter(this, void 0, void 0, function* () { | ||
yield walkDir(dir + path_1.sep + dirLevelDown, cb); | ||
})); | ||
if (shouldReturn) { | ||
return returnValue.sort(); | ||
} | ||
return; | ||
}); | ||
await utils_1.asyncEach(dirList, async (dirLevelDown) => { | ||
await walkDir(dir + path_1.sep + dirLevelDown, cb); | ||
}); | ||
if (shouldReturn) { | ||
return returnValue.sort(); | ||
} | ||
return null; | ||
} | ||
exports.walkDir = walkDir; | ||
function exists(file) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
let result = yield async_1.cb2promise(fs.access)(file, fs.constants.F_OK); | ||
if (result === undefined) { | ||
return true; | ||
} | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
}); | ||
/** | ||
* Check if file exists | ||
*/ | ||
async function exists(file) { | ||
try { | ||
let result = await util_1.promisify(fs.access)(file, fs.constants.F_OK); | ||
return result === undefined; | ||
} | ||
catch (e) { | ||
// Because only "err" field is returned if not exists | ||
return false; | ||
} | ||
} | ||
exports.exists = exists; | ||
function isFile(path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (yield stat(path)).isFile(); | ||
}); | ||
/** | ||
* Is path a file | ||
* @param path path to test | ||
*/ | ||
async function isFile(path) { | ||
return (await stat(path)).isFile(); | ||
} | ||
exports.isFile = isFile; | ||
function isDirectory(path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (yield stat(path)).isDirectory(); | ||
}); | ||
/** | ||
* Is path a directory | ||
*/ | ||
async function isDirectory(path) { | ||
return (await stat(path)).isDirectory(); | ||
} | ||
exports.isDirectory = isDirectory; | ||
function getReadStream(path, options) { | ||
/** | ||
* Wrapper to fs function | ||
*/ | ||
function getReadStream(path, options = {}) { | ||
return fs.createReadStream(path, options); | ||
} | ||
exports.getReadStream = getReadStream; | ||
function getWriteStream(path, options) { | ||
/** | ||
* Wrapper to fs function | ||
*/ | ||
function getWriteStream(path, options = {}) { | ||
return fs.createWriteStream(path, options); | ||
@@ -102,0 +143,0 @@ } |
100
index.ts
import * as fs from 'fs'; | ||
import {cb2promise, asyncEach} from '@meteor-it/async'; | ||
import {asyncEach} from '@meteor-it/utils'; | ||
import {sep} from 'path'; | ||
import {promisify} from 'util'; | ||
// TODO: Deprecate (Because node now supports promised fs) | ||
/** | ||
* Returns true if path is a valid data url | ||
* @param path path | ||
*/ | ||
function isDataUrl(path:string):boolean{ | ||
return /^data:.+\/.+;base64,/.test(path.substr(0,268)) | ||
} | ||
export interface IParsedDataUrl{ | ||
mime: string, | ||
data: Buffer | ||
} | ||
/** | ||
* Returns mime and data of dataurl | ||
* @param path Data url | ||
*/ | ||
function parseDataUrl(path:string):IParsedDataUrl{ | ||
return { | ||
mime: path.slice(5,path.indexOf(';')), | ||
data: Buffer.from(path.slice(path.indexOf(',')+1),'base64') | ||
} | ||
} | ||
/** | ||
* Get all files in directory | ||
*/ | ||
export async function readDir (dir) { | ||
return cb2promise(fs.readdir)(dir); | ||
export async function readDir (dir:string) { | ||
return await promisify(fs.readdir)(dir); | ||
} | ||
/** | ||
* Read file | ||
* Read file or parse data url | ||
* @param file Path to file to read | ||
*/ | ||
export async function readFile (file) { | ||
return cb2promise(fs.readFile)(file); | ||
export async function readFile (file:string):Promise<Buffer> { | ||
if(isDataUrl(file)) | ||
return parseDataUrl(file).data; | ||
return await promisify(fs.readFile)(file); | ||
} | ||
export async function stat (file) { | ||
return cb2promise(fs.stat)(file); | ||
export async function stat (file:string):Promise<fs.Stats>{ | ||
return await promisify(fs.stat)(file); | ||
} | ||
export async function open (file:string,mode:string,access:string):Promise<number>{ | ||
return await promisify(fs.open)(file,mode,access); | ||
} | ||
export async function read(fd:number, buffer:Buffer, offset:number, length:number, position:number){ | ||
return await promisify(fs.read)(fd, buffer, offset, length, position); | ||
} | ||
export async function close(fd:number){ | ||
return await promisify(fs.close)(fd); | ||
} | ||
/** | ||
* Write text to file | ||
*/ | ||
export function writeFile (filename, text) { | ||
return cb2promise(fs.writeFile)(filename, text); | ||
export async function writeFile (filename:string, text:string|Buffer) { | ||
return await promisify(fs.writeFile)(filename, text); | ||
} | ||
@@ -31,7 +72,8 @@ | ||
* Walk directory | ||
* @param cb if provided, found files will returned realtime. If not - function will return all found files | ||
* @param dir Directory to walk | ||
* @param cb If provided, found files will returned realtime. If not - function will return all found files | ||
*/ | ||
export async function walkDir (dir, cb) { | ||
export async function walkDir (dir:string, cb?:(file:string,dir:string)=>void):Promise<string[]|null> { | ||
if (!await exists(dir)) { throw new Error('No such file or directory: ' + dir); } | ||
let returnValue; | ||
let returnValue:string[]; | ||
let shouldReturn = false; | ||
@@ -41,8 +83,8 @@ if (!cb) { | ||
shouldReturn = true; | ||
cb = (file, dir) => { | ||
cb = (file:string, dir:string) => { | ||
returnValue.push(dir + sep + file); | ||
}; | ||
} | ||
let dirList = []; | ||
await asyncEach(await readDir(dir), async(file) => { | ||
let dirList:string[] = []; | ||
await asyncEach(await readDir(dir), async(file:string) => { | ||
let path = dir + sep + file; | ||
@@ -55,7 +97,9 @@ if (await isFile(path)) { | ||
}); | ||
await asyncEach(dirList, async(dirLevelDown) => { | ||
await asyncEach(dirList, async(dirLevelDown:string) => { | ||
await walkDir(dir + sep + dirLevelDown, cb); | ||
}); | ||
if (shouldReturn) { return returnValue.sort(); } | ||
return; | ||
if (shouldReturn) { | ||
return returnValue.sort(); | ||
} | ||
return null; | ||
} | ||
@@ -66,7 +110,8 @@ | ||
*/ | ||
export async function exists (file) { | ||
export async function exists (file:string):Promise<boolean> { | ||
try { | ||
let result = await cb2promise(fs.access)(file, fs.constants.F_OK); | ||
if (result === undefined) { return true; } // Because only "err" field is returned if not exists | ||
} catch (e) { | ||
let result = await promisify(fs.access)(file, fs.constants.F_OK); | ||
return result === undefined; | ||
} catch (e) { | ||
// Because only "err" field is returned if not exists | ||
return false; | ||
@@ -78,4 +123,5 @@ } | ||
* Is path a file | ||
* @param path path to test | ||
*/ | ||
export async function isFile (path) { | ||
export async function isFile (path:string): Promise<boolean> { | ||
return (await stat(path)).isFile(); | ||
@@ -86,3 +132,3 @@ } | ||
*/ | ||
export async function isDirectory (path) { | ||
export async function isDirectory (path:string): Promise<boolean> { | ||
return (await stat(path)).isDirectory(); | ||
@@ -94,3 +140,3 @@ } | ||
*/ | ||
export function getReadStream (path, options) { | ||
export function getReadStream (path:string, options = {}) { | ||
return fs.createReadStream(path, options); | ||
@@ -102,4 +148,4 @@ } | ||
*/ | ||
export function getWriteStream (path, options) { | ||
export function getWriteStream (path:string, options={}) { | ||
return fs.createWriteStream(path, options); | ||
} |
{ | ||
"name": "@meteor-it/fs", | ||
"version": "1.0.0", | ||
"description": "Async filesystem", | ||
"version": "1.0.2", | ||
"description": "Wrappers for native fs module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Creeplays/fs.git" | ||
}, | ||
"keywords": [ | ||
"async", | ||
"fs", | ||
"file", | ||
"filesystem", | ||
"promise", | ||
"await" | ||
"meteor-it", | ||
"fs" | ||
], | ||
"author": "f6cf", | ||
"author": "Yaroslaw Bolyukin <iam@f6cf.pw> (http://f6cf.pw/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Creeplays/fs/issues" | ||
"dependencies": { | ||
"@meteor-it/utils": "^1.3.2" | ||
}, | ||
"homepage": "https://github.com/Creeplays/fs#readme", | ||
"dependencies": { | ||
"@meteor-it/utils": "latest" | ||
} | ||
"gitHead": "7d0b20950a168a6ae5dbbe7dd47629af7ce02ea2" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
14962
7
326
1
1
28
2
2
+ Added@meteor-it/utils@1.3.33(transitive)
- Removed@meteor-it/utils@2.1.0(transitive)
Updated@meteor-it/utils@^1.3.2