-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (45 loc) · 1.88 KB
/
app.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
module.exports = function replace_all_in_dir(dir,replace_str, replace_with_str, encoding){
return new Promise((resolve,reject)=>{
if(String.ReplaceAll === undefined) require("./str_replace_all_proto_promise.js")()
let fs_readdir_promise = require("./fs_readdir_promise.js")
let fs_readfile_promise = require("./fs_readfile_promise.js")
let fs_writefile_promise = require("./fs_writeFile_promise.js")
fs_readdir_promise(dir).then((filenames)=>{
Promise.all(
filenames.map(function(filename){
return new Promise((resolve,reject)=>{
fs_readfile_promise(dir,filename,encoding).then((file_content)=>{
//replace value in string first, replace with value second
file_content.ReplaceAll(replace_str,replace_with_str).then((new_file_content)=>{
fs_writefile_promise(dir,filename,new_file_content,encoding).then((response)=>{
resolve(response)
}).catch((err)=>{
reject(err)
})
})
})
})
})
)
.then((responses)=>{
resolve(responses)
})
.catch((err)=>{
reject(err)
})
})
})
}
//HOW TO USE
// replace_all_in_dir(
// "c:/query files/", //dont forget to make it end in with '/'
// "replace string",//string that you wish to replace in all the files
// "with this string",//string that you wish to replace the previous string with
// "utf-8"//encoding
// ).then((responses)=>{
// //returns an array of dir+filename that were saved
// console.log(responses)
// }).catch((err)=>{
// //what to do if something goes wrong
// console.log(err)
// })