odin-javascript-exercises/node_modules/read-pkg/index.js

42 lines
840 B
JavaScript
Raw Normal View History

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