@modern-js/codesmith-api-json
Version:
codesmith json api
127 lines (126 loc) • 4.4 kB
JavaScript
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import commentJSON from "comment-json";
import * as declarationUpdate from "declaration-update";
import { editJson } from "./utils";
var JsonAPI = /* @__PURE__ */ function() {
"use strict";
function JsonAPI2(generatorCore) {
_class_call_check(this, JsonAPI2);
this.generatorCore = generatorCore;
}
var _proto = JsonAPI2.prototype;
_proto.get = function get(resource) {
var _this = this;
return _async_to_generator(function() {
var originJsonValue, origin;
return _ts_generator(this, function(_state) {
switch (_state.label) {
case 0:
return [
4,
resource.value()
];
case 1:
originJsonValue = _state.sent();
try {
origin = commentJSON.parse(originJsonValue.content);
return [
2,
origin
];
} catch (e) {
_this.generatorCore.logger.debug("❗️ [JSON Get Parse Error]:", e);
throw new Error("resource content is not a legal json");
}
return [
2
];
}
});
})();
};
_proto.extend = function extend(resource, obj) {
var endWithNewLine = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
var _this = this;
return _async_to_generator(function() {
return _ts_generator(this, function(_state) {
switch (_state.label) {
case 0:
return [
4,
editJson(_this.generatorCore, resource, /* @__PURE__ */ _async_to_generator(function() {
var originJsonValue, origin, newObj, jsonIntent;
return _ts_generator(this, function(_state2) {
switch (_state2.label) {
case 0:
return [
4,
resource.value()
];
case 1:
originJsonValue = _state2.sent();
try {
origin = commentJSON.parse(originJsonValue.content);
newObj = commentJSON.assign(origin, obj);
jsonIntent = 2;
return [
2,
commentJSON.stringify(newObj, void 0, jsonIntent) + (endWithNewLine ? "\n" : "")
];
} catch (e) {
_this.generatorCore.logger.debug("❗️ [JSON Extend Parse Error]:", e);
throw new Error("resource content is not a legal json");
}
return [
2
];
}
});
}))
];
case 1:
_state.sent();
return [
2
];
}
});
})();
};
_proto.update = function update(resource, operation) {
var endWithNewLine = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
var _this = this;
return _async_to_generator(function() {
return _ts_generator(this, function(_state) {
switch (_state.label) {
case 0:
return [
4,
editJson(_this.generatorCore, resource, function(text) {
try {
var jsonContent = commentJSON.parse(text);
declarationUpdate.query(jsonContent, operation.query, operation.update);
var jsonIntent = 2;
return Promise.resolve(commentJSON.stringify(jsonContent, void 0, jsonIntent) + (endWithNewLine ? "\n" : ""));
} catch (e) {
_this.generatorCore.logger.debug("❗️ [JSON Update Parse Error]:", e);
throw new Error("resource content is not a legal json");
}
})
];
case 1:
_state.sent();
return [
2
];
}
});
})();
};
return JsonAPI2;
}();
export {
JsonAPI
};