如何使用 npm 脚本重命名文件 [英] How to rename file with npm script

查看:92
本文介绍了如何使用 npm 脚本重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 copyfiles 作为 npm scirpt copyfiles -u 2/src/app/conf.dev.json dist/config/ 但最后我想让文件重命名

如何将输入文件重命名为 conf.json 之类的内容?

我查看了文档,没有发现可以使用 copyfiles 来实现.有什么想法吗?

更新:我跟进了这个 使用 NPM 重命名文件,但是在执行 npm 时出现错误复制:

"copy": "copyfiles -u 2/src/app/conf.dev.json dist/config/&& node -e require('fs').rename('dist/config/conf.prod.json','dist/config/conf.json')"

<块引用>

fs.js:137抛出新的 ERR_INVALID_CALLBACK();^

TypeError [ERR_INVALID_CALLBACK]: 回调必须是一个函数

解决方案

以下是一些成功满足您要求的解决方案:

解决方案 A

@vitorlui 的回答中所述,使用 nodejs 内置 fs.rename().

此外,当通过 npm 脚本使用 node -e 命令时,需要包装脚本以在 JSON 转义双引号中进行评估,即 \"...\".

  1. 例如,按如下方式配置 package.jsonscripts 部分:

    "scripts": {"rename": "node -e \"require('fs').rename('dist/config/conf.dev.json', 'dist/config/conf.json', function(err) { if (err)console.log(err); console.log('文件重命名成功!') })\"","copy": "copyfiles -u 2 \"src/app/conf.dev.json\" \"dist/config/\"","copy-and-rename": "npm run copy && npm run rename"},

  2. 然后运行以下 npm 命令:

    npm run copy-and-rename

  3. 成功完成后,您应该会在文件复制和重命名后看到以下记录到控制台:

    <块引用>

    文件重命名成功!

<小时>

解决方案 B

您还可以考虑安装和使用 renamer 来重命名文件.如果您的重命名要求变得比问题中提供的示例更复杂,或者如果您想要比解决方案 A 更简洁的内容,这可能会有所帮助.

安装并检查哪个版本:

  1. cd 到您的项目目录并通过运行以下命令安装 renamer:

    npm i -D 重命名器

  2. 然后运行以下命令来检查安装了哪个版本的renamer.

    npm ls 重命名器

<小时>

注意: 我要求您检查安装了哪个版本的原因是因为这将决定您应该使用以下哪些 renamer 命令.如果安装的版本是 <0.7.0 或 >=0.7.0,则略有不同:

<小时>

如果安装的重命名器版本是 <0.7.0

  1. package.jsonscripts 部分设置为以下内容:

    "scripts": {"rename": "renamer --dry-run -f --regex \"^conf.dev.json$\" -r \"conf.json\" \"dist/config/*\"","copy": "copyfiles -u 2 \"src/app/conf.dev.json\" \"dist/config/\"","copy-and-rename": "npm run copy && npm run rename"},

  2. 然后运行以下 npm 命令:

    npm run copy-and-rename

  3. 您应该会看到如下内容记录到您的控制台;

    <块引用>

    √ dist\config\conf.dev.json ->dist\config\conf.json

    指示更改了哪个路径名.

  4. 您还会注意到复制文件的实际文件名没有改变,这是因为我们包含了 --dry-run 选项.只需省略脚本中的 --dry-run 选项,然后再次运行命令 即可更改实际文件名.

<小时>

如果安装的renamer版本是>=0.7.0

自 v0.7.0 以来发生了重大变化,其中包括删除了 --regex 选项 (请参阅 此处 了解更多信息).从这个版本开始,现在提供了一个正则表达式文字.

对 API 的这种更改导致 rename 脚本(如前所示)需要重新定义如下:

"rename": "renamer -f \"/^conf.dev.json$/\" -r \"conf.json\" \"dist/config/*\"",^ ^

注意: --regex 选项已被省略,正则表达式现在是一个文字,即它现在包含在前导和尾随正斜杠中.此外,在此示例中,--dry-run 选项已被删除,因此出于测试目的恢复它.

<小时>

附加说明

  • 对于解决方案A解决方案B,复制和重命名逻辑已添加到单独的npm脚本中,(即copyrename 分别)以便于解释清楚.但是,您可以使用 && 操作符来链接这两个命令以形成一个 npm 脚本 - 尽管单行会很长 :)

  • 对于解决方案B,我经常使用renamer0.6.1版本,所以我运行;npm i -D renamer@0.6.1 安装,因为我通常需要支持旧版本的 nodejs.在这种情况下,我根据上述小节中显示的示例使用 --regex 标志:如果安装的重命名器版本为 <0.7.0".

