-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.js
51 lines (44 loc) · 1.23 KB
/
fs.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
"use strict";
const fs = require ('fs')
, path = require ('path')
function replaceInFile (filename, regex, replacement) {
const contents = fs.readFileSync (filename, 'utf8')
const newContents = contents.replace (regex, replacement)
fs.truncateSync (filename)
fs.writeFileSync (filename, newContents)
}
function copyFile (oldName, newName) {
const contents = fs.readFileSync (oldName, 'utf8')
if (fs.existsSync (newName)) {
fs.truncateSync (newName)
}
fs.writeFileSync (newName, contents)
}
function overwriteFile (filename, contents) {
// log.cyan ('Overwriting → ' + filename.yellow)
fs.closeSync (fs.openSync (filename, 'a'));
fs.truncateSync (filename)
fs.writeFileSync (filename, contents)
}
function createFolder (folder) {
try {
fs.mkdirSync (folder)
} catch (err) {
if (err.code !== 'EEXIST') {
throw err
}
}
}
function createFolderRecursively (folder) {
const parts = folder.split (path.sep)
for (let i = 1; i <= parts.length; i++) {
createFolder (path.join.apply (null, parts.slice (0, i)))
}
}
module.exports = {
replaceInFile,
copyFile,
overwriteFile,
createFolder,
createFolderRecursively,
}