javascript - 在 node.js 中重命名文件夹失败

标签 javascript node.js fs

在使用 Node 脚本进行一些搜索替换后,我正在尝试重命名一个文件夹(一个 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 ]

我做错了什么?

最佳答案

你得到的错误:

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)}`);
      });
    });
  }

关于javascript - 在 node.js 中重命名文件夹失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50198562/

相关文章:

javascript - 在 node.js 中过滤 Stream 对象

javascript - 如何使用 sitemap.js?

node.js - 如何解析 Angular for Vimeo SDK 所需的 'fs' 模块

node.js - 如何将图像从另一台服务器获取到我的 Express 服务器并写入磁盘

node.js - nodejs/fs : writing a tar to memory buffer

javascript - 调整视频iframe的高度

java - jsp 中使用 java 操作进行 ajax 调用 (J2EE)

javascript - 如何通过 javascript 确定触摸事件(或鼠标事件)中的真实 x y 坐标?

javascript - 通过谷歌标签管理器传递时,jquery on click 不起作用

node.js - Vueify/Browserify 在标准配置下抛出 ENOENT