I am using copyfiles as an npm scirpt copyfiles -u 2 /src/app/conf.dev.json dist/config/ but in the end I want to get the file renamed

How would you rename the input file in to something like conf.json ?

I check the docs and didn't find that it is possible to achieve with copyfiles. Any thoughts?

UPDATE: I follow up to this Rename file with NPM but getting an error when do npm run copy:

"copy": "copyfiles -u 2 /src/app/conf.dev.json dist/config/ && node -e require('fs').rename('dist/config/conf.prod.json','dist/config/conf.json')"

fs.js:137 throw new ERR_INVALID_CALLBACK(); ^

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

解决方案

Below are a couple of solutions to successfully meet your requirement:

Solution A

As noted in @vitorlui's answer the callback parameter is mandatory when using the nodejs built-in fs.rename().

Also when utilizing the node -e command via a npm script it is necessary to wrap the script to evaluate in JSON escaped double quotes, i.e. \"...\".

  1. For instance, configure the scripts section of your package.json as follows:

    "scripts": {
      "rename": "node -e \"require('fs').rename('dist/config/conf.dev.json', 'dist/config/conf.json', function(err) { if (err) console.log(err); console.log('File successfully renamed!') })\"",
      "copy": "copyfiles -u 2 \"src/app/conf.dev.json\" \"dist/config/\"",
      "copy-and-rename": "npm run copy && npm run rename"
    },
    

  2. Then run the following npm command:

    npm run copy-and-rename
    

  3. On successful completion you should see the following logged to the console after the file has been copied and renamed:

    File successfully renamed!


Solution B

You could also consider installing and utilizing renamer for renaming the file. This may be beneficial if your renaming requirements become more complex than the example provided in your question, or if you want something less verbose than Solution A.

Install and check which version:

  1. cd to your project directory and install renamer by running the following command:

    npm i -D renamer
    

  2. Then run the following command to check which version of renamer was installed.

    npm ls renamer
    


Note: The reason I ask you to check which version was installed is because this will determine which of the following renamer commands you should utilize. It differs slightly if the version installed is <0.7.0 or >=0.7.0:


If the version of renamer installed is <0.7.0

  1. Set the scripts section of your package.json to the following:

    "scripts": {
      "rename": "renamer --dry-run -f --regex \"^conf.dev.json$\" -r \"conf.json\" \"dist/config/*\"",
      "copy": "copyfiles -u 2 \"src/app/conf.dev.json\" \"dist/config/\"",
      "copy-and-rename": "npm run copy && npm run rename"
    },
    

  2. Then run the following npm command:

    npm run copy-and-rename
    

  3. You should see something like the following logged to your console;

    √ dist\config\conf.dev.json -> dist\config\conf.json

    to indicate which pathname was changed.

  4. You'll also notice that the actual filename of the copied file hasn't changed, that's because we included the --dry-run option. Simply omit the --dry-run option from your script and run the command again for the actual file name to be changed.


If the version of renamer installed is >=0.7.0

There was a breaking change since v0.7.0 which included the removal of the --regex option (see here for further info). A regular expression literal is now provided since this version instead.

This change to the API results in the rename script, (as previously shown), needing to be redefined as follows:

"rename": "renamer -f \"/^conf.dev.json$/\" -r \"conf.json\" \"dist/config/*\"",
                        ^               ^

Note: The --regex option has been omitted and a regexp is now a literal, i.e. it's now wrapped in a leading and trailing forward slash. Also, in this example the --dry-run option was removed, so reinstate it for testing purposes.


Additional Notes

  • For both Solution A and Solution B, the copying and renaming logic has been added to separate npm scripts, (namely copy and rename respectively) for clarity of explanation. However you can chain the two commands using the && operator instead to form one npm script - the single line will be rather long though :)

  • For Solution B, I often utilize version 0.6.1 of renamer, so I run; npm i -D renamer@0.6.1 to install, as I typically have older versions of nodejs to support. In which case I utilize the --regex flag as per the example shown in the aforementioned sub section titled: "If the version of renamer installed is <0.7.0".

这篇关于如何使用 npm 脚本重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