UNPKG

@practica/create-node-app

Version:

Create Node.js app that is packed with best practices AND strive for simplicity

40 lines (39 loc) 1.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createUniqueFolder = exports.doesFileContainPhrase = exports.doesFolderExistInPath = void 0; const node_fs_1 = require("node:fs"); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); function getRandomFolderPath(basePath) { const randomFolderName = new Date().getTime().toString(); return path_1.default.join(basePath, randomFolderName); } async function doesFolderExistInPath(path) { return await fs_extra_1.default.pathExists(path); } exports.doesFolderExistInPath = doesFolderExistInPath; async function doesFileContainPhrase(filePath, phrase) { const readStream = (0, node_fs_1.createReadStream)(filePath, { encoding: "utf-8" }); for await (const chunkOfData of readStream) { const chunkAsString = chunkOfData + ""; if (chunkAsString.includes(phrase)) { return true; } } return false; } exports.doesFileContainPhrase = doesFileContainPhrase; const createUniqueFolder = async () => { const testOutputFolder = path_1.default.join(os_1.default.tmpdir(), "practica-tests-output"); const doesPathExist = await fs_extra_1.default.pathExists(testOutputFolder); if (!doesPathExist) { await fs_extra_1.default.mkdir(testOutputFolder, { recursive: true }); } const uniqueTestFolderPath = await fs_extra_1.default.mkdtemp(`${testOutputFolder}${path_1.default.sep}`); return uniqueTestFolderPath; }; exports.createUniqueFolder = createUniqueFolder;