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

58 lines
1.2 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');
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;
}
2018-09-20 02:56:13 +02:00
module.exports = (fp, data, opts) => {
2017-05-28 00:38:50 +02:00
if (typeof fp !== 'string') {
2018-09-20 02:56:13 +02:00
opts = data;
2017-05-28 00:38:50 +02:00
data = fp;
fp = '.';
}
2018-09-20 02:56:13 +02:00
opts = Object.assign({normalize: true}, opts, {detectIndent: true});
2017-05-28 00:38:50 +02:00
fp = path.basename(fp) === 'package.json' ? fp : path.join(fp, 'package.json');
2018-09-20 02:56:13 +02:00
data = opts.normalize ? normalize(data) : data;
2017-05-28 00:38:50 +02:00
return writeJsonFile(fp, data, opts);
};
2018-09-20 02:56:13 +02:00
module.exports.sync = (fp, data, opts) => {
2017-05-28 00:38:50 +02:00
if (typeof fp !== 'string') {
2018-09-20 02:56:13 +02:00
opts = data;
2017-05-28 00:38:50 +02:00
data = fp;
fp = '.';
}
2018-09-20 02:56:13 +02:00
opts = Object.assign({normalize: true}, opts, {detectIndent: true});
2017-05-28 00:38:50 +02:00
fp = path.basename(fp) === 'package.json' ? fp : path.join(fp, 'package.json');
2018-09-20 02:56:13 +02:00
data = opts.normalize ? normalize(data) : data;
2017-05-28 00:38:50 +02:00
writeJsonFile.sync(fp, data, opts);
};