1 | const fs = require("fs-extra");
|
2 | const chalk = require("chalk");
|
3 | const boxen = require("boxen");
|
4 | const ONE_SIXTH = 1 / 6;
|
5 | const ONE_THIRD = 1 / 3;
|
6 | const TWO_THIRDS = 2 / 3;
|
7 | const utils = {};
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | utils.emptyDir = async f => {
|
18 | try {
|
19 | await fs.emptyDir(f);
|
20 |
|
21 | } catch (err) {
|
22 | console.error(err);
|
23 | }
|
24 | };
|
25 |
|
26 | const desiredMode = 0o2775;
|
27 | const defaultOptions = {
|
28 | mode: desiredMode
|
29 | };
|
30 | utils.ensureDir = async (directory, options = defaultOptions) => {
|
31 | try {
|
32 | await fs.ensureDir(directory, options);
|
33 | } catch (err) {
|
34 | console.error(err);
|
35 | }
|
36 | };
|
37 |
|
38 | utils.ensureFile = async f => {
|
39 | try {
|
40 | await fs.ensureFile(f);
|
41 | } catch (err) {
|
42 | console.error(err);
|
43 | }
|
44 | };
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | utils.pathExists = async f => {
|
55 | const exists = await fs.pathExists(f);
|
56 |
|
57 | return exists;
|
58 | };
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | utils.readJson = async f => {
|
69 | try {
|
70 | const packageObj = await fs.readJson(f);
|
71 |
|
72 | return packageObj;
|
73 | } catch (err) {
|
74 | console.error(err);
|
75 | return {};
|
76 | }
|
77 | };
|
78 |
|
79 | utils.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 |
|
89 | utils.removeFile = async f => {
|
90 | try {
|
91 |
|
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 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | utils.writeFile = async (f, contents = "") => {
|
109 | try {
|
110 | return await fs.outputFile(f, contents);
|
111 |
|
112 | } catch (err) {
|
113 | console.error(err);
|
114 | }
|
115 | };
|
116 |
|
117 | utils.writeJson = async (f, json = {}) => {
|
118 | try {
|
119 | await fs.writeJson(f, json);
|
120 |
|
121 | } catch (err) {
|
122 | console.error(err);
|
123 | }
|
124 | };
|
125 |
|
126 | utils.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 |
|
136 | utils.appendToFile = async (f, contents = "") => {
|
137 | try {
|
138 | await fs.appendFileSync(f, contents);
|
139 | } catch (err) {
|
140 | console.error(err);
|
141 | }
|
142 | };
|
143 |
|
144 | utils.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 | },
|
161 | align: "center"
|
162 | }
|
163 | )
|
164 | );
|
165 | };
|
166 |
|
167 | const 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 |
|
186 | const 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 |
|
195 | utils.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 |
|
204 | module.exports = utils;
|