我有一个简单的包裹。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注释。
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"
}
在浪费了一个小时的复杂和俗套的解决方案后,我找到了注释package.json中庞大的依赖项部分的简单而有效的解决方案。就像这样:
{
"name": "package name",
"version": "1.0",
"description": "package description",
"scripts": {
"start": "npm install && node server.js"
},
"scriptsComments": {
"start": "Runs development build on a local server configured by server.js"
},
"dependencies": {
"ajv": "^5.2.2"
},
"dependenciesComments": {
"ajv": "JSON-Schema Validator for validation of API data"
}
}
当以同样的方式排序时,现在我很容易在Git提交差异中或在编辑器中跟踪这些依赖/注释对,同时使用package.json文件。
不需要使用额外的工具,只需要简单有效的JSON。
As of pnpm 7.17.1, which was just released, you can switch to pnpm for package management, move your package.json to package.json5, and comments in package.json5 are allowed and will be preserved by pnpm. Note however that for publishing as a package to use on the npm registry (for example), a package.json5 will not be recognized by other package managers and I doubt by all of the registry's processing. So you would have to convert the package.json5 to a package.json before publishing. On the other hand, for "top-level applications" that are unlikely to be included as packages in other projects, a package.json5 seems to work just fine, as long as you then stick with pnpm as your package manager.
另一个黑客
我创建了一个脚本来读取文件包。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"
}}}
最近在Node.js邮件列表中讨论了这个问题。
根据创建npm的Isaac Schlueter的说法:
... "//"键永远不会被NPM用于任何目的,它是为注释保留的…如果想使用多行注释,可以使用数组或多个“//”键。
当使用你常用的工具(npm, yarn等)时,多个“//”键将被移除。这生存:
{ "//": [
"first line",
"second line" ] }
这将不复存在:
{ "//": "this is the first line of a comment",
"//": "this is the second line of the comment" }
必须注意,"//"只能在包的根目录下使用。json对象。例如
{
“/ /”:“评论!”
“依赖”:{…}
}
是有效的但是
{
“依赖”:{
“/ /”:“评论?”
}
}
是无效的。
——@david_p评论