pathname-ts
Version:
A partial implementation of Ruby's Pathname class for Node.js.
185 lines (179 loc) • 5.38 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var path = require('path');
var promises = require('fs/promises');
var fs = require('fs');
const asyncFilter = async (arr, predicate) => {
const results = await Promise.all(arr.map(predicate));
return arr.filter((_v, index) => results[index]);
};
/**
* - https://ruby-doc.org/stdlib-2.7.0/libdoc/pathname/rdoc/Pathname.html
* - https://github.com/ruby/pathname/blob/master/lib/pathname.rb
*/
class Pathname {
path;
realpath;
constructor(path) {
if (typeof path === "string" || path instanceof String) {
this.path = path;
}
else {
this.path = path.toString();
}
this.realpath = this.absolutePath();
}
toJSON() { return this.realpath; }
toString(os = "unix") {
if (os === "windows") {
return this.realpath.replaceAll("/", "\\\\");
}
return this.realpath;
}
isAbsolute() {
return path.isAbsolute(this.path);
}
absolutePath() {
if (this.isAbsolute()) {
return this.path;
}
else {
return path.resolve(process.cwd(), this.path);
}
}
join(...segments) {
const newPath = path.resolve(this.realpath, ...segments);
return new Pathname(newPath);
}
extname() {
return path.extname(this.realpath);
}
basename(extension) {
return path.basename(this.realpath, extension);
}
dirname() {
return path.dirname(this.realpath);
}
// public async rename(newPath: string): Promise<IPathname> {
// await rename(this.absolutePath(), newPath)
// const newFullPath = join(this.dirname(), newPath)
// return new Pathname(newFullPath)
// }
parent() {
const parentPath = path.resolve(this.realpath, "..");
return new Pathname(parentPath);
}
async doesExist() {
try {
const _ = await promises.access(this.realpath, fs.constants.F_OK);
return true;
}
catch (error) {
return false;
}
}
async isFile() {
try {
// if (!(await this.doesExist())) { return false }
const stat = await this.stat();
return stat.isFile();
}
catch (error) {
throw error;
}
}
async isDirectory() {
try {
if (!(await this.doesExist())) {
return false;
}
const stat = await this.stat();
return stat.isDirectory();
}
catch (error) {
throw error;
}
}
async stat() {
try {
return await promises.stat(this.realpath);
}
catch (error) {
// return null
throw error;
}
}
async read() {
try {
const fileContent = await promises.readFile(this.realpath);
return fileContent.toString();
}
catch (error) {
throw error;
}
}
async readJSON() {
try {
const fileContentString = await this.read();
const json = JSON.parse(fileContentString);
return json;
}
catch (error) {
throw error;
}
}
async write(data, options = {}) {
try {
await promises.writeFile(this.realpath, data, options);
}
catch (error) {
throw error;
}
}
async writeJSON(data, options = {}) {
try {
const json = JSON.stringify(data, null, 4);
await promises.writeFile(this.realpath, json, options);
}
catch (error) {
throw error;
}
}
async children(type = "all") {
try {
const children = await promises.readdir(this.realpath);
const childPaths = children.map(x => this.join(x));
if (type === "all") {
return childPaths;
}
if (type === "files") {
const filePaths = await asyncFilter(childPaths, async (p) => { return await p.isFile(); });
// const filePaths = []
// for (const path of childPaths) {
// const isFile = await path.isFile()
// if (isFile) {
// filePaths.push(path)
// }
// }
return filePaths;
}
if (type === "folders") {
const folderPaths = await asyncFilter(childPaths, async (p) => { return await p.isDirectory(); });
// const folderPaths = []
// for (const path of childPaths) {
// const isDir = await path.isDirectory()
// if (isDir) {
// folderPaths.push(path)
// }
// }
return folderPaths;
}
return childPaths;
}
catch (error) {
throw error;
}
}
}
exports.Pathname = Pathname;
//# sourceMappingURL=index.js.map