json - 如何验证 Bower.json?

标签 json bower

我的 Bower.json 文件有错误。

谷歌发现这种情况很常见,通常是因为不可打印的字符(由于使用使用此类字符的编辑器)。

有没有办法从 cli 本地验证文件?

仅供引用,我的错误如下:

bower meltingpot#*          EMALFORMED Failed to read /tmp/james/bower/meltingpot-5659-xBMHsL/bower.json

Additional error details:
Unexpected token ]

我的bower.json文件的内容如下:

{
  "name": "meltingpot",
  "version": "0.0.1",
  "homepage": "https://github.com/jamesjenner/meltingpot",
  "authors": [
    "james-jenner <james.jenner@########.com>"
  ],
  "description": "HTML based application, websockets for comms and node.js for backend",
  "keywords": [
    "application",
    "websocket",
    "node"
  ],
  "license": "MIT",
  "ignore": [
    "**/.*",
    "Gruntfile.js",
    "application.js",
    "bower_components",
    "comms.js",
    "node_modules",
    "*.json",
    "panelHandler.js",
    "shared/panel.js",
    "test",
    "tests"
  ]
}

虽然我很高兴有人能指出问题的原因,但真正的问题是如何验证 Bower.json 文件以检测和识别任何错误。优先选择在发生错误的地方提供该行。

:编辑:

我创建了一个快速脚本来读取 Bower 文件,通过 JSON 解析数据,然后将结果字符串化。根据经验,如果 json 文件包含格式错误的 json,那么我会收到异常。所以看起来问题不在于文件的格式,而在于内容。

我用来测试文件的代码:

var fs = require('fs');

var data;

try {
  data = fs.readFileSync('bower.json');
} catch (e) {
  if (e.code === 'ENOENT') {
    // ENOENT is file not found, that is okay, just means no records
  } else {
    // unknown error, lets throw
    throw e;
  }
}

var json = JSON.parse(data);
console.log(JSON.stringify(json, null, ' '));

:编辑:

我现在已经使用bower-json进行了测试包(感谢 jayeff ),并且仍然验证为正常。使用的代码如下:

var bowerJson = require('bower-json');

// Can also be used by simply calling bowerJson()
bowerJson.read('./bower.json', function (err, json) {
    if (err) {
        console.error('There was an error reading the file');
        console.error(err.message);
        return;
    }

    console.log('JSON: ', json);

    try {
      bowerJson.validate(json);
    } catch (err) {
        console.error('There was an error validating the object');
        console.error(err.message);
    }
});

:编辑:

为了清楚地说明解决方案(由下面的 drorb 提供),问题是我创建了一个标签,注意到了额外的逗号,然后通过提交更改来修复逗号。就 Bower 而言,它采用的是最新版本/标签,因此忽略了我提交中的修复。

此外,在 git 中,您可以通过 git -d <tag> 删除版本,bower 似乎采用的是版本,而不是最新标签(github 使用版本的方式有些复杂)。就我而言,我删除了 0.1.0,添加了 0.0.1,并且 Bower 寄存器忽略了 0.0.1。我被迫创建一个 0.1.1 标签(在 Bower.json 文件中提交更改后),然后我就可以注册了。

我现在发现您可以通过网站界面从 github 删除版本(请参阅 Editing and Deleting Releases )。我能够整理我的版本,更新 package.json然后通过 git tag <tag> -a -m "<desc>" 推送我想要的版本和git push --tags 。现在bower info <mypackage>显示正确的信息。

我添加了上面的内容,因为当我查看其他存在此问题的 stackoverflow 帖子时没有讨论这一点。我希望上面的内容能够阐明标签的问题以及使用 Bower 进行提交、尝试注册以及如何清理任何错误。

我还建议阅读Creating and Maintaining your own Bower Package .

最佳答案

问题不在于您发布的 Bower.json,我认为这是您在 master 分支中的问题。
问题是 0.1.0 tag 中找到的文件版本 - 忽略列表中有一个不必要的逗号:

  "ignore": [
    "**/.*",
    "Gruntfile.js",
    "application.js",
    "bower_components",
    "comms.js",
    "node_modules",
    "package.json",
    "panelHandler.js",
    "shared/panel.js",
    "test",
    "tests", << issue is here
  ]

针对此文件运行测试代码会产生正确的错误消息:

undefined:27
  ]
  ^
SyntaxError: Unexpected token ]
    at Object.parse (native)
    at Object.<anonymous> (D:\work\js\meltingpot\test.js:16:17)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

关于json - 如何验证 Bower.json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27035791/

相关文章:

javascript - 如何在不在html上添加脚本标签的情况下使用google-maps-api-v3

c# - 如何处理来自httpclient的数据

json - 从 Django 模型获取数据并使用 AJAX 在 html 表中显示

java - 如何从 JSON 响应填充 List<Object>

php - laravel 5.6批量插入json数据

HashMap<String,LinkedList<Investor>> 的 java jackson 数据绑定(bind)

ruby-on-rails - 如何在 rails-assets.org 中包含一个带有 .在名字里?

javascript - Bower 不安装组件

npm - 使用 npm 全局安装 Bower