【问题标题】:What is the behavior of -f (force?) argument in npm cache clean?npm cache clean 中 -f (force?) 参数的行为是什么?
【发布时间】:2015-09-19 19:33:33
【问题描述】:

我已经在网上搜索过,但我无法回答我的问题。怎么样:

npm cache clean -f

不同于:

npm cache clean

我怀疑-f--force 的简写,但即使在官方文档中,我也无法找到为什么要使用-f,它的优点和缺点是什么。我确定使用-f时会有警告,但我不知道为什么。

【问题讨论】:

    标签: node.js caching npm command-line-interface packages


    【解决方案1】:

    看一下npm cache clean源码:

    // npm cache clean [<path>]
    function clean (args, cb) {
      assert(typeof cb === 'function', 'must include callback')
    
      if (!args) args = []
    
      var f = path.join(npm.cache, normalize(args))
      if (f === npm.cache) {
        fs.readdir(npm.cache, function (er, files) {
          if (er) return cb()
          asyncMap(
            files.filter(function (f) {
              return npm.config.get('force') || f !== '-'
            }).map(function (f) {
              return path.join(npm.cache, f)
            }),
            rm,
            cb
          )
        })
      } else {
        rm(f, cb)
      }
    }
    

    源代码链接:https://github.com/npm/npm/blob/master/lib/cache.js#L199

    默认情况下,缓存清理命令看起来像浏览默认的 npm 缓存目录并删除除名为 - 的文件之外的所有文件。默认跳过此文件,仅在强制选项中删除。

    问题是破折号作为文件名的用途是什么?我在question 中找到了可能的答案。根据接受的答案中提供的信息,一些程序使用- 文件作为标准标准输出。

    总结一下。标准 npm cache clean 删除所有缓存文件,但有时用于存储标准输出内容的 - 文件除外。但是npm cache clean -fnpm cache clean --force 会删除所有文件。

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      相关资源
      最近更新 更多