@sinclair/hammer
Version:
Build Tool for Browser and Node Applications
112 lines (105 loc) • 4.69 kB
JavaScript
;
/*--------------------------------------------------------------------------
MIT License
Copyright (c) Hammer 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.folder_delete = exports.common_readdir_flatmap = exports.common_readdir = exports.folder_exists = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const util_1 = require("util");
const readdirAsync = (0, util_1.promisify)(fs_1.readdir);
const rmdirAsync = (0, util_1.promisify)(fs_1.rmdir);
const unlinkAsync = (0, util_1.promisify)(fs_1.unlink);
const statAsync = (0, util_1.promisify)(fs_1.stat);
function compare(left, right) {
if (left > right) {
return 1;
}
if (left < right) {
return -1;
}
return 0;
}
/** Returns true if this path is a folder that exists. */
async function folder_exists(folder) {
if (!(0, fs_1.existsSync)(folder))
return false;
const stat = await statAsync(folder);
return stat.isDirectory();
}
exports.folder_exists = folder_exists;
/**
* A specialized readdir that returns the {folder, path, stat} for the
* given folder. The 'folder' component returns the absolute path for
* the given folder passed to this function, and the path is the path
* down level from the folder. Thus join(folder, path) yields the full
* path of the item. The stat is a node stat object for the item
* indicating either file or folder.
*/
async function common_readdir(folder) {
const items = await Promise.all((await readdirAsync(folder)).map(async (path) => ({
path: (0, path_1.basename)(path),
folder: (0, path_1.resolve)(folder),
stat: await statAsync((0, path_1.join)(folder, (0, path_1.basename)(path))),
})));
const folders = items.filter((item) => item.stat.isDirectory()).sort((a, b) => compare(a.path, b.path));
const files = items.filter((item) => item.stat.isFile()).sort((a, b) => compare(a.path, b.path));
return [...folders, ...files];
}
exports.common_readdir = common_readdir;
async function read_folder(folder) {
const items = [];
for (const item of await common_readdir(folder)) {
if (item.stat.isDirectory()) {
items.push(item);
items.push(...(await read_folder((0, path_1.join)(item.folder, item.path))));
}
if (item.stat.isFile()) {
items.push(item);
}
}
return items;
}
/** Returns a recursive list of a folders contents. */
async function common_readdir_flatmap(folder) {
return (await read_folder(folder)).map((item) => ({
folder: (0, path_1.resolve)(folder),
path: (0, path_1.relative)(folder, (0, path_1.join)(item.folder, item.path)),
stat: item.stat,
}));
}
exports.common_readdir_flatmap = common_readdir_flatmap;
/** Deletes this folder. If not exists, then no action. */
async function folder_delete(folder) {
if (await folder_exists(folder)) {
const items = await common_readdir_flatmap(folder);
const folders = items.filter((item) => item.stat.isDirectory());
const files = items.filter((item) => item.stat.isFile());
for (const item of files) {
const target = (0, path_1.join)(item.folder, item.path);
await unlinkAsync(target);
}
for (const item of folders.reverse()) {
const target = (0, path_1.join)(item.folder, item.path);
await rmdirAsync(target);
}
await rmdirAsync(folder);
}
}
exports.folder_delete = folder_delete;