Last active
November 20, 2018 03:38
-
-
Save phucpnt/04486920241fb83e9db58cf1e75e9438 to your computer and use it in GitHub Desktop.
Create react app - build script for moving "build" and "static" folder to another location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs-extra"); | |
const path = require("path"); | |
const klaw = require("klaw"); | |
const through2 = require("through2"); | |
function run(newBuildFolder = "./dist", staticFolderName = "statiz") { | |
fs.remove("./dist").then(() => | |
fs.copy("./build", newBuildFolder).then(() => { | |
const updateFolderBuildFile = through2.obj(function(item, enc, next) { | |
if (/\.(js|css|json|html)$/.test(path.extname(item.path))) { | |
let content = fs.readFileSync(item.path, { encoding: "utf8" }); | |
content = content.replace( | |
/static\/(css|media|js)/g, | |
staticFolderName + "/$1" | |
); | |
fs.writeFileSync(item.path, content, { encoding: "utf8" }); | |
item.updated = true; | |
} | |
this.push(item); | |
next(); | |
}); | |
let updatedFiles = []; | |
klaw("./dist") | |
.pipe(updateFolderBuildFile) | |
.on("data", item => { | |
if (item.updated) { | |
updatedFiles.push(item.path); | |
} | |
}) | |
.on("end", () => { | |
console.dir(updatedFiles); | |
fs.remove("./dist/" + staticFolderName).then(() => { | |
fs.move("./dist/static", "./dist/" + staticFolderName); | |
}); | |
}); | |
}) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment