t-comm
Version:
专业、稳定、纯粹的工具库
55 lines (48 loc) • 1.36 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var fs = require('fs');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
/**
* 解析带注释的 json 文件
* @param content 原始文件内容
* @returns json数据
*/
function parseCommentJson(content) {
var newContent = content.replace(/(?:\s+|^)\/\/[^\n]*/g, '');
// console.log('[newContent]', newContent);
var json = {};
try {
json = JSON.parse(newContent);
} catch (err) {}
return json;
}
/**
* 获取带注释的 json 文件内容
* @param file 文件路径
* @returns json数据
*/
function readCommentJson(file) {
var content = fs__namespace.readFileSync(file, {
encoding: 'utf-8'
});
return parseCommentJson(content);
}
exports.parseCommentJson = parseCommentJson;
exports.readCommentJson = readCommentJson;