UNPKG

1.31 kBJavaScriptView Raw
1import fs from "fs";
2import {dirname} from "path";
3
4import {configObject} from "./config.js";
5import {homedir} from "os";
6
7
8let home;
9if(homedir){
10 home = homedir();
11}
12const colon = /:/g;
13const siloLike = /(silo\-\w+?)s?\/([^\/]+)\.([\w1234567890]+)$/g;
14export function pathTransform(path){
15 if(path.includes(":")){
16 //Ignore the first colon in window-like filesystems
17 path = path.slice(0, 3) + path.slice(3).replace(colon, "--");
18 }
19 if(configObject.invertedPath){
20 path = path.replace(siloLike, "$2-$1.$3")
21 }
22 if(path.includes("\\342\\200\\220")){
23 path = path.replace("\\342\\200\\220", "‐");
24 }
25 return path;
26}
27
28export function readFileSync(path, options){
29 return fs.readFileSync(pathTransform(path), options);
30}
31//Create writefilesync, with ability to create directory if it doesnt exist
32export function writeFileSync(path, data, options, dircreated = false){
33 path = pathTransform(path);
34 try{
35 return fs.writeFileSync(path, data, options);
36 }catch(e){
37 if(dircreated) throw e;
38 let directory = dirname(path);
39 try{
40 fs.statSync(directory);
41 throw e;
42 }catch(nodir){
43 fs.mkdirSync(directory);
44 return writeFileSync(path, data, options, true);
45 }
46 }
47}