-
Notifications
You must be signed in to change notification settings - Fork 15
/
file_utils.js
64 lines (55 loc) · 1.59 KB
/
file_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
const { promisify } = require('util')
const fs = require('fs')
const path = require('path');
// The folder's path containing all of our .gitignore files
const folderPath = path.join(__dirname, '/gitignores')
/**
* Returns an array of the names of the files contained in folderPath
*/
const getGitignoreNames = async () => {
const readdir = promisify(fs.readdir)
const files = await readdir(folderPath).catch((err) => {
console.error(`Error reading ${folderPath}: ${err}`)
})
return files
}
/**
* Returns an array of file types based on the following format:
* type.gitignore
* @param {*} files
*/
const getFileType = files => files.map(file => ({
title: file.split('.')[0],
value: file.split('.')[0],
}))
/**
* Write the .gitignore file content
* @param {*} file
* @param {*} content
*/
const writeFile = async (file, content) => {
const asyncWriteFile = promisify(fs.writeFile)
await asyncWriteFile(file, content)
.then(console.info('📄 .gitignore created successfully'))
.catch(e => console.error(e))
}
/**
* Read a file content
* @param {*} files
*/
const readFile = async (files) => {
const asyncReadFile = promisify(fs.readFile)
if (files.includes(',')) {
const filesToIterate = files.split(',')
return Promise.all(filesToIterate.map(file => asyncReadFile(`${folderPath}/${file}.gitignore`, { encoding: 'utf-8' })))
.catch(e => console.error(e))
}
return asyncReadFile(`${folderPath}/${files}.gitignore`, { encoding: 'utf-8' })
.catch(e => console.error(e))
}
module.exports = {
getGitignoreNames,
getFileType,
writeFile,
readFile,
}