Node.js npm postbuild脚本在重命名文件时出错
我已经为npm postbuild制作了一个节点脚本,在react应用程序中生成构建后,该脚本执行简单的任务来重命名构建文件夹中的文件 这是文件Node.js npm postbuild脚本在重命名文件时出错,node.js,create-react-app,package.json,npm-scripts,Node.js,Create React App,Package.json,Npm Scripts,我已经为npm postbuild制作了一个节点脚本,在react应用程序中生成构建后,该脚本执行简单的任务来重命名构建文件夹中的文件 这是文件postbuild.js let fs = require("fs"); let path = require("path"); const oldJSPath = path.join(__dirname, "build", "static", "js", "*.js"); const newJSPath = path.join(__dirname,
postbuild.js
let fs = require("fs");
let path = require("path");
const oldJSPath = path.join(__dirname, "build", "static", "js", "*.js");
const newJSPath = path.join(__dirname, "build", "static", "js", "bundle.js");
const oldCSSPath = path.join(__dirname, "build", "static", "css", "*.css");
const newCSSPath = path.join(__dirname, "build", "static", "css", "bundle.css");
fs.renameSync(oldJSPath, newJSPath);
fs.renameSync(oldCSSPath, newCSSPath);
现在的问题是,我不断得到错误:
enoint:没有这样的文件或目录,请重命名C:\Users\HDL\Documents\demo redux app\build\static\js\*.js'->“C:\Users\HDL\Documents\demo redux app\build\static\js\bundle.js”
let fs = require("fs");
let path = require("path");
const oldJSPath = path.join(__dirname, "build", "static", "js", "*.js");
const newJSPath = path.join(__dirname, "build", "static", "js", "bundle.js");
const oldCSSPath = path.join(__dirname, "build", "static", "css", "*.css");
const newCSSPath = path.join(__dirname, "build", "static", "css", "bundle.css");
fs.renameSync(oldJSPath, newJSPath);
fs.renameSync(oldCSSPath, newCSSPath);
即使构建目录中确实存在文件和目录
build
dir的结构:
-build
-static
-css
*.css
-js
*.js
-media
*.png, jpg etc
不知道这是否必要,但这里是package.json
:
{
"name": "demo-redux-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-redux": "^5.0.7",
"react-scripts": "1.1.4",
"redux": "^4.0.0",
"redux-promise-middleware": "^5.1.1",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"postbuild": "node postbuild.js",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Node.js
fs
不支持通配符
如果目的是匹配*.js
中的第一个文件,并将其重命名为bundle.js
,则可以使用globs:
const globby = require('globby');
const newJSPath = path.join(__dirname, "build", "static", "js", "bundle.js");
const oldJSWildcardPath = path.join(__dirname, "build", "static", "js", "*.js");
const [oldJSPath] = globby.sync(oldJSWildcardPath);
if (oldJSPath) {
fs.renameSync(oldJSPath, newJSPath);
}
...
或使用regexp:
const newJSPath = path.join(__dirname, "build", "static", "js", "bundle.js");
const oldJSDirPath = path.join(__dirname, "build", "static", "js");
const [oldJSPath] = fs.readdirSync(oldJSDirPath).filter(filename => /.js$/.test(filename));
if (oldJSPath) {
fs.renameSync(oldJSPath, newJSPath);
}
...
fs不支持通配符。newJSPath应该做什么?您正在尝试将多个文件重命名为单个build\static\js\bundle.js文件,是吗?well build只在js目录中生成一个以名称
main*.js
开头的js文件,它位于static
dir中,所以我尝试将单个文件重命名为bundle.js
,是的,我刚刚知道fs不支持通配符除了安装之外还有更好的解决方案吗glob fs
除非您想用regexp解析文件列表,否则您无论如何都需要glob。glob fs不够好,我建议globby。