-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (162 loc) · 5.23 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict'
const fs = require('fs-extra')
const path = require('path')
const supportedTypes = ['.md', '.json', '.txt', '.csv']
/**
* Class FSDocs
*/
class FSDocsManager {
/**
* constructor
* @param {string} dir - absolute path to the save dir
* @returns {FSDocsManager} instance
*/
constructor (dir) {
if (!path.isAbsolute(dir)) {
throw new Error('ERR_FILEPATH_NOT_ABSOLUTE')
}
this.basePath = dir
fs.mkdirpSync(dir)
}
/**
* @param {...any} args - wrapper for `saveFile`
* @param {string} dir - relative path to basePath to save destination
* @param {string} name - name of the file
* @param {string} ext - filetype extension .txt, .json, .md, .csv
* @param {string | object} content - content of the file - object if JSON
* @param {boolean} replace - overwrite the existing file
* @returns {Promise} - path to saved file
*/
async createFile (...args) {
this.checkIsNotAbsolute(args[0])
return this.saveFile(...args)
}
/**
* update a file
* @param {string} filePath relative path to the file needing updating
* @param {string|object} content new content for the file
* @returns {Promise} - path to updated file
*/
async updateFile (filePath, content) {
this.checkIsNotAbsolute(filePath)
const updatePath = path.resolve(this.basePath, filePath)
await this.checkExists(updatePath)
const { dir, name, ext } = path.parse(updatePath)
return this.saveFile(dir, name, ext, content, true)
}
/**
* Read a file contents
* @param {string} filePath path to the file
* @returns {Promise} - string contents of file
*/
async readFile (filePath) {
this.checkIsNotAbsolute(filePath)
const readPath = path.resolve(this.basePath, filePath)
await this.checkExists(readPath)
const stat = await fs.stat(readPath)
if (stat.isDirectory()) {
return this.listFiles(filePath)
}
return fs.readFile(readPath, { encoding: 'utf8' })
}
/**
* Delete a file
* @param {string} filePath path to the file to be deleted
* @returns {Promise} - string with path of deleted file
*/
async deleteFile (filePath) {
this.checkIsNotAbsolute(filePath)
const deletePath = path.resolve(this.basePath, filePath)
await this.checkExists(deletePath)
const stat = await fs.stat(deletePath)
if (stat.isDirectory()) {
const fileList = await fs.readdir(deletePath)
if (fileList.length) {
throw new Error('ERR_DELETE_DIR_WITH_CONTENTS')
}
}
await fs.remove(deletePath)
return deletePath
}
/**
* Check if a file exists
* @param {string} filePath - path to the file
*/
async checkExists (filePath) {
const exists = await fs.pathExists(filePath)
if (!exists) {
throw new Error('ERR_FILE_NOT_EXISTS')
}
}
checkIsNotAbsolute (filePath) {
if (path.isAbsolute(filePath)) {
throw new Error('ERR_ABSOLUTE_FILEPATH_NOT_ALLOWED')
}
}
/**
* List a directory content
* @param {string} dirPath - path to the dir to list
* @returns {Promise} - array of string filenames
*/
async listFiles (dirPath) {
this.checkIsNotAbsolute(dirPath)
const listPath = path.resolve(this.basePath, dirPath)
await this.checkExists(listPath)
const files = await fs.readdir(listPath)
return files
}
/**
* create a valid filename
* @param {string} dir - path to the file
* @param {string} name - base name for the fil
* @param {string} ext - extension for the file
* @param {boolean} replace overwrite or increment filename
* @returns {Promise} - string filename
*/
async makeFileName (dir, name, ext, replace = false) {
const savePath = path.resolve(dir, name)
if (replace || !(await fs.pathExists(`${savePath}${ext}`))) {
return `${savePath}${ext}`
}
let fileNameIncr = 1
while (await fs.pathExists(`${savePath}_${fileNameIncr}${ext}`)) {
fileNameIncr++
}
return `${savePath}_${fileNameIncr}${ext}`
}
/**
* sanitize by turning the content to a string
* @param {string|object} content - content to be saved
* @param {string} ext - filetype extension
* @returns {string} - sanitixed content
*/
sanitize (content, ext) {
let sanitized = content.toString()
if (ext === '.json') {
if (typeof content === 'object') {
sanitized = JSON.stringify(content)
}
}
return sanitized
}
/**
* Save the file to disk
* @param {string} dir - relative path to basePath to save destination
* @param {string} name - name of the file
* @param {string} ext - filetype extension .txt, .json, .md, .csv
* @param {string | object} content - content of the file - object if JSON
* @param {boolean} replace - overwrite the existing file
* @returns {Promise} - string path to saved file
*/
async saveFile (dir, name, ext, content = '', replace = false) {
const saveDir = path.resolve(this.basePath, dir)
if (!supportedTypes.includes(ext)) {
throw new Error('ERR_FILETYPE_NOT_SUPPORTED')
}
const sanitized = this.sanitize(content, ext)
const savePath = await this.makeFileName(saveDir, name, ext, replace)
await fs.outputFile(savePath, sanitized)
return path.relative(this.basePath, savePath)
}
}
module.exports = FSDocsManager