1'use strict' 2var path = require('path') 3var writeFileAtomic = require('write-file-atomic') 4var moduleName = require('../utils/module-name.js') 5var deepSortObject = require('../utils/deep-sort-object.js') 6var sortedObject = require('sorted-object') 7var isWindows = require('../utils/is-windows.js') 8 9var sortKeys = [ 10 'dependencies', 'devDependencies', 'bundleDependencies', 11 'optionalDependencies', 'keywords', 'engines', 'scripts', 12 'files' 13] 14 15module.exports = function (mod, buildpath, next) { 16 var pkg = sortedObject(mod.package) 17 var name = moduleName(mod) 18 // Add our diagnostic keys to the package.json. 19 // Note that there are folks relying on these, for ex, the Visual Studio 20 // Node.js addon. 21 pkg._requiredBy = 22 mod.requiredBy 23 .map(function (req) { 24 if ( 25 req.package.devDependencies && 26 req.package.devDependencies[name] && 27 !req.package.dependencies[name] 28 ) { 29 return '#DEV:' + req.location 30 } else { 31 return req.location 32 } 33 }) 34 .concat(mod.userRequired ? ['#USER'] : []) 35 .sort() 36 pkg._location = mod.location 37 pkg._phantomChildren = {} 38 Object.keys(mod.phantomChildren).sort().forEach(function (name) { 39 pkg._phantomChildren[name] = mod.phantomChildren[name].package.version 40 }) 41 pkg._inBundle = !!mod.fromBundle 42 43 // sort keys that are known safe to sort to produce more consistent output 44 sortKeys.forEach(function (key) { 45 if (pkg[key] != null) pkg[key] = deepSortObject(pkg[key]) 46 }) 47 48 var data = JSON.stringify(sortedObject(pkg), null, 2) + '\n' 49 50 writeFileAtomic(path.resolve(buildpath, 'package.json'), data, { 51 // We really don't need this guarantee, and fsyncing here is super slow. Except on 52 // Windows where there isn't a big performance difference and it prevents errors when 53 // rolling back optional packages (#17671) 54 fsync: isWindows 55 }, next) 56} 57