line-api-cli
Version:
LINE API CLIs for Node.js
112 lines (85 loc) • 2.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("../typedef");
var _fs = _interopRequireDefault(require("fs"));
var _jsYaml = _interopRequireDefault(require("js-yaml"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
class Operation {
static get configFileName() {
return '.line-api-cli.yml';
}
/** @type {Config} */
static get config() {
if (!this._config) {
let configFile;
try {
configFile = _fs.default.readFileSync(`./${this.configFileName}`);
} catch (_) {
console.log(`Run command ${`line init`.code} to initialize project configuration file`.help);
process.exit(0);
}
try {
this._config = _jsYaml.default.load(configFile);
} catch (error) {
console.log('Unable to safe load configuration file', error);
process.exit(1);
}
}
return this._config;
}
static validateConfig() {
if (!this.config.channel.id || !this.config.channel.secret) {
console.log(`Run command ${`line init`.code} to initialize project configuration file`.help);
return false;
}
if (!this.config.channel.accessToken) {
console.log(`Run command ${`line token --issue`.code} and save access token project configuration file`.help);
return false;
}
return true;
}
static validateFileExists(value) {
if (value) {
return _fs.default.existsSync(value) === true ? true : 'File not exists';
} else {
return 'Please input data file location to proceed.';
}
}
static logAxiosError(error) {
if (error.isAxiosError) {
console.log(`${error.response.status}, ${error.response.statusText}`.error);
if (error.response.data && typeof error.response.data.detail === 'string') {
console.log(`${error.response.data.detail}`.warn);
}
} else {
console.log(error.message.error);
}
return;
}
/**
*
* @param {import('axios').AxiosResponse} response
*/
static logAxiosResponse(response) {
if (!response.data) {
console.log(`${response.status}, ${response.statusText}`.code);
return;
}
if (typeof response.data === 'object') {
console.log(JSON.stringify(response.data, null, 2));
} else {
console.log(`${response.data}`.code);
}
return;
}
}
exports.default = Operation;
_defineProperty(Operation, "cancelOption", {
onCancel: () => process.exit(0)
});
_defineProperty(Operation, "_config", void 0);
//# sourceMappingURL=operation.js.map