UNPKG

@almaclaine/fs-utils

Version:
115 lines (109 loc) 3.45 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var fs = require('fs'); var path = require('path'); const doesNotExistError = (path) => `Provided path ${path} does not exist: ${path}.`; const alreadyExistsError = (path) => `Provided file/directory already exists: ${path}.`; const isNotDirectoryError = (path) => `Provided path ${path} is not a directory.`; const isDirectoryError = (path) => `Provided path ${path} is a directory.`; const isNotJsonFilError = (path) => `Provided path ${path} is not a json file. Extension must be .json`; function isHidden(path$1) { const pathSplit = path$1.split(path.sep); return pathSplit[pathSplit.length - 1][0] === '.'; } function createReadStream(path) { if (!fs.existsSync(path)) { throw Error(doesNotExistError(path)); } return fs.createReadStream(path); } const makeCwdRelPath = (...files) => path.join(process.cwd(), ...files); function isDirectorySync(path) { if (!fs.existsSync(path)) return false; return fs.lstatSync(path).isDirectory(); } function readDirectorySync(path) { if (!isDirectorySync(path)) { throw Error(isNotDirectoryError(path)); } return fs.readdirSync(path); } function makeDirectorySync(path) { if (fs.existsSync(path)) { throw Error(alreadyExistsError(path)); } fs.mkdirSync(path); } function importJson(path) { if (!fs.existsSync(path)) { throw Error(doesNotExistError(path)); } if (!/.*\.json/.test(path)) { throw Error(isNotJsonFilError(path)); } return require(path); } function readFileAsStringSync(path) { if (!fs.existsSync(path)) { throw Error(doesNotExistError(path)); } if (isDirectorySync(path)) { throw Error(isNotDirectoryError(path)); } return fs.readFileSync(path, 'utf-8'); } function readFileSync(path) { if (!fs.existsSync(path)) { throw Error(doesNotExistError(path)); } if (isDirectorySync(path)) { throw Error(isNotDirectoryError(path)); } return fs.readFileSync(path); } function writeFileAsStringSync(path, data) { if (isDirectorySync(path)) { throw Error(isDirectoryError(path)); } fs.writeFileSync(path, data, 'utf-8'); } Object.defineProperty(exports, 'copyFileSync', { enumerable: true, get: function () { return fs.copyFileSync; } }); Object.defineProperty(exports, 'existsSync', { enumerable: true, get: function () { return fs.existsSync; } }); Object.defineProperty(exports, 'join', { enumerable: true, get: function () { return path.join; } }); Object.defineProperty(exports, 'resolve', { enumerable: true, get: function () { return path.resolve; } }); exports.alreadyExistsError = alreadyExistsError; exports.createReadStream = createReadStream; exports.doesNotExistError = doesNotExistError; exports.importJson = importJson; exports.isDirectoryError = isDirectoryError; exports.isDirectorySync = isDirectorySync; exports.isHidden = isHidden; exports.isNotDirectoryError = isNotDirectoryError; exports.isNotJsonFilError = isNotJsonFilError; exports.makeCwdRelPath = makeCwdRelPath; exports.makeDirectorySync = makeDirectorySync; exports.readDirectorySync = readDirectorySync; exports.readFileAsStringSync = readFileAsStringSync; exports.readFileSync = readFileSync; exports.writeFileAsStringSync = writeFileAsStringSync;