UNPKG

2.88 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5var __importStar = (this && this.__importStar) || function (mod) {
6 if (mod && mod.__esModule) return mod;
7 var result = {};
8 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
9 result["default"] = mod;
10 return result;
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13const path_1 = __importDefault(require("path"));
14const util_1 = __importDefault(require("util"));
15const child_process_1 = __importDefault(require("child_process"));
16const state_1 = require("./state");
17const config_1 = require("./config");
18const request = __importStar(require("./request"));
19const types_1 = require("./types");
20const pExec = util_1.default.promisify(child_process_1.default.exec);
21async function exec(command) {
22 try {
23 return pExec(command);
24 }
25 catch (err) {
26 return err;
27 }
28}
29exports.exec = exec;
30function getHostOS() {
31 return process.env.OPS_HOST_PLATFORM || 'unknown';
32}
33exports.getHostOS = getHostOS;
34function getInterfaceType() {
35 return process.env.SDK_INTERFACE_TYPE || types_1.Interfaces.Terminal;
36}
37exports.getInterfaceType = getInterfaceType;
38function homeDir() {
39 return process.env.SDK_HOME_DIR || '/root';
40}
41exports.homeDir = homeDir;
42function log(...args) {
43 console.log(...args);
44}
45exports.log = log;
46function getStatePath() {
47 return path_1.default.resolve(process.env.SDK_STATE_DIR || '');
48}
49exports.getStatePath = getStatePath;
50function getConfigPath() {
51 return path_1.default.resolve(process.env.SDK_CONFIG_DIR || '');
52}
53exports.getConfigPath = getConfigPath;
54async function setState(key, value) {
55 const state = new state_1.State(getStatePath());
56 await state.set(key, value);
57}
58exports.setState = setState;
59async function getState(key) {
60 const state = new state_1.State(getStatePath());
61 return await state.get(key);
62}
63exports.getState = getState;
64async function setConfig(key, value) {
65 const config = new config_1.Config(getConfigPath());
66 await config.set(key, value);
67}
68exports.setConfig = setConfig;
69async function getConfig(key) {
70 const config = new config_1.Config(getConfigPath());
71 return await config.get(key);
72}
73exports.getConfig = getConfig;
74async function getSecret(key) {
75 return await request.getSecret(key);
76}
77exports.getSecret = getSecret;
78async function setSecret(key, value) {
79 return await request.setSecret(key, value);
80}
81exports.setSecret = setSecret;
82// We expect to add an 'event' field soon
83async function track(tags, metadata) {
84 try {
85 await request.track(Object.assign({ tags: Array.isArray(tags) ? tags : [tags] }, metadata));
86 }
87 catch (e) {
88 // Do something with this error eventually
89 }
90}
91exports.track = track;