codingame-connector
Version:
An interface for Codingame website
213 lines (193 loc) • 5.48 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _assign = require('babel-runtime/core-js/object/assign');
var _assign2 = _interopRequireDefault(_assign);
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _readline = require('readline');
var _readline2 = _interopRequireDefault(_readline);
var _child_process = require('child_process');
var _child_process2 = _interopRequireDefault(_child_process);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var parameters = {};
/**
* Load the configuration file
*
* @name load
* @function
* @param {string} path - Path of the configuration file
* @param {Object} [options] - Additionnal parameters which will replace parameters from configuration file
* @returns {Promise<Object>} Configuration parameters
* @memberof module:configure
* @instance
*/
/**
* @file Module 'configure'
* @author woshilapin <woshilapin@tuziwo.info>
*/
/**
* Manage global configuration for Codingame's connector
* @module configure
*/
var load = function load(path, options) {
return new _promise2.default(function (resolve, reject) {
var resolvefromerror = function resolvefromerror(error) {
if (options !== undefined && typeof options === 'object') {
(0, _assign2.default)(parameters, options);
resolve(options);
} else {
reject(error);
}
};
try {
_fs2.default.readFile(path, 'utf8', function (error, file) {
if (error) {
resolvefromerror(error);
} else {
try {
parameters = JSON.parse(file);
} catch (error) {
reject(error);
}
(0, _assign2.default)(parameters, options);
resolve(parameters);
}
});
} catch (error) {
resolvefromerror(error);
}
});
};
/**
* Get result of a shell command
*
* @name getShell
* @function
* @param {string} cmd - Shell command to run
* @returns {Promise<string>} A promise of the output of the shell command
*/
var getShell = function getShell(cmd) {
return new _promise2.default(function (resolve, reject) {
_child_process2.default.exec(cmd, function (error, stdout, stderr) {
if (error) {
console.error(stderr);
reject(error);
} else {
var result = stdout.trim();
resolve(result);
}
});
});
};
/**
* Get content of a file
*
* @name getFile
* @function
* @param {string} path - Path to the file
* @returns {Promise<Object>} A promise of an object with `path` and `content`
*/
var getFile = function getFile(path) {
return new _promise2.default(function (resolve, reject) {
_fs2.default.readFile(path, 'utf8', function (error, data) {
if (error) {
console.error(error.message);
reject(error);
} else {
resolve({
"path": path,
"data": data
});
}
});
});
};
/**
* Get answer to a question asked to the end-user
*
* @name getQuestion
* @function
* @param {string} question - Question to ask to the end-user
* @returns {Promise<string>} A promise of the answer of the end-user
*/
var getQuestion = function getQuestion(question) {
return new _promise2.default(function (resolve, reject) {
var rl = _readline2.default.createInterface({
"input": process.stdin,
"output": process.stdout
});
if (question !== undefined && typeof question === 'string') {
rl.question(question, function (result) {
rl.close();
resolve(result);
});
} else {
rl.close();
reject(new Error('\'configure.get()\' second parameter must be a string.'));
}
});
};
/**
* Get the value from configuration
*
* @name get
* @function
* @param {string} name - Name of the parameter
* @param {string} [option] - 'shell' if value may be executed as shell command, 'file' if value is a path and content of file should be returned
* @param {string} [question] - If present and value not found, will be shown to the user to ask for the value on 'stdin'
* @returns {Promise<string|Array|Object>} The value of the parameter
* @memberof module:configure
* @instance
*/
var get = function get(name, option, question) {
return new _promise2.default(function (resolve, reject) {
if (!name || typeof name !== 'string') {
reject(new Error('\'configure.get()\' takes at least one string parameter.'));
}
var property = parameters[name];
if (property !== undefined && option !== undefined && option === 'shell' && Array.isArray(property)) {
getShell(property.join(' ')).then(function (result) {
parameters[name] = result;
resolve(result);
}, function (error) {
reject(error);
});
} else if (property !== undefined && option !== undefined && option === 'file') {
resolve(getFile(property));
} else if (property !== undefined) {
resolve(property);
} else {
getQuestion(question).then(function (result) {
parameters[name] = result;
resolve(result);
}, function (error) {
reject(error);
});
}
});
};
/**
* Ask configure module to delete a parameter
*
* @name forget
* @function
* @param {string} name - Name of the parameter
* @memberof module:configure
* @instance
*/
var forget = function forget(name) {
if (typeof name !== 'string') {
throw new Error('\'configure.forget()\' function takes one string parameter.');
} else {
delete parameters[name];
}
};
exports.default = {
"load": load,
"get": get,
"forget": forget
};
;