alwaysai
Version:
The alwaysAI command-line interface (CLI)
122 lines • 6.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.targetJsonComponent = void 0;
const logSymbols = require("log-symbols");
const alwayscli_1 = require("@alwaysai/alwayscli");
const destination_prompt_component_1 = require("./destination-prompt-component");
const target_path_prompt_component_1 = require("./target-path-prompt-component");
const target_protocol_prompt_component_1 = require("./target-protocol-prompt-component");
const write_target_json_file_component_1 = require("./write-target-json-file-component");
const constants_1 = require("../../../constants");
const device_1 = require("../../device");
const docker_1 = require("../../docker");
const util_1 = require("../../../util");
const app_1 = require("../../../core/app");
const project_1 = require("../../../core/project");
const general_1 = require("../../general");
const environment_1 = require("../../../environment");
async function targetJsonComponent(props) {
var _a, _b, _c, _d, _e, _f;
const { yes } = props;
const currentTargetJson = (0, app_1.TargetJsonFile)();
const currentTargetProtocol = currentTargetJson.readTargetProtocolSafe();
let targetProtocol;
if (yes) {
targetProtocol = (_a = props.targetProtocol) !== null && _a !== void 0 ? _a : currentTargetProtocol;
if (!targetProtocol) {
throw new alwayscli_1.CliUsageError((0, util_1.RequiredWithYesMessage)('protocol'));
}
if (targetProtocol === 'native:' || targetProtocol === 'docker:') {
if (!(await (0, destination_prompt_component_1.checkProcessorBrand)())) {
throw new alwayscli_1.CliUsageError('edgeIQ is not supported on devices with Atom/Celeron processors.');
}
}
}
else if (!props.targetProtocol) {
targetProtocol = await (0, target_protocol_prompt_component_1.targetProtocolPromptComponent)({
prevTargetProtocol: currentTargetProtocol
});
}
else {
targetProtocol = props.targetProtocol;
}
switch (targetProtocol) {
case 'native:': {
await (0, write_target_json_file_component_1.writeTargetJsonFileComponent)({
targetProtocol
});
break;
}
case 'docker:': {
await (0, docker_1.checkForDockerComponent)();
// Order of precedence: Command line > environment variable >
// auto-determine based on system
const targetHardware = (_c = (_b = props.targetHardware) !== null && _b !== void 0 ? _b : (0, app_1.parseTargetHW)(environment_1.ALWAYSAI_TARGET_HW_OVERRIDE)) !== null && _c !== void 0 ? _c : (await (0, app_1.getTargetHardwareType)({}));
(0, util_1.echo)(`${logSymbols.success} Set target hardware to ${targetHardware}`);
await (0, write_target_json_file_component_1.writeTargetJsonFileComponent)({
targetProtocol,
dockerImageId: constants_1.DOCKER_IMAGE_ID_INITIAL_VALUE,
targetHardware
});
break;
}
case 'ssh+docker:': {
await (0, general_1.findOrWritePrivateKeyFileComponent)({ yes });
const projectJsonFile = (0, project_1.ProjectJsonFile)();
const projectConfig = projectJsonFile.read().project;
if (!projectConfig) {
throw new alwayscli_1.CliTerseError(`App must have a project associated with it. Run '${constants_1.ALWAYSAI_CLI_EXECUTABLE_NAME} app configure' to select a project.`);
}
const projectId = projectConfig.id;
const deviceSelectOut = await (0, device_1.deviceSelectComponent)({
yes,
deviceId: props.deviceId
});
const device = deviceSelectOut.device;
const isNewDevice = deviceSelectOut.new;
const targetHostname = await (0, device_1.getTargetHostnameComponent)({
yes,
device,
targetHostname: props.targetHostname
});
if (targetHostname === undefined) {
throw new alwayscli_1.CliTerseError('Cannot access selected device! The device hostname is unknown.');
}
await (0, device_1.deviceCheckAndUpdateComponent)({
yes,
targetHostname,
device,
isNewDevice
});
await (0, project_1.addDeviceToProject)({ device, projectId });
await (0, docker_1.checkForDockerComponent)({ targetHostname });
let targetPath = (_e = (_d = props.targetPath) !== null && _d !== void 0 ? _d : currentTargetJson.readFieldSafe({ name: 'targetPath' })) !== null && _e !== void 0 ? _e : (0, app_1.TargetPathDefaultValue)();
if (!yes) {
targetPath = await (0, target_path_prompt_component_1.targetPathPromptComponent)({
targetHostname,
targetPath
});
}
if ((0, app_1.parseTargetHW)(environment_1.ALWAYSAI_TARGET_HW_OVERRIDE) !== undefined) {
const msg = 'ALWAYSAI_TARGET_HW set but not used for remote device';
(0, util_1.echo)(`${logSymbols.warning} ${msg}`);
util_1.logger.warn(msg);
}
const targetHardware = (_f = props.targetHardware) !== null && _f !== void 0 ? _f : (await (0, app_1.getTargetHardwareType)({ targetHostname }));
(0, util_1.echo)(`${logSymbols.success} Set target hardware to ${targetHardware}`);
(0, app_1.TargetJsonFile)().write({
targetProtocol,
targetHostname,
targetPath,
dockerImageId: constants_1.DOCKER_IMAGE_ID_INITIAL_VALUE,
targetHardware,
deviceId: device.uuid
});
break;
}
default:
throw new Error('Unsupported protocol');
}
}
exports.targetJsonComponent = targetJsonComponent;
//# sourceMappingURL=target-json-component.js.map