mihawk
Version:
A tiny & simple mock server tool, support json,js,cjs,ts(typescript).
116 lines (115 loc) • 4.74 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readJsonSafeSync = exports.readFileSafeSync = exports.writeJSONSafeSync = exports.writeFileSafeSync = void 0;
const path_1 = require("path");
const color_cc_1 = __importDefault(require("color-cc"));
const JSON5 = __importStar(require("json5"));
const fs_extra_1 = require("fs-extra");
const consts_1 = require("../consts");
function writeFileSafeSync(filePath, data, options) {
if (!filePath) {
return;
}
filePath = (0, path_1.isAbsolute)(filePath) ? filePath : (0, path_1.resolve)(consts_1.CWD, filePath);
const dirPath = (0, path_1.dirname)(filePath);
if (!(0, fs_extra_1.existsSync)(dirPath)) {
(0, fs_extra_1.ensureDirSync)(dirPath);
}
options = typeof options === 'string' ? { encoding: options } : options;
(0, fs_extra_1.writeFileSync)(filePath, data, Object.assign({ encoding: 'utf-8' }, options));
}
exports.writeFileSafeSync = writeFileSafeSync;
function writeJSONSafeSync(jsonFilePath, obj, options) {
if (!jsonFilePath) {
return;
}
jsonFilePath = (0, path_1.isAbsolute)(jsonFilePath) ? jsonFilePath : (0, path_1.resolve)(consts_1.CWD, jsonFilePath);
const dirPath = (0, path_1.dirname)(jsonFilePath);
if (!(0, fs_extra_1.existsSync)(dirPath)) {
(0, fs_extra_1.ensureDirSync)(dirPath);
}
options = typeof options === 'string' ? { encoding: options } : options;
(0, fs_extra_1.writeJSONSync)(jsonFilePath, obj, Object.assign({ spaces: 2, encoding: 'utf-8' }, options));
}
exports.writeJSONSafeSync = writeJSONSafeSync;
function readFileSafeSync(filePath, options) {
if (!filePath) {
return '';
}
filePath = (0, path_1.isAbsolute)(filePath) ? filePath : (0, path_1.resolve)(consts_1.CWD, filePath);
if (!(0, fs_extra_1.existsSync)(filePath)) {
console.log(color_cc_1.default.warn(`The target file path is not existed!`), filePath);
return '';
}
let opts = { encoding: 'utf-8' };
const typeOfOptions = typeof options;
if (typeOfOptions === 'object' && options !== null) {
opts = Object.assign(Object.assign({}, opts), options);
}
if (typeOfOptions === 'string') {
opts.encoding = options;
}
try {
return (0, fs_extra_1.readFileSync)(filePath, opts);
}
catch (error) {
console.log(color_cc_1.default.warn(`Read file error! filePath=${filePath}`), error);
return '';
}
}
exports.readFileSafeSync = readFileSafeSync;
function readJsonSafeSync(jsonFilePath, options) {
if (!jsonFilePath) {
return null;
}
let json = null;
jsonFilePath = (0, path_1.isAbsolute)(jsonFilePath) ? jsonFilePath : (0, path_1.resolve)(consts_1.CWD, jsonFilePath);
if (!(0, fs_extra_1.existsSync)(jsonFilePath)) {
console.log(color_cc_1.default.warn(`The target file path is not existed!`), jsonFilePath);
return json;
}
let opts = { encoding: 'utf-8' };
const typeOfOptions = typeof options;
if (typeOfOptions === 'object' && options !== null) {
opts = Object.assign(Object.assign({}, opts), options);
}
if (typeOfOptions === 'string') {
opts.encoding = options;
}
try {
const content = (0, fs_extra_1.readFileSync)(jsonFilePath, opts);
json = JSON5.parse(content);
}
catch (error) {
console.error(color_cc_1.default.error(`Parse JsonFile Error: ${jsonFilePath}`), error, '\n');
json = {};
}
return json;
}
exports.readJsonSafeSync = readJsonSafeSync;