UNPKG

4.94 kBJavaScriptView Raw
1const fs = require("fs-extra");
2const chalk = require("chalk");
3const boxen = require("boxen");
4const ONE_SIXTH = 1 / 6;
5const ONE_THIRD = 1 / 3;
6const TWO_THIRDS = 2 / 3;
7const utils = {};
8
9/**
10 * Output json file
11 * @param f - file name with directory
12 * @return {boolean}
13 * @example
14 const file = "/tmp/this/path/does/not/exist/file.json";
15 removeFile(file);
16 */
17utils.emptyDir = async f => {
18 try {
19 await fs.emptyDir(f);
20 // console.log(`${chalk.green.underline("Directory")} ${chalk.bold(f)} emptied!`);
21 } catch (err) {
22 console.error(err);
23 }
24};
25
26const desiredMode = 0o2775;
27const defaultOptions = {
28 mode: desiredMode
29};
30utils.ensureDir = async (directory, options = defaultOptions) => {
31 try {
32 await fs.ensureDir(directory, options);
33 } catch (err) {
34 console.error(err);
35 }
36};
37
38utils.ensureFile = async f => {
39 try {
40 await fs.ensureFile(f);
41 } catch (err) {
42 console.error(err);
43 }
44};
45
46/**
47 * Ensure path exists with dirs
48 * @param f the file path
49 * @return {boolean}
50 * @example
51 const file = ".fsr/config.json";
52 pathExists(file);
53 */
54utils.pathExists = async f => {
55 const exists = await fs.pathExists(f);
56
57 return exists;
58};
59
60/**
61 * Read json file
62 * @param f - file name with directory
63 * @return {Promise<void>}
64 * @example
65 const file = "/tmp/this/path/does/not/exist/file.json";
66 outputJson(file);
67 */
68utils.readJson = async f => {
69 try {
70 const packageObj = await fs.readJson(f);
71 // console.log(`${chalk.green.underline("File")} ${chalk.bold(f)} read!`);
72 return packageObj;
73 } catch (err) {
74 console.error(err);
75 return {};
76 }
77};
78
79utils.readFile = async f => {
80 try {
81 let fl = await fs.readFileSync(f, "utf8");
82 return fl;
83 } catch (err) {
84 console.error(err);
85 return {};
86 }
87};
88
89utils.removeFile = async f => {
90 try {
91 // console.log(`${chalk.green.underline("File")} ${chalk.bold(f)} removed!`);
92 return await fs.remove(f);
93 } catch (err) {
94 console.error(`File ${f} NOT REMOVED! ${err}`);
95 return false;
96 }
97};
98
99/**
100 * Write file
101 * @param f - file name with directory
102 * @param contents - text inside the file
103 * @return {Promise<void>}
104 * @example
105 const file = "/tmp/this/path/does/not/exist/file.json";
106 writeFile(file);
107 */
108utils.writeFile = async (f, contents = "") => {
109 try {
110 return await fs.outputFile(f, contents);
111 // console.log(`${chalk.green.underline("File")} ${chalk.bold(f)} written!`);
112 } catch (err) {
113 console.error(err);
114 }
115};
116
117utils.writeJson = async (f, json = {}) => {
118 try {
119 await fs.writeJson(f, json);
120 // console.log(`${chalk.green.underline("File")} ${chalk.bold(f)} written!`);
121 } catch (err) {
122 console.error(err);
123 }
124};
125
126utils.chainAsync = fns => {
127 let curr = 0;
128 const last = fns[fns.length - 1];
129 const next = () => {
130 const fn = fns[curr++];
131 fn === last ? fn() : fn(next);
132 };
133 next();
134};
135
136utils.appendToFile = async (f, contents = "") => {
137 try {
138 await fs.appendFileSync(f, contents);
139 } catch (err) {
140 console.error(err);
141 }
142};
143
144utils.boxInform = async (msg, secondary = "") => {
145 console.log(
146 boxen(
147 chalk.hex("#717877")(msg) +
148 chalk.bold.underline.hex("#438b34")(secondary) +
149 chalk.hex("#717877")(" "),
150 {
151 padding: 0,
152 margin: { left: 2, top: 0, bottom: 0, right: 0 },
153 borderStyle: {
154 topLeft: chalk.hex("#5a596d")("╔"),
155 topRight: chalk.hex("#5a596d")("╗"),
156 bottomLeft: chalk.hex("#5a596d")("╚"),
157 bottomRight: chalk.hex("#5a596d")("╝"),
158 horizontal: chalk.hex("#5a596d")("═"),
159 vertical: chalk.hex("#5a596d")("║")
160 }, //"round",
161 align: "center" //,
162 }
163 )
164 );
165};
166
167const hue2rgb = (p, q, t) => {
168 if (t < 0) {
169 t += 1;
170 }
171 if (t > 1) {
172 t -= 1;
173 }
174 if (t < ONE_SIXTH) {
175 return p + (q - p) * 6 * t;
176 }
177 if (t < 0.5) {
178 return q;
179 }
180 if (t < TWO_THIRDS) {
181 return p + (q - p) * (TWO_THIRDS - t) * 6;
182 }
183 return p;
184};
185
186const hsl2rgb = (h, s, l) => {
187 if (s === 0) {
188 return new Array(3).fill(l);
189 }
190 const q = l < 0.5 ? l * s + l : l + s - l * s;
191 const p = 2 * l - q;
192 return [hue2rgb(p, q, h + ONE_THIRD), hue2rgb(p, q, h), hue2rgb(p, q, h - ONE_THIRD)];
193};
194
195utils.rainbowGradient = (len, saturation = 1, lightness = 0.5) => {
196 const gradient = [];
197 for (let x = 0; x < len; x++) {
198 gradient.push(hsl2rgb(x / len, saturation, lightness).map(c => Math.round(c * 255)));
199 }
200 return gradient;
201};
202
203// utils.emptyDir = async () => {};
204module.exports = utils;