clientHelper.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.envObjectToArray = envObjectToArray;
  6. exports.evaluationScript = evaluationScript;
  7. var _fs = _interopRequireDefault(require("fs"));
  8. var _utils = require("../utils");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * Copyright 2017 Google Inc. All rights reserved.
  12. * Modifications copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. function envObjectToArray(env) {
  27. const result = [];
  28. for (const name in env) {
  29. if (!Object.is(env[name], undefined)) result.push({
  30. name,
  31. value: String(env[name])
  32. });
  33. }
  34. return result;
  35. }
  36. async function evaluationScript(fun, arg, addSourceUrl = true) {
  37. if (typeof fun === 'function') {
  38. const source = fun.toString();
  39. const argString = Object.is(arg, undefined) ? 'undefined' : JSON.stringify(arg);
  40. return `(${source})(${argString})`;
  41. }
  42. if (arg !== undefined) throw new Error('Cannot evaluate a string with arguments');
  43. if ((0, _utils.isString)(fun)) return fun;
  44. if (fun.content !== undefined) return fun.content;
  45. if (fun.path !== undefined) {
  46. let source = await _fs.default.promises.readFile(fun.path, 'utf8');
  47. if (addSourceUrl) source += '\n//# sourceURL=' + fun.path.replace(/\n/g, '');
  48. return source;
  49. }
  50. throw new Error('Either path or content property must be present');
  51. }