我尝试重命名文件夹(WordPress主题),在使用节点脚本进行一些搜索替换后,但文件夹的重命名似乎失败了。
我想要这个
public_html/wp-content/my_theme/
成为
public_html/wp-content/something_other/
从提示中取出文件夹的名称(此部分有效,因为文件中的搜索替换工作正常)。
脚本看起来像这样
const fs = require('fs');
const path = require('path');
const rootDir = path.join(__dirname, '..');
// themePackageName is taken from the prompt and is defined
if (themePackageName !== 'my_theme') {
fs.renameSync(`${rootDir}/wp-content/my_theme/`, `${rootDir}/wp-content/${themePackageName}/`, (err) => {
if (err) {
throw err;
}
fs.statSync(`${rootDir}/wp-content/${themePackageName}/`, (error, stats) => {
if (error) {
throw error;
}
console.log(`stats: ${JSON.stringify(stats)}`);
});
});
}
基本上取自{{3}}
我得到的错误是
fs.js:781
return binding.rename(pathModule.toNamespacedPath(oldPath),
^
Error: ENOENT: no such file or directory, rename '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/my_theme/' -> '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/aws-theme/'
at Object.fs.renameSync (fs.js:781:18)
at Object.<anonymous> (/vagrant-local/www/me/wp-boilerplate/public_html/bin/rename.js:163:8)
at Module._compile (internal/modules/cjs/loader.js:654:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
at startup (internal/bootstrap/node.js:201:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my_theme@1.0.0 rename: `./bin/rename.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my_theme@1.0.0 rename script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /.npm/_logs/2018-05-06T10_06_25_961Z-debug.log
而debug.log
是
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/Cellar/node/9.11.1/bin/node',
1 verbose cli '/usr/local/bin/npm',
1 verbose cli 'run',
1 verbose cli 'rename' ]
2 info using npm@5.6.0
3 info using node@v9.11.1
4 verbose run-script [ 'prerename', 'rename', 'postrename' ]
5 info lifecycle my_theme@1.0.0~prerename: my_theme@1.0.0
6 info lifecycle my_theme@1.0.0~rename: my_theme@1.0.0
7 verbose lifecycle my_theme@1.0.0~rename: unsafe-perm in lifecycle true
8 verbose lifecycle my_theme@1.0.0~rename: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/my_user/vagrant-local/www/me/wp-boilerplate/public_html/node_modules/.bin:/usr/local/sbin:/Users/my_user/wpcs/vendor/bin:/Users/my_user/.rbenv/shims:/Users/my_user/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin
9 verbose lifecycle my_theme@1.0.0~rename: CWD: /Users/my_user/vagrant-local/www/me/wp-boilerplate/public_html
10 silly lifecycle my_theme@1.0.0~rename: Args: [ '-c', './bin/rename.js' ]
11 silly lifecycle my_theme@1.0.0~rename: Returned: code: 1 signal: null
12 info lifecycle my_theme@1.0.0~rename: Failed to exec rename script
13 verbose stack Error: my_theme@1.0.0 rename: `./bin/rename.js`
13 verbose stack Exit status 1
13 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack at EventEmitter.emit (events.js:180:13)
13 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:180:13)
13 verbose stack at maybeClose (internal/child_process.js:936:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
14 verbose pkgid my_theme@1.0.0
15 verbose cwd /Users/my_user/vagrant-local/www/me/wp-boilerplate/public_html
16 verbose Darwin 17.5.0
17 verbose argv "/usr/local/Cellar/node/9.11.1/bin/node" "/usr/local/bin/npm" "run" "rename"
18 verbose node v9.11.1
19 verbose npm v5.6.0
20 error code ELIFECYCLE
21 error errno 1
22 error my_theme@1.0.0 rename: `./bin/rename.js`
22 error Exit status 1
23 error Failed at the my_theme@1.0.0 rename script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
我做错了什么?
答案 0 :(得分:2)
您收到的错误:
Error: ENOENT: no such file or directory, rename '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/my_theme/' -> '/vagrant-local/www/me/wp-boilerplate/public_html/wp-content/aws-theme/'
说您尝试重命名的目录不存在。
在重命名目录之前,您应首先检查它是否存在,使用fs.existsSync()
:
const fs = require('fs');
const path = require('path');
const rootDir = path.join(__dirname, '..');
// themePackageName is taken from the prompt and is defined
if (themePackageName !== 'my_theme' && fs.existsSync(`${rootDir}/wp-content/my_theme/`)) {
fs.renameSync(`${rootDir}/wp-content/my_theme/`, `${rootDir}/wp-content/${themePackageName}/`, (err) => {
if (err) {
throw err;
}
fs.statSync(`${rootDir}/wp-content/${themePackageName}/`, (error, stats) => {
if (error) {
throw error;
}
console.log(`stats: ${JSON.stringify(stats)}`);
});
});
}
&#13;