@universal-packages/fs-utils
Advanced tools
Comparing version 1.0.3 to 1.1.0
{ | ||
"name": "@universal-packages/fs-utils", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Extended functionality for fs", | ||
@@ -13,22 +13,19 @@ "author": "David De Anda <david@universal-packages.com> (https://github.com/universal-packages)", | ||
"test": "jest --watch", | ||
"test:full": "jest --coverage --verbose", | ||
"test:coverage": "jest --coverage", | ||
"test:clear": "jest --clearCache", | ||
"format": "prettier --write \"./{src,tests}/**/*.{ts,tsx,js,jsx,json}\"" | ||
"format": "prettier --write \"./{src,tests}/**/*.{ts,tsx,js,jsx,json}\"", | ||
"update-universal-dependencies": "umaintenance update-universal-dependencies" | ||
}, | ||
"devDependencies": { | ||
"@trivago/prettier-plugin-sort-imports": "^4.1.1", | ||
"@types/jest": "^28.1.0", | ||
"@types/node": "^17.0.39", | ||
"jest": "^28.1.0", | ||
"jest-circus": "^28.1.0", | ||
"prettier": "^2.8.7", | ||
"ts-jest": "^28.0.4", | ||
"typescript": "^4.7.3" | ||
"@trivago/prettier-plugin-sort-imports": "^4.3.0", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^18.11.9", | ||
"@universal-packages/maintenance": "^1.5.1", | ||
"jest": "^29.7.0", | ||
"prettier": "^3.0.3", | ||
"ts-jest": "^29.1.2", | ||
"typescript": "^5.4.3" | ||
}, | ||
"jest": { | ||
"testRunner": "jest-circus/runner", | ||
"transform": { | ||
"^.+\\.(ts|tsx)$": "ts-jest" | ||
}, | ||
"testRegex": "(/tests/.*\\.test\\.ts?)$", | ||
"preset": "ts-jest", | ||
"collectCoverageFrom": [ | ||
@@ -38,6 +35,3 @@ "src/**/*.ts" | ||
"setupFilesAfterEnv": [ | ||
"<rootDir>/tests/setupTests.ts" | ||
], | ||
"watchPathIgnorePatterns": [ | ||
"<rootDir>/tmp" | ||
"<rootDir>/tests/setup.ts" | ||
] | ||
@@ -50,2 +44,5 @@ }, | ||
"trailingComma": "none", | ||
"plugins": [ | ||
"@trivago/prettier-plugin-sort-imports" | ||
], | ||
"importOrder": [ | ||
@@ -52,0 +49,0 @@ "^[./]" |
@@ -19,3 +19,3 @@ # fs Utils | ||
Checks if a string is a valid path to a directory and expands it. | ||
Checks if a string is a valid path to a directory and expands it. It throw verbose errors depending on why the directory is not valid. | ||
@@ -32,4 +32,2 @@ ```js | ||
Any other case is thrown as an error. | ||
```js | ||
@@ -48,5 +46,19 @@ import { checkDirectory } from '@universal-packages/fs-utils' | ||
#### **`quickCheckDirectory(location: String)`** | ||
It does the same as `checkDirectory` but returns false if the directory is not valid instead of throwing an error. | ||
```js | ||
import { quickCheckDirectory } from '@universal-packages/fs-utils' | ||
const finalPath = quickCheckDirectory('./src/fake') | ||
console.log(finalPath) | ||
// > false | ||
``` | ||
#### **`checkFile(location: String)`** | ||
Checks if a string is a valid path to a file and expands it. | ||
Checks if a string is a valid path to a file and expands it. It throw verbose errors depending on why the file is not valid. | ||
@@ -63,4 +75,2 @@ ```js | ||
Any other case is thrown as an error. | ||
```js | ||
@@ -79,2 +89,16 @@ import { checkFile } from '@universal-packages/fs-utils' | ||
#### **`quickCheckFile(location: String)`** | ||
It does the same as `checkFile` but throw verbose errors depending on why the file is not valid. | ||
```js | ||
import { quickCheckFile } from '@universal-packages/fs-utils' | ||
const finalPath = quickCheckFile('./src/README.md') | ||
console.log(finalPath) | ||
// > /Users/david/project/src/README.md | ||
``` | ||
#### **`ensureDirectory(location: String)`** | ||
@@ -105,3 +129,3 @@ | ||
// > Error Directory "/new" is an invalid path or greater permisons are reaquired | ||
// > Error Directory "/new" is an invalid path or greater permissions are required | ||
``` | ||
@@ -134,3 +158,3 @@ | ||
// > Error File location "/file.rb" is an invalid path or greater permisons are reaquired | ||
// > Error File location "/file.rb" is an invalid path or greater permissions are required | ||
``` | ||
@@ -137,0 +161,0 @@ |
export declare const IS_WINDOWS: boolean; | ||
export declare const HOME_DIR: string; | ||
/** Checks if a string is a valid path to a directory and expands it */ | ||
export declare function checkDirectory(location: string): string; | ||
/** Checks if a string is a valid path to a file and expands it */ | ||
export declare function quickCheckDirectory(location: string): string | false; | ||
export declare function checkFile(location: string): string; | ||
/** Checks and expand a path and tries to create the directory if the check fails */ | ||
export declare function quickCheckFile(location: string): string | false; | ||
export declare function ensureDirectory(location: string): string; | ||
/** Checks and expand a path and tries to create an empty file if the check fails */ | ||
export declare function ensureFile(location: string): string; | ||
/** Tries to expand a path by resolving the tilde and resolving to an absolute path */ | ||
export declare function expandPath(location: string): string; |
23
utils.js
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.expandPath = exports.ensureFile = exports.ensureDirectory = exports.checkFile = exports.checkDirectory = exports.HOME_DIR = exports.IS_WINDOWS = void 0; | ||
exports.expandPath = exports.ensureFile = exports.ensureDirectory = exports.quickCheckFile = exports.checkFile = exports.quickCheckDirectory = exports.checkDirectory = exports.HOME_DIR = exports.IS_WINDOWS = void 0; | ||
const fs_1 = __importDefault(require("fs")); | ||
@@ -13,3 +13,2 @@ const os_1 = __importDefault(require("os")); | ||
exports.HOME_DIR = os_1.default.homedir(); | ||
/** Checks if a string is a valid path to a directory and expands it */ | ||
function checkDirectory(location) { | ||
@@ -26,3 +25,10 @@ const finalLocation = expandPath(location); | ||
exports.checkDirectory = checkDirectory; | ||
/** Checks if a string is a valid path to a file and expands it */ | ||
function quickCheckDirectory(location) { | ||
const finalLocation = expandPath(location); | ||
if (!fs_1.default.existsSync(finalLocation) || !fs_1.default.lstatSync(finalLocation).isDirectory()) { | ||
return false; | ||
} | ||
return finalLocation; | ||
} | ||
exports.quickCheckDirectory = quickCheckDirectory; | ||
function checkFile(location) { | ||
@@ -39,3 +45,10 @@ const finalLocation = expandPath(location); | ||
exports.checkFile = checkFile; | ||
/** Checks and expand a path and tries to create the directory if the check fails */ | ||
function quickCheckFile(location) { | ||
const finalLocation = expandPath(location); | ||
if (!fs_1.default.existsSync(finalLocation) || fs_1.default.lstatSync(finalLocation).isDirectory()) { | ||
return false; | ||
} | ||
return finalLocation; | ||
} | ||
exports.quickCheckFile = quickCheckFile; | ||
function ensureDirectory(location) { | ||
@@ -54,3 +67,2 @@ const finalLocation = expandPath(location); | ||
exports.ensureDirectory = ensureDirectory; | ||
/** Checks and expand a path and tries to create an empty file if the check fails */ | ||
function ensureFile(location) { | ||
@@ -67,3 +79,2 @@ const finalLocation = expandPath(location); | ||
exports.ensureFile = ensureFile; | ||
/** Tries to expand a path by resolving the tilde and resolving to an absolute path */ | ||
function expandPath(location) { | ||
@@ -70,0 +81,0 @@ const expanded = location && !exports.IS_WINDOWS && exports.HOME_DIR ? location.replace(/^~(?=$|\/|\\)/, exports.HOME_DIR) : location; |
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
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
14501
105
182