import fs = require("fs-extra"); import co = require("co"); import prompt = require("co-prompt"); import request = require("request"); const chalk = require("chalk"); const path = require("path"); interface KeyValue { key?:String, value?:String } export class Environment { private keyValue: KeyValue; constructor(env){ const config = env? ((pair) => JSON.parse(`{ "key" : \"${pair[0]}\", "value" :\"${pair[1]}\" }` ))(env.split("=")) : {}; this.keyValue = { key : config.key, value : config.value }; } process(option){ const keyName = `RECIME_${option.toUpperCase()}_ACCESS_TOKEN`; switch(option){ case "set":{ this.save([this.keyValue]); break; } case "facebook":{ co(function *(){ const items = []; const appId = yield prompt(`Paste your App ID:\r\n`); items.push({ key : "RECIME_FACEBOOK_APP_ID", value : appId }); const secret = yield prompt(`Paste your App Secret:\r\n`); items.push({ key : "RECIME_FACEBOOK_APP_SECRET", value : secret }); const accessToken = yield prompt(`Paste your Page access token:\r\n`); items.push({ key : "RECIME_FACEBOOK_ACCESS_TOKEN", value : accessToken }); return items; }).then ((items)=>{ // valid page access token and store the page Id for use from dashboard. request({ url : "https://graph.facebook.com/v2.6/me", json : true, qs : { access_token : items[items.length -1].value } }, (err, response, body)=>{ const error = body.error || err; if (error){ console.log(chalk.red(error.message)); process.exit(1); } else { items.push({ key : "RECIME_FACEBOOK_PAGE_ID", value : body.id }); this.save(items); } }); }); break; } case "telegram":{ co(function *(){ return yield prompt(`Telegram access key:\r\n`); }).then ((value)=>{ this.save([{ key : keyName, value : value}]); }); break; } case "wechat":{ co(function *(){ return yield prompt(`WeChat access token:\r\n`); }).then ((value)=>{ this.save([{ key : keyName, value : value}]); }); break; } case "slack":{ co(function *(){ const items = []; const clientId = yield prompt(`Paste your Client ID:\r\n`); items.push({ key : "RECIME_SLACK_CLIENT_ID", value : clientId }); const secret = yield prompt(`Paste your Client Secret:\r\n`); items.push({ key : "RECIME_SLACK_CLIENT_SECRET", value : secret }); return items; }).then ((items)=>{ this.save(items); }); break; } case "viber":{ co(function *(){ return yield prompt(`Viber Auth token:\r\n`); }).then ((value)=>{ this.save([{ key : keyName, value : value}]); }); break; } } } save(items){ return new Promise((resolve, reject)=>{ const cwd = process.cwd(); const filePath = path.join(cwd, "config.json"); const dirname = path.dirname(filePath); // create the directory if (!fs.existsSync(dirname)) fs.mkdirSync(path.dirname(filePath)); fs.readJson(filePath).then((config)=>{ for (var item of items) config[item.key] = item.value; this.write(filePath, config); }).catch((err)=>{ const json = {}; for (var item of items) json[item.key] = item.value; this.write(filePath, json); }); }); } write(path, body){ fs.writeJson(path, body).then(()=>{ process.exit(0); }).catch(this.printError); } printError(err){ console.log(chalk.red(err.message)); process.exit(1) } }