-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskUtils.js
69 lines (63 loc) · 1.86 KB
/
diskUtils.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
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
/**
* writeData
* writes the string on a file on disk
* @param dataString
* @param filename
*/
const writeData = (dataString, filename) => {
const fileUrl = path.join(process.cwd(), filename) //Current Working Directory + filename
return new Promise((resolve, reject) => {
fs.writeFile(fileUrl, dataString, (err) => {
if (err) {
console.error('Error writing file ' + fileUrl, err.toString())
return reject('Error writing file ' + fileUrl + ' ' + err.toString())
}
console.log('The file was saved as ' + fileUrl)
return resolve(true)
})
})
}
const checkAccess = name =>
new Promise((resolve, reject) => {
try {
fs.access(name, fs.constants.F_OK, function (err) {
if (err) reject(err)
else resolve()
})
} catch (err) {
reject(err)
}
})
const pad2 = (n) => n.toString().padStart(2, '0')
const writeExport = (organizationId, boards) => {
// write each board in a JSON file in the folder /export/YYYY-MM-DD/organizationId/board.name.json
// create folders if no exists
const today = new Date(),
YYYY = today.getFullYear(),
MM = pad2(today.getMonth() + 1),
DD = pad2(today.getDate()),
todayRelativePath = path.join('export', `${YYYY}-${MM}-${DD}`, organizationId)
return checkAccess(todayRelativePath)
.catch(err => {
//if no directory, we create it
if (err && err.code === 'ENOENT') {
console.log('Creating ' + todayRelativePath)
return mkdirp(todayRelativePath)
}
})
.then(() => {
//write files
return Promise.all(boards.map(board => {
const nameCleaned = board.name.match(/[A-z0-9]/g).join(''),
filename = path.join(todayRelativePath, nameCleaned + '.json')
console.log('Writing file ' + filename)
return writeData(JSON.stringify(board), filename)
}))
})
}
module.exports = {
writeExport
}