demo-xrp
v1.0.0
Published
```Javascript: const fs = require("fs"); const path = require("path"); const { Console } = require("console"); const copyDir = (source, target) => { fs.mkdirSync(target); fs.readdirSync(so
Downloads
2
Readme
Javascript copyDir
const fs = require("fs");
const path = require("path");
const { Console } = require("console");
const copyDir = (source, target) => {
fs.mkdirSync(target);
fs.readdirSync(source).forEach((pathfile) => {
const midSource = path.join(source, pathfile)
const midTarget = path.join(target, pathfile)
if (fs.statSync(midSource).isFile()) {
if (path.extname(midSource) === ".css") {
let file = fs.readFileSync(midSource, "utf-8");
let result = file.replace(/background/g, "color")
fs.writeFileSync(midTarget, result)
} else {
fs.copyFileSync(midSource, midTarget)
}
} else {
copyDir(midSource, midTarget)
}
})
}
const dirtoJSON = (dir) => {
return fs.statSync(dir).isFile() ? {
name: path.basename(dir),
path: path.resolve(dir),
type: path.extname(dir).slice(1),
} : {
name: path.basename(dir),
path: path.resolve(dir),
type: "文件夹",
children: fs.readdirSync(dir).map(item => dirtoJSON(path.join(dir, item))),
}
}
copyDir("day8", "XXX")
const o = dirtoJSON("XXX")
fs.writeFileSync("data.json",JSON.stringify(o))
redDir
const readFile = require("./main");
async function read() {
let str1 = await readFile("1.txt");
let str2 = await readFile("2.txt");
let str3 = await readFile("3.txt");
console.log(str1, str2, str3)
}
read()
main.js中
const fs = require("fs");
const readFile = (pathfile, encoding = "utf8") => {
return new Promise((resolve, reject) => {
fs.readFile(pathfile, encoding, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data)
})
})
}
module.exports = readFile