UNPKG

cloudworker-proxy

Version:
120 lines (95 loc) 3.82 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLoggedInUser = exports.writeConfigFile = exports.readConfigFile = exports.getConfigFilePath = undefined; var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _os = require('os'); var _os2 = _interopRequireDefault(_os); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _v = require('uuid/v1'); var _v2 = _interopRequireDefault(_v); var _ramda = require('ramda'); var _writeFileAtomic = require('write-file-atomic'); var _writeFileAtomic2 = _interopRequireDefault(_writeFileAtomic); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // Locate the correct .serverlessrc per current environment var fileName = 'serverless'; if (process.env.SERVERLESS_PLATFORM_STAGE && process.env.SERVERLESS_PLATFORM_STAGE !== 'prod') { fileName = 'serverless' + process.env.SERVERLESS_PLATFORM_STAGE.toLowerCase(); fileName = fileName.trim(); } /* * Get Config File Path * - .serverlessrc can either be in the current working dir or system root dir. * - This function returns the local path first, if it exists. */ var getConfigFilePath = exports.getConfigFilePath = function getConfigFilePath() { var localPath = _path2.default.join(process.cwd(), `.${fileName}rc`); var globalPath = _path2.default.join(_os2.default.homedir(), `.${fileName}rc`); var localConfigExists = _fs2.default.existsSync(localPath); var globalConfigExists = _fs2.default.existsSync(globalPath); if (localConfigExists) { return localPath; } else if (globalConfigExists) { return globalPath; } // If neither exist, create the config file in the home dir // Normally the Framework does this, but just in case... var config = { userId: null, frameworkId: (0, _v2.default)(), trackingDisabled: false, // default false meta: { created_at: Math.round(+new Date() / 1000), // config file creation date updated_at: null // config file updated date } }; _writeFileAtomic2.default.sync(globalPath, JSON.stringify(config, null, 2)); return globalPath; }; /* * Read Config File * - The Framework always creates a config file on post-install via the logstat method. (This isn't optimal and should be changed in the Framework.) * - The "rc" package automatically looks in many places (local folder, up a few levels, root dir) */ var readConfigFile = exports.readConfigFile = function readConfigFile() { var configFilePath = getConfigFilePath(); var configFile = configFilePath ? _fs2.default.readFileSync(configFilePath) : null; return configFile ? JSON.parse(configFile) : {}; }; /* * Write Config File * - Writes a .serverlessrc file on the local machine in the root dir */ var writeConfigFile = exports.writeConfigFile = function writeConfigFile(data) { var configFilePath = getConfigFilePath(); var configFile = readConfigFile(); var updatedConfigFile = (0, _ramda.mergeDeepRight)(configFile, data); updatedConfigFile.meta.updated_at = Math.round(+new Date() / 1000); _writeFileAtomic2.default.sync(configFilePath, JSON.stringify(updatedConfigFile, null, 2)); return updatedConfigFile; }; /* * Get Logged In User * - Fetches the current logged in user from the .serverlessrc file */ var getLoggedInUser = exports.getLoggedInUser = function getLoggedInUser() { var config = readConfigFile(); if (!config.userId) { return null; } var user = (0, _ramda.path)(['users', config.userId, 'dashboard'], config); if (!user || !user.username) { return null; // user is logged out } return { userId: config.userId, username: user.username, accessKeys: user.accessKeys, idToken: user.idToken }; }; //# sourceMappingURL=configFile.js.map