-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
75 lines (61 loc) · 1.75 KB
/
utils.js
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
68
69
70
71
72
73
74
75
'use strict';
const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
const Colour = require('./Colour');
const msal = require('@azure/msal-node');
class Utils {
static execShell(cmd) {
Colour.writeColouredText(`OS: ${cmd}`, Colour.OPTIONS.FG_BLUE);
console.log();
return new Promise((resolve, reject) => {
exec(cmd, {maxBuffer: 1024 * 999999}, (error, stdout, stderr) => {
if (error) {
console.warn(error);
return reject(error);
}
//console.log(stdout ? stdout : stderr);
resolve(stdout ? stdout : stderr);
});
});
}
static removeMany(files) {
files.forEach(f => {
try {
if (fs.existsSync(f)) {
fs.unlinkSync(f);
} else {
Colour.writeColouredText(`${f} does not exist`, Colour.OPTIONS.FG_RED);
}
} catch {
Colour.writeColouredText(`Failed to remove ${f}`, Colour.OPTIONS.FG_RED);
}
});
}
static removeFolder(folderPath) {
Utils.removeMany(fs.readdirSync(folderPath).map(f => path.join(folderPath, f)));
fs.rmdirSync(folderPath);
}
static async getToken(config) {
const cca = new msal.ConfidentialClientApplication(config);
const resp = await cca.acquireTokenByClientCredential({
scopes: ['https://graph.microsoft.com/.default']
});
return resp.accessToken;
}
static async listFiles(config, path) {
const url = `https://graph.microsoft.com/v1.0/users/${config.files.user}/drive/root:${path}:/children`;
const token = await Utils.getToken(config);
const res = await fetch(url, {
headers: {
'Authorization': `Bearer ${token}`
}
});
const json = await res.json();
return json;
}
static async sleep(milliseconds){
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
}
module.exports = Utils;