UNPKG

@knapsack/app

Version:

Build Design Systems on top of knapsack, by Basalt

148 lines (124 loc) 5.63 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.KsCloudConnect = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _core = require("@knapsack/core"); var _urlJoin = _interopRequireDefault(require("url-join")); var _path = require("path"); var _log = require("../cli/log"); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } class KsCloudConnect { constructor(cloudConfig) { (0, _defineProperty2.default)(this, "cloudConfig", void 0); (0, _defineProperty2.default)(this, "saveFilesToCloud", async ({ files, title = 'New changes', message, user }) => { var _user$role, _user$role$permission; if (!this.cloudConfig) { return { ok: false, message: 'No "cloud" in your "knapsack.config.js"' }; } if (!(user === null || user === void 0 ? void 0 : (_user$role = user.role) === null || _user$role === void 0 ? void 0 : (_user$role$permission = _user$role.permissions) === null || _user$role$permission === void 0 ? void 0 : _user$role$permission.includes(_core.PERMISSIONS.WRITE))) { return { ok: false, message: `Your user does not have write permission, sorry.` }; } const { // apiKey, apiBase, repoRoot, repoName, repoOwner, baseBranch = 'master', alterSavedFilePath } = this.cloudConfig; const repo = `${repoOwner}/${repoName}`; // @todo re-enable repo access check. disabled b/c adding users to user involved manual api call. // if (!user.ksRepoAccess.includes(repo)) { // return { // ok: false, // message: `Your user does not have permission to write to the "${repo}" repo, only these: ${user.ksRepoAccess?.join( // ', ', // )}`, // }; // } function prepFilePath(filePath) { const fullPath = (0, _path.isAbsolute)(filePath) ? filePath : (0, _path.join)(process.cwd(), filePath); return (0, _path.relative)(repoRoot, fullPath); } const body = { owner: repoOwner, repo: repoName, baseBranch, title: `${title} from ${user.username}`, message, payload: { files: files.map(file => { const filePath = alterSavedFilePath ? alterSavedFilePath(file.path) : prepFilePath(file.path); if (typeof filePath !== 'string') { console.log(file); const msg = `While prepping for file save, this file did not end up as a string, in: "${file.path}", out: "${filePath}".`; (0, _log.error)(msg, null, 'cloud'); throw new Error(msg); } if ((0, _path.isAbsolute)(filePath)) { console.log(file); const msg = `While prepping for file save, this file did not end up as a relative path, in: "${file.path}", out: "${filePath}".`; (0, _log.error)(msg, null, 'cloud'); throw new Error(msg); } return _objectSpread({}, file, { path: filePath }); }) } }; // console.log('Sending save request to Knapsack Cloud...'); // console.log('files paths:'); // console.log(body.payload.files.map(file => file.path).join('\n')); const endpoint = (0, _urlJoin.default)(apiBase, 'api/save'); return fetch(endpoint, { method: 'POST', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json', Accept: 'application/json' // can't add auth just yet, cloud api uses it if present to attempt to authenticate as GitHub user (then falls back to GitHub app); this auth token is currently the AWS Cognito user. // Authorization: user.Authorization, // 'x-api-key': apiKey, } }).then(res => { const { ok, status, statusText } = res; // console.log({ ok, status, statusText }); if (!ok) { const result = { ok, message: `${status} - ${statusText}` }; return result; } return res.json(); }).catch(e => { console.error('error saveFilesToCloud'); console.error(e); const result = { ok: false, message: `thrown error in saveFilesToCloud: ${e.message}` }; return result; }); }); this.cloudConfig = cloudConfig; // this.saveFilesToCloud = this.saveFilesToCloud.bind(this); } } exports.KsCloudConnect = KsCloudConnect;