gengyunfeikz
v1.0.0
Published
```js
Downloads
3
Readme
//fs.文件
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))