wallet-core/node_modules/write-pkg/index.js

54 lines
1.0 KiB
JavaScript
Raw Normal View History

2017-05-28 00:38:50 +02:00
'use strict';
const path = require('path');
const writeJsonFile = require('write-json-file');
const sortKeys = require('sort-keys');
2017-08-14 05:01:11 +02:00
const opts = {detectIndent: true};
2017-05-28 00:38:50 +02:00
const dependencyKeys = new Set([
'dependencies',
'devDependencies',
'optionalDependencies',
'peerDependencies'
]);
function normalize(pkg) {
const ret = {};
for (const key of Object.keys(pkg)) {
2017-08-14 05:01:11 +02:00
if (!dependencyKeys.has(key)) {
ret[key] = pkg[key];
} else if (Object.keys(pkg[key]).length !== 0) {
ret[key] = sortKeys(pkg[key]);
}
2017-05-28 00:38:50 +02:00
}
return ret;
}
module.exports = (fp, data) => {
if (typeof fp !== 'string') {
data = fp;
fp = '.';
}
fp = path.basename(fp) === 'package.json' ? fp : path.join(fp, 'package.json');
data = normalize(data);
return writeJsonFile(fp, data, opts);
};
module.exports.sync = (fp, data) => {
if (typeof fp !== 'string') {
data = fp;
fp = '.';
}
fp = path.basename(fp) === 'package.json' ? fp : path.join(fp, 'package.json');
data = normalize(data);
writeJsonFile.sync(fp, data, opts);
};