{%= include("install-bower", {save: true}) %}
var config = require('./')();
var pkg = config.normalize(require('./package'));
Normalizes most package.json fields, and:
- converts
repository
objects to a string - stringifies
author
object - stringifies each "person" object in
maintainers
,contributors
andcollaborators
- converts
licenses
arrays and objects to alicense
string - removes files that don't exist from
bin
,main
and thefiles
array - adds
cli.js
tobin
if it exists - creates
keywords
array fromname
if not defined
See the schema, normalizers, and unit tests for more examples.
Values are normalized using a schema that is passed to [map-schema][].
- only properties that have a corresponding field on the schema will be normalized.
- any properties that do not have a corresponding field are returned unmodified.
See the .field docs to learn how to add or overwrite a field on the schema.
A default
value may optionally be defined when a .field
is registered. When .normalize
is run and a property that is required or recommended by npm is missing, normalize-pkg
attempts to create the field if valid data can be found in the repository.
built-in fields have a default value:
version
:'0.1.0'
license
:'MIT'
engines
:{node: '>= 0.10.0'}
For example:
name
: the [project-name][] library is used to fill in the namebin
: if empty, populated withcli.js
orbin
if either exists on the file system
Example
The following:
var config = require('./')();
// no package.json is passed, just an empty object
var pkg = config.normalize({});
console.log(pkg);
Results
Since an empty object was passed, normalize-pkg
was smart enough to fill in missing fields looking for info in the project. In this case, specifically from parsing .git
config and using any defaults defined on the schema.
{ name: 'normalize-pkg',
version: '0.1.0',
homepage: 'https://github.com/jonschlinkert/normalize-pkg',
repository: 'jonschlinkert/normalize-pkg',
license: 'MIT',
files: [ 'index.js' ],
main: 'index.js',
engines: { node: '>= 0.10.0' } }
{%= apidocs("index.js") %}
Type: boolean
Default: undefined
Omit properties from package.json that do not have a field registered on the schema.
var Config = require('{%= name %}');
var config = new Config({knownOnly: true});
var pkg = config.normalize({name: 'my-project', foo: 'bar'});
console.log(pkg);
//=> {name: 'my-project'}
Type: array
Default: undefined
Filter the resulting object to contain only the specified keys.
Type: array
Default: undefined
Remove the specified keys from the resulting object.
Pass a fields
object on the options to customize any fields on the schema (also see options.extend):
var pkg = config.normalize(require('./package'), {
extend: true,
fields: {
name: {
normalize: function() {
return 'bar'
}
}
}
});
console.log(pkg.name);
//=> 'bar'
Type: boolean
Default: undefined
Used with options.field, pass true
if you want to extend a field that is already defined on the schema.
var pkg = config.normalize(require('./package'), {
extend: true,
fields: {
name: {
normalize: function() {
return 'bar'
}
}
}
});
console.log(pkg.name);
//=> 'bar'