2020-03-07 11:45:40 +08:00
|
|
|
'use strict';
|
2020-03-31 20:57:48 +08:00
|
|
|
const {promisify} = require('util');
|
|
|
|
const fs = require('fs');
|
2020-03-07 11:45:40 +08:00
|
|
|
const path = require('path');
|
2020-03-31 20:57:48 +08:00
|
|
|
const parseJson = require('parse-json');
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
const readFileAsync = promisify(fs.readFile);
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
module.exports = async options => {
|
|
|
|
options = {
|
|
|
|
cwd: process.cwd(),
|
|
|
|
normalize: true,
|
|
|
|
...options
|
|
|
|
};
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
const filePath = path.resolve(options.cwd, 'package.json');
|
|
|
|
const json = parseJson(await readFileAsync(filePath, 'utf8'));
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
if (options.normalize) {
|
|
|
|
require('normalize-package-data')(json);
|
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
return json;
|
2020-03-07 11:45:40 +08:00
|
|
|
};
|
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
module.exports.sync = options => {
|
|
|
|
options = {
|
|
|
|
cwd: process.cwd(),
|
|
|
|
normalize: true,
|
|
|
|
...options
|
|
|
|
};
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
const filePath = path.resolve(options.cwd, 'package.json');
|
|
|
|
const json = parseJson(fs.readFileSync(filePath, 'utf8'));
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
if (options.normalize) {
|
|
|
|
require('normalize-package-data')(json);
|
2020-03-07 11:45:40 +08:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:57:48 +08:00
|
|
|
return json;
|
2020-03-07 11:45:40 +08:00
|
|
|
};
|