2017-12-15 10:56:14 -08:00
|
|
|
'use strict';
|
2021-05-08 05:39:21 -07:00
|
|
|
const {promisify} = require('util');
|
|
|
|
const fs = require('fs');
|
2017-12-15 10:56:14 -08:00
|
|
|
const path = require('path');
|
2021-05-08 05:39:21 -07:00
|
|
|
const parseJson = require('parse-json');
|
2017-12-15 10:56:14 -08:00
|
|
|
|
2021-05-08 05:39:21 -07:00
|
|
|
const readFileAsync = promisify(fs.readFile);
|
2017-12-15 10:56:14 -08:00
|
|
|
|
2021-05-08 05:39:21 -07:00
|
|
|
module.exports = async options => {
|
|
|
|
options = {
|
|
|
|
cwd: process.cwd(),
|
|
|
|
normalize: true,
|
|
|
|
...options
|
|
|
|
};
|
2017-12-15 10:56:14 -08:00
|
|
|
|
2021-05-08 05:39:21 -07:00
|
|
|
const filePath = path.resolve(options.cwd, 'package.json');
|
|
|
|
const json = parseJson(await readFileAsync(filePath, 'utf8'));
|
2017-12-15 10:56:14 -08:00
|
|
|
|
2021-05-08 05:39:21 -07:00
|
|
|
if (options.normalize) {
|
|
|
|
require('normalize-package-data')(json);
|
|
|
|
}
|
2017-12-15 10:56:14 -08:00
|
|
|
|
2021-05-08 05:39:21 -07:00
|
|
|
return json;
|
2017-12-15 10:56:14 -08:00
|
|
|
};
|
|
|
|
|
2021-05-08 05:39:21 -07:00
|
|
|
module.exports.sync = options => {
|
|
|
|
options = {
|
|
|
|
cwd: process.cwd(),
|
|
|
|
normalize: true,
|
|
|
|
...options
|
|
|
|
};
|
2017-12-15 10:56:14 -08:00
|
|
|
|
2021-05-08 05:39:21 -07:00
|
|
|
const filePath = path.resolve(options.cwd, 'package.json');
|
|
|
|
const json = parseJson(fs.readFileSync(filePath, 'utf8'));
|
2017-12-15 10:56:14 -08:00
|
|
|
|
2021-05-08 05:39:21 -07:00
|
|
|
if (options.normalize) {
|
|
|
|
require('normalize-package-data')(json);
|
2017-12-15 10:56:14 -08:00
|
|
|
}
|
|
|
|
|
2021-05-08 05:39:21 -07:00
|
|
|
return json;
|
2017-12-15 10:56:14 -08:00
|
|
|
};
|