我试图重命名一个文件夹(WordPress主题),在使用一个Node脚本进行搜索和替换后,但似乎重命名文件夹失败了。
我希望能够实现这个目标。
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)}`);
});
});
}
这基本上是从这里取得的。
我遇到的错误是
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 ]
我做错了什么?
/vagrant-local/www/me/
,但是在之前的输出中,当前工作目录为/Users/my_user/vagrant-local/www/me/
。 - Neil Lunn