我有一个简单的包裹。json文件,我想添加一个评论。有办法做到这一点吗,或者有什么hack可以做到这一点吗?
{
"name": "My Project",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x",
"mongoose": "3.x"
},
"devDependencies" : {
"should": "*"
/* "mocha": "*" not needed as should be globally installed */
}
}
上面的示例注释在npm崩溃时不起作用。我还尝试了// style注释。
您总是可以滥用重复的键会被覆盖的事实。这就是我刚才写的:
"dependencies": {
"grunt": "...",
"grunt-cli": "...",
"api-easy": "# Here is the pull request: https://github.com/...",
"api-easy": "git://..."
"grunt-vows": "...",
"vows": "..."
}
然而,不清楚JSON是否允许复制键(参见
JSON语法是否允许在一个对象中重复键?它似乎与npm一起工作,所以我冒这个险。
推荐的破解方法是使用“//”键(来自nodejs邮件列表)。当我测试它时,它不能处理“依赖”部分。另外,文章中的例子使用了多个“//”键,这意味着npm不会拒绝具有重复键的JSON文件。换句话说,上面的hack应该总是好的。
更新:重复密钥的一个恼人的缺点是npm install——save无声地消除了所有重复密钥。不幸的是,它很容易被忽视,你善意的评论也消失了。
“//”攻击看起来仍然是最安全的。但是,多行注释也会被npm install -save删除。
我有一个有趣的黑客想法。
创建一个适当的npm包名,作为文件包中的依赖项和devDependencies块的注释分隔符。Json,例如x----x----x
{
"name": "app-name",
"dependencies": {
"x----x----x": "this is the first line of a comment",
"babel-cli": "6.x.x",
"babel-core": "6.x.x",
"x----x----x": "this is the second line of a comment",
"knex": "^0.11.1",
"mocha": "1.20.1",
"x----x----x": "*"
}
}
注意:您必须在最后一个注释分隔行中添加一个有效的版本,如块中的*。
NPS(节点包脚本)为我解决了这个问题。它允许你把NPM脚本放到一个单独的JavaScript文件中,在那里你可以添加大量的注释和任何其他你需要的JavaScript逻辑。
https://www.npmjs.com/package/nps
来自我的一个项目的package-scripts.js示例
module.exports = {
scripts: {
// makes sure e2e webdrivers are up to date
postinstall: 'nps webdriver-update',
// run the webpack dev server and open it in browser on port 7000
server: 'webpack-dev-server --inline --progress --port 7000 --open',
// start webpack dev server with full reload on each change
default: 'nps server',
// start webpack dev server with hot module replacement
hmr: 'nps server -- --hot',
// generates icon font via a gulp task
iconFont: 'gulp default --gulpfile src/deps/build-scripts/gulp-icon-font.js',
// No longer used
// copyFonts: 'copyfiles -f src/app/glb/font/webfonts/**/* dist/1-0-0/font'
}
}
我只是做了一个本地安装npm安装nps -save-dev,并把它放在我的包。json脚本。
"scripts": {
"start": "nps",
"test": "nps test"
}
由于重复的注释键被删除运行包。json工具(npm, yarn等),我开始使用散列版本,允许更好地阅读多行和键,如:
"//": {
"alpaca": "we use the bootstrap version",
"eonasdan-bootstrap-datetimepicker": "instead of bootstrap-datetimepicker",
"moment-with-locales": "is part of moment"
},
根据我的IDE作为根键,这是“有效的”,但在依赖关系中,它抱怨期望字符串值。
另一个黑客
我创建了一个脚本来读取文件包。Json作为句柄模板的上下文。
代码如下,以防有人发现这种方法有用:
const templateData = require('../package.json');
const Handlebars = require('handlebars');
const fs = require('fs-extra');
const outputPath = __dirname + '/../package-json-comments.md';
const srcTemplatePath = __dirname + '/package-json-comments/package-json-comments.hbs';
Handlebars.registerHelper('objlist', function() {
// The first argument is an object, and the list is a set of keys for that obj
const obj = arguments[0];
const list = Array.prototype.slice.call(arguments, 1).slice(0,-1);
const mdList = list.map(function(k) {
return '* ' + k + ': ' + obj[k];
});
return new Handlebars.SafeString(mdList.join("\n"));
});
fs.readFile(srcTemplatePath, 'utf8', function(err, srcTemplate){
if (err) throw err;
const template = Handlebars.compile(srcTemplate);
const content = template(templateData);
fs.writeFile(outputPath, content, function(err) {
if (err) throw err;
});
});
句柄模板文件package-json-comments.hbs
### Dependency Comments
For package: {{ name }}: {{version}}
#### Current Core Packages
should be safe to update
{{{objlist dependencies
"@material-ui/core"
"@material-ui/icons"
"@material-ui/styles"
}}}
#### Lagging Core Packages
breaks current code if updated
{{{objlist dependencies
"amazon-cognito-identity-js"
}}}
#### Major version change
Not tested yet
{{{objlist dependencies
"react-dev-utils"
"react-redux"
"react-router"
"redux-localstorage-simple"
}}}