【发布时间】:2018-10-16 08:04:34
【问题描述】:
在使用节点脚本进行一些搜索替换后,我正在尝试重命名文件夹(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)}`);
});
});
}
基本上取自here
我得到的错误是
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 ]
我做错了什么?
【问题讨论】:
-
您确定该文件存在吗?当您使用拥有目录的帐户时,这是否会改变,您要重命名
-
它应该存在,但我没有明确地检查它:/脚本应该有权进行更改,因为它可以很好地更改文件中的字符串
-
哦,我想我刚刚意识到我的路径不是
${rootDir}/wp-content/my_theme/,而是${rootDir}/wp-content/themes/my_theme/-_-" -
我认为你来错地方了。您在尝试重命名时有
/vagrant-local/www/me/,但在之前的输出中有CWD: /Users/my_user/vagrant-local/www/me/。 -
做到了! ;) ...
标签: javascript node.js fs