【问题标题】:Parsing Bower's Semantic Version Syntax解析 Bower 的语义版本语法
【发布时间】:2016-08-19 07:20:43
【问题描述】:

Bower 似乎偏离了semver spec,因为我有时会看到类似这样的依赖项(来自 2klic-angular/bower.json):

  "dependencies": {
    "angulargrid": "s-yadav/angulargrid#^0.4.0"
  }

This question 在解释 semver 本身方面有很长的路要走,但对于 s-yadav/angulargrid# 部分的情况并没有那么多。

查看 bower/lib/node_modules/bower-endpoint-parser/index.js

我看到以下代码:

function decompose(endpoint) {
    // Note that we allow spaces in targets and sources but they are trimmed
    var regExp = /^(?:([\w\-]|(?:[\w\.\-]+[\w\-])?)=)?([^\|#]+)(?:#(.*))?$/;
    var matches = endpoint.match(regExp);
    var target;
    var error;

    if (!matches) {
        error = new Error('Invalid endpoint: ' + endpoint);
        error.code = 'EINVEND';
        throw error;
    }

    target = trim(matches[3]);

    return {
        name: trim(matches[1]),
        source: trim(matches[2]),
        target: isWildcard(target) ? '*' : target
    };
}

因此,似乎可以使用 # 作为分隔符将存储库源指定为依赖版本的一部分。

但是,我无法在 bower 文档中找到任何描述此内容的内容。

Bowers 对 semver 的解释是否还有其他注意事项需要注意,或者这是唯一的一个,在 # 处拆分字符串是否足以找到需求表达式?

【问题讨论】:

    标签: bower semantic-versioning


    【解决方案1】:

    可以在json2decomposed@bower/lib/node_modules/bower-endpoint-parser/index.js 中找到相关代码:

    function json2decomposed(key, value) {
        ...
        key = trim(key);
        value = trim(value);
        ...
        endpoint = key + '=';
        split = value.split('#').map(trim);
    
        // If # was found, the source was specified
        if (split.length > 1) {
            endpoint += (split[0] || key) + '#' + split[1];
        // Check if value looks like a source
        } else if (isSource(value)) {
            endpoint += value + '#*';
        // Otherwise use the key as the source
        } else {
            endpoint += key + '#' + split[0];
        }
    
        return decompose(endpoint);
    }
    

    所以后来的target 是通过将JSON 依赖数组中的值除以# 来生成的。这个targetresolver解析,所以实际行为取决于使用的解析器,但典型的解析器使用node-semver,如果node-semver可以解析它。否则它会使用提交 ID、分支名称、标签等。

    所以用'#'分割字符串并在之后修剪空格就足以找到需求表达式,但它毕竟可能不是一个semver版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-02
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 2011-03-29
      相关资源
      最近更新 更多