UNPKG

elm-spa

Version:
80 lines (79 loc) 3.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.read = exports.mkdir = exports.copyFile = exports.copy = exports.exists = exports.scan = exports.remove = exports.create = void 0; const fs_1 = require("fs"); const fs_2 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); /** * Create a new file, creating the containing folder if missing. * @param filepath - the absolute path of the file to create * @param contents - the raw string contents of the file */ exports.create = async (filepath, contents) => { await ensureFolderExists(filepath); return fs_1.promises.writeFile(filepath, contents, { encoding: 'utf8' }); }; /** * Removes a file or folder at the given path. * @param filepath - the path of the file or folder to remove */ exports.remove = async (filepath) => { const stats = await fs_1.promises.stat(filepath); return stats.isFile() ? fs_1.promises.unlink(filepath) : fs_1.promises.rmdir(filepath, { recursive: true }); }; exports.scan = async (dir, extension = '.elm') => { const doesExist = await exports.exists(dir); if (!doesExist) return []; const items = await ls(dir); const [folders, files] = await Promise.all([ keepFolders(items), items.filter(f => f.endsWith(extension)) ]); const listOfFiles = await Promise.all(folders.map(f => exports.scan(f, extension))); const nestedFiles = listOfFiles.reduce((a, b) => a.concat(b), []); return files.concat(nestedFiles); }; const ls = (dir) => fs_1.promises.readdir(dir) .then(data => data.map(p => path_1.default.join(dir, p))); const isDirectory = (dir) => fs_1.promises.lstat(dir).then(data => data.isDirectory()).catch(_ => false); const keepFolders = async (files) => { const possibleFolders = await Promise.all(files.map(f => isDirectory(f).then(isDir => isDir ? f : undefined))); return possibleFolders.filter(a => a !== undefined); }; exports.exists = (filepath) => fs_1.promises.stat(filepath) .then(_ => true) .catch(_ => false); /** * Copy the file or folder at the given path. * @param filepath - the path of the file or folder to copy */ exports.copy = (src, dest) => { const exists = fs_2.default.existsSync(src); const stats = exists && fs_2.default.statSync(src); if (stats && stats.isDirectory()) { try { fs_2.default.mkdirSync(dest, { recursive: true }); } catch (_) { } fs_2.default.readdirSync(src).forEach(child => exports.copy(path_1.default.join(src, child), path_1.default.join(dest, child))); } else { fs_2.default.copyFileSync(src, dest); } }; exports.copyFile = async (src, dest) => { await ensureFolderExists(dest); return fs_1.promises.copyFile(src, dest); }; const ensureFolderExists = async (filepath) => { const folder = filepath.split(path_1.default.sep).slice(0, -1).join(path_1.default.sep); return fs_1.promises.mkdir(folder, { recursive: true }); }; exports.mkdir = (folder) => fs_1.promises.mkdir(folder, { recursive: true }); exports.read = async (path) => fs_1.promises.readFile(path, { encoding: 'utf-8' });