@bemoje/fs 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +1 -1
- package/dist/index.esm.js +1 -1
- package/package.json +2 -3
package/dist/index.cjs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @bemoje/fs v0.0.
|
|
2
|
+
* @bemoje/fs v0.0.6
|
|
3
3
|
* (c) Benjamin Møller Jensen
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/"use strict";var string=require("@bemoje/string"),path=require("path"),fs=require("fs"),child_process=require("child_process"),extract=require("extract-zip");function absoluteToRelativePath(absolute,workingDirectory=process.cwd()){return path.normalize(absolute).replace(new RegExp(string.regexEscapeString(path.normalize(workingDirectory)+path.sep),"i"),"")}function __awaiter(thisArg,_arguments,P,generator){function adopt(value){return value instanceof P?value:new P(function(resolve){resolve(value)})}return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):adopt(result.value).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})}"function"==typeof SuppressedError?SuppressedError:function(error,suppressed,message){var e=new Error(message);return e.name="SuppressedError",e.error=error,e.suppressed=suppressed,e};function createDirectory(dirpath){return __awaiter(this,void 0,void 0,function*(){return yield fs.promises.mkdir(dirpath,{recursive:!0}),dirpath})}function appendLineToFile(filepath,line,linebreakBefore=!1){return __awaiter(this,void 0,void 0,function*(){fs.existsSync(filepath)||(yield createDirectory(path.dirname(filepath)),yield fs.promises.writeFile(filepath,"")),yield fs.promises.appendFile(filepath,linebreakBefore?"\n"+line:line+"\n")})}function cleanDirectorySync(dirpath,predicate){for(const filename of fs.readdirSync(dirpath)){const filepath=path.join(dirpath,filename),stat=fs.statSync(filepath);predicate(filepath,stat)&&fs.unlinkSync(filepath)}}function createDirectorySync(dirpath){return fs.mkdirSync(dirpath,{recursive:!0}),dirpath}function normalizeFileExtension(ext){if(""===ext||"."===ext)return"";if(/[<>"|?*:]/g.test(ext))throw new Error(`Illegal characters in file extension: ${ext} | Illegal characters are: <>"|?:*`);return ext.includes(".")?ext.substring(ext.lastIndexOf(".")):"."+ext}function createFileExtensionFilter(...fileExtensions){return fileExtensions.length?filepath=>{for(const ext of fileExtensions.map(normalizeFileExtension))if(path.extname(filepath)===ext)return!0;return!1}:()=>!0}function cwdpath(...paths){return path.join(process.cwd(),...paths)}function deleteDirectory(dirpath){return __awaiter(this,void 0,void 0,function*(){yield fs.promises.rm(dirpath,{recursive:!0,force:!0})})}function deleteDirectorySafe(dirpath){return __awaiter(this,void 0,void 0,function*(){fs.existsSync(dirpath)&&(yield fs.promises.rm(dirpath,{recursive:!0,force:!0}))})}function deleteDirectorySafeSync(dirpath){return fs.existsSync(dirpath)?(fs.rmSync(dirpath,{recursive:!0,force:!0}),dirpath):dirpath}function deleteDirectorySync(dirpath){return fs.rmSync(dirpath,{recursive:!0,force:!0}),dirpath}function ensureValidWindowsPath(path,options){const throwOrFalse=msg=>{if(options&&!0===options.assert)throw new Error(`Invalid windows path. ${msg} | input received: ${path}`);return!1};if(0===path.length)return throwOrFalse("Path string is length 0.");if(-1!==path.indexOf("/")&&-1!==path.indexOf("\\"))return throwOrFalse("Path contains both backslash and forward slash.");const maxLength=(options&&options.extendedMaxLength?32767:260)-12;if(path.length>maxLength)return throwOrFalse(`Maximum length of ${maxLength} exceeded`);let noDriveLetter=path;return /^\w:(\\|\/)/g.test(path)&&(noDriveLetter=path.substring(2)),/[<>"|?*:]/g.test(noDriveLetter)?throwOrFalse(`Illegal characters in: ${path} | Illegal characters are: <>"|?:*`):!/(\\|\/)(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])((\\|\/)|$)/g.test(path)||throwOrFalse("Illegal name in path string")}function isWindows(){return process.platform.includes("win")}function getDiskDrivesWindows(){if(!isWindows())throw new Error("This function is only available on Windows.");return child_process.execSync("wmic logicaldisk get name").toString().split("\n").map(s=>s.trim()).filter(s=>/^[a-z]:/i.test(s)).map(s=>s.substring(0,2)+path.sep)}function getHomeDirectory(){const home=path.resolve(process.env.HOME||process.env.USERPROFILE||"");if(!home)throw new Error("Home directory not found");return home}function getDownloadsDirectory(){return path.join(getHomeDirectory(),"Downloads")}function getLastMofifiedFileIn(dirpath){return __awaiter(this,void 0,void 0,function*(){const children=yield fs.promises.readdir(dirpath),filtered=children.map(fname=>{const fpath=path.join(dirpath,fname),stat=fs.statSync(fpath);return[fpath,stat.mtimeMs,stat.isFile()]}).filter(a=>a[2]),sorted=filtered.sort((a,b)=>b[1]-a[1]);return sorted[0][0]})}function getRootDir(){return path.parse(process.cwd()).root}function readJsonFile(filepath){return __awaiter(this,void 0,void 0,function*(){return JSON.parse(yield fs.promises.readFile(filepath,"utf8"))})}function readJsonFileSync(filepath){return JSON.parse(fs.readFileSync(filepath,"utf8"))}function readPackageJson(projectRoot=process.cwd()){const filepath=path.join(projectRoot,"package.json");return fs.existsSync(filepath)?readJsonFileSync(filepath):void 0}function removeIllegalFilenameCharacters(string){return string.replace(/[/\\?%*:|"<>]/g,"")}function unzipFile(filepath,outdir){return __awaiter(this,void 0,void 0,function*(){yield extract(filepath,{dir:outdir})})}function unzipMergeFiles(source,target){return __awaiter(this,void 0,void 0,function*(){"string"==typeof source&&(source=fs.readdirSync(source).map(filename=>path.join(source,filename)));for(const filepath of source){const stat=yield fs.promises.stat(filepath);stat.isDirectory()||filepath.endsWith(".zip")&&(yield extract(filepath,{dir:target}))}})}function wipeDirectorySafe(dirpath){return __awaiter(this,void 0,void 0,function*(){yield deleteDirectorySafe(dirpath),yield createDirectory(dirpath)})}function writeJsonFile(filepath,data,pretty=!1){return __awaiter(this,void 0,void 0,function*(){yield fs.promises.writeFile(filepath,JSON.stringify(data,null,pretty?2:void 0))})}function writeJsonFileSync(filepath,data,pretty=!1){fs.writeFileSync(filepath,JSON.stringify(data,null,pretty?2:void 0))}exports.absoluteToRelativePath=absoluteToRelativePath,exports.appendLineToFile=appendLineToFile,exports.cleanDirectorySync=cleanDirectorySync,exports.createDirectory=createDirectory,exports.createDirectorySync=createDirectorySync,exports.createFileExtensionFilter=createFileExtensionFilter,exports.cwdpath=cwdpath,exports.deleteDirectory=deleteDirectory,exports.deleteDirectorySafe=deleteDirectorySafe,exports.deleteDirectorySafeSync=deleteDirectorySafeSync,exports.deleteDirectorySync=deleteDirectorySync,exports.ensureValidWindowsPath=ensureValidWindowsPath,exports.getDiskDrivesWindows=getDiskDrivesWindows,exports.getDownloadsDirectory=getDownloadsDirectory,exports.getHomeDirectory=getHomeDirectory,exports.getLastMofifiedFileIn=getLastMofifiedFileIn,exports.getRootDir=getRootDir,exports.isWindows=isWindows,exports.normalizeFileExtension=normalizeFileExtension,exports.readJsonFile=readJsonFile,exports.readJsonFileSync=readJsonFileSync,exports.readPackageJson=readPackageJson,exports.removeIllegalFilenameCharacters=removeIllegalFilenameCharacters,exports.unzipFile=unzipFile,exports.unzipMergeFiles=unzipMergeFiles,exports.wipeDirectorySafe=wipeDirectorySafe,exports.writeJsonFile=writeJsonFile,exports.writeJsonFileSync=writeJsonFileSync;
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @bemoje/fs v0.0.
|
|
2
|
+
* @bemoje/fs v0.0.6
|
|
3
3
|
* (c) Benjamin Møller Jensen
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/import{regexEscapeString}from"@bemoje/string";import path from"path";import fs from"fs";import{execSync}from"child_process";import extract from"extract-zip";function absoluteToRelativePath(absolute,workingDirectory=process.cwd()){return path.normalize(absolute).replace(new RegExp(regexEscapeString(path.normalize(workingDirectory)+path.sep),"i"),"")}function __awaiter(thisArg,_arguments,P,generator){function adopt(value){return value instanceof P?value:new P(function(resolve){resolve(value)})}return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):adopt(result.value).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})}"function"==typeof SuppressedError?SuppressedError:function(error,suppressed,message){var e=new Error(message);return e.name="SuppressedError",e.error=error,e.suppressed=suppressed,e};function createDirectory(dirpath){return __awaiter(this,void 0,void 0,function*(){return yield fs.promises.mkdir(dirpath,{recursive:!0}),dirpath})}function appendLineToFile(filepath,line,linebreakBefore=!1){return __awaiter(this,void 0,void 0,function*(){fs.existsSync(filepath)||(yield createDirectory(path.dirname(filepath)),yield fs.promises.writeFile(filepath,"")),yield fs.promises.appendFile(filepath,linebreakBefore?"\n"+line:line+"\n")})}function cleanDirectorySync(dirpath,predicate){for(const filename of fs.readdirSync(dirpath)){const filepath=path.join(dirpath,filename),stat=fs.statSync(filepath);predicate(filepath,stat)&&fs.unlinkSync(filepath)}}function createDirectorySync(dirpath){return fs.mkdirSync(dirpath,{recursive:!0}),dirpath}function normalizeFileExtension(ext){if(""===ext||"."===ext)return"";if(/[<>"|?*:]/g.test(ext))throw new Error(`Illegal characters in file extension: ${ext} | Illegal characters are: <>"|?:*`);return ext.includes(".")?ext.substring(ext.lastIndexOf(".")):"."+ext}function createFileExtensionFilter(...fileExtensions){return fileExtensions.length?filepath=>{for(const ext of fileExtensions.map(normalizeFileExtension))if(path.extname(filepath)===ext)return!0;return!1}:()=>!0}function cwdpath(...paths){return path.join(process.cwd(),...paths)}function deleteDirectory(dirpath){return __awaiter(this,void 0,void 0,function*(){yield fs.promises.rm(dirpath,{recursive:!0,force:!0})})}function deleteDirectorySafe(dirpath){return __awaiter(this,void 0,void 0,function*(){fs.existsSync(dirpath)&&(yield fs.promises.rm(dirpath,{recursive:!0,force:!0}))})}function deleteDirectorySafeSync(dirpath){return fs.existsSync(dirpath)?(fs.rmSync(dirpath,{recursive:!0,force:!0}),dirpath):dirpath}function deleteDirectorySync(dirpath){return fs.rmSync(dirpath,{recursive:!0,force:!0}),dirpath}function ensureValidWindowsPath(path,options){const throwOrFalse=msg=>{if(options&&!0===options.assert)throw new Error(`Invalid windows path. ${msg} | input received: ${path}`);return!1};if(0===path.length)return throwOrFalse("Path string is length 0.");if(-1!==path.indexOf("/")&&-1!==path.indexOf("\\"))return throwOrFalse("Path contains both backslash and forward slash.");const maxLength=(options&&options.extendedMaxLength?32767:260)-12;if(path.length>maxLength)return throwOrFalse(`Maximum length of ${maxLength} exceeded`);let noDriveLetter=path;return /^\w:(\\|\/)/g.test(path)&&(noDriveLetter=path.substring(2)),/[<>"|?*:]/g.test(noDriveLetter)?throwOrFalse(`Illegal characters in: ${path} | Illegal characters are: <>"|?:*`):!/(\\|\/)(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])((\\|\/)|$)/g.test(path)||throwOrFalse("Illegal name in path string")}function isWindows(){return process.platform.includes("win")}function getDiskDrivesWindows(){if(!isWindows())throw new Error("This function is only available on Windows.");return execSync("wmic logicaldisk get name").toString().split("\n").map(s=>s.trim()).filter(s=>/^[a-z]:/i.test(s)).map(s=>s.substring(0,2)+path.sep)}function getHomeDirectory(){const home=path.resolve(process.env.HOME||process.env.USERPROFILE||"");if(!home)throw new Error("Home directory not found");return home}function getDownloadsDirectory(){return path.join(getHomeDirectory(),"Downloads")}function getLastMofifiedFileIn(dirpath){return __awaiter(this,void 0,void 0,function*(){const children=yield fs.promises.readdir(dirpath),filtered=children.map(fname=>{const fpath=path.join(dirpath,fname),stat=fs.statSync(fpath);return[fpath,stat.mtimeMs,stat.isFile()]}).filter(a=>a[2]),sorted=filtered.sort((a,b)=>b[1]-a[1]);return sorted[0][0]})}function getRootDir(){return path.parse(process.cwd()).root}function readJsonFile(filepath){return __awaiter(this,void 0,void 0,function*(){return JSON.parse(yield fs.promises.readFile(filepath,"utf8"))})}function readJsonFileSync(filepath){return JSON.parse(fs.readFileSync(filepath,"utf8"))}function readPackageJson(projectRoot=process.cwd()){const filepath=path.join(projectRoot,"package.json");return fs.existsSync(filepath)?readJsonFileSync(filepath):void 0}function removeIllegalFilenameCharacters(string){return string.replace(/[/\\?%*:|"<>]/g,"")}function unzipFile(filepath,outdir){return __awaiter(this,void 0,void 0,function*(){yield extract(filepath,{dir:outdir})})}function unzipMergeFiles(source,target){return __awaiter(this,void 0,void 0,function*(){"string"==typeof source&&(source=fs.readdirSync(source).map(filename=>path.join(source,filename)));for(const filepath of source){const stat=yield fs.promises.stat(filepath);stat.isDirectory()||filepath.endsWith(".zip")&&(yield extract(filepath,{dir:target}))}})}function wipeDirectorySafe(dirpath){return __awaiter(this,void 0,void 0,function*(){yield deleteDirectorySafe(dirpath),yield createDirectory(dirpath)})}function writeJsonFile(filepath,data,pretty=!1){return __awaiter(this,void 0,void 0,function*(){yield fs.promises.writeFile(filepath,JSON.stringify(data,null,pretty?2:void 0))})}function writeJsonFileSync(filepath,data,pretty=!1){fs.writeFileSync(filepath,JSON.stringify(data,null,pretty?2:void 0))}export{absoluteToRelativePath,appendLineToFile,cleanDirectorySync,createDirectory,createDirectorySync,createFileExtensionFilter,cwdpath,deleteDirectory,deleteDirectorySafe,deleteDirectorySafeSync,deleteDirectorySync,ensureValidWindowsPath,getDiskDrivesWindows,getDownloadsDirectory,getHomeDirectory,getLastMofifiedFileIn,getRootDir,isWindows,normalizeFileExtension,readJsonFile,readJsonFileSync,readPackageJson,removeIllegalFilenameCharacters,unzipFile,unzipMergeFiles,wipeDirectorySafe,writeJsonFile,writeJsonFileSync};
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/fs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Tools for working with the local filesystem in nodejs.",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
7
7
|
"types": "dist/types/index.d.ts",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"build": "rimraf dist && rollup --config ./rollup.config.js --bundleConfigAsCjs"
|
|
10
|
-
"docs": "rimraf ../../docs/fs && typedoc --out ../../docs/fs --entryPoints src/index.ts"
|
|
9
|
+
"build": "rimraf dist && rollup --config ./rollup.config.js --bundleConfigAsCjs"
|
|
11
10
|
},
|
|
12
11
|
"author": {
|
|
13
12
|
"name": "Benjamin Møller Jensen",
|