-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
67 lines (57 loc) · 1.55 KB
/
Copy pathhelpers.js
File metadata and controls
67 lines (57 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const runProcess = async ( cmd ) => {
const p = Deno.run({
cmd,
stdout: 'inherit',
stderr: 'inherit',
});
await p.status();
p.close();
};
const success = ( msg ) => {
console.log(`%c${msg}`, 'color: green');
return false;
}
const error = (msg) => {
console.log(`%c${msg}`, 'color: red');
return false;
}
const fromHome = ( route = '' ) => {
// Determine the correct environment variable based on the operating system
const homeEnvVar = Deno.build.os === "windows" ? "USERPROFILE" : "HOME";
// Retrieve the home directory path
let path = Deno.env.get(homeEnvVar) + '/.doinfo';
if( route ){
path += '/' + route;
}
return path;
}
const iterateOver = (data, callback, path = '') => {
// Check if the current data is an array or object
if (Array.isArray(data) || (data && typeof data === 'object') ) {
Object.keys(data).forEach(key => {
// Build the path
const newPath = path === '' ? key : `${path}.${key}`;
// If the value is an array or object, recurse into it
if (Array.isArray(data[key]) || typeof data[key] === 'object') {
iterateOver(data[key], callback, newPath);
} else {
callback(key, data[key], newPath);
}
});
}
}
const getJson = async ( filePath ) => {
try {
return JSON.parse(await Deno.readTextFile(filePath));
} catch(e) {
return false;
}
}
export default {
runProcess,
success,
error,
fromHome,
iterateOver,
getJson
};