alwaysai
Version:
The alwaysAI command-line interface (CLI)
98 lines • 4.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.appPublishComponent = void 0;
const alwayscli_1 = require("@alwaysai/alwayscli");
const crypto = require("crypto");
const constants_1 = require("../../constants");
const user_1 = require("../user");
const util_1 = require("../../util");
const app_check_component_1 = require("./app-check-component");
const app_configure_component_1 = require("./app-configure-component");
const project_1 = require("../../core/project");
const infrastructure_1 = require("../../infrastructure");
const app_install_component_1 = require("./app-install-component");
const app_invalid_models_check_component_1 = require("./models/app-invalid-models-check-component");
async function appPublishComponent(props) {
const { yes, name, excludes } = props;
await (0, user_1.checkUserIsLoggedInComponent)({ yes });
try {
await (0, app_check_component_1.appCheckComponent)({ ignoreTargetJsonFile: true });
}
catch (err) {
if (yes) {
throw new alwayscli_1.CliUsageError(`App is not properly configured. Did you run \`${constants_1.ALWAYSAI_CLI_EXECUTABLE_NAME} app configure\`?`);
}
else {
await (0, app_configure_component_1.appConfigureComponent)({ yes });
}
}
const invalidModels = await (0, app_invalid_models_check_component_1.checkForInvalidModelsComponent)();
if (Object.keys(invalidModels).length > 0) {
throw new alwayscli_1.CliTerseError(`You do not have permission to use the following models, or the model version does not exist:\n\n` +
`${Object.entries(invalidModels)
.map(([model, version]) => `- ${model}: version ${version}`)
.join('\n')}\n\n` +
`Please remove these models before publishing the application again.`);
}
const applicationPackage = await (0, util_1.runWithSpinner)(createApplicationFiles, [{ excludes: excludes || [] }], 'Create application package');
await (0, util_1.runWithSpinner)(publishApplicationPackage, [applicationPackage, name], 'Publish application package');
return applicationPackage.releaseHash;
}
exports.appPublishComponent = appPublishComponent;
async function createApplicationFiles(params) {
const { excludes } = params;
// Get project ID
const projectJsonFile = (0, project_1.ProjectJsonFile)().read();
const projectId = projectJsonFile.project
? projectJsonFile.project.id
: undefined;
if (projectId === undefined) {
throw new alwayscli_1.CliTerseError('Please set up a project for this app');
}
// Put src in tarball
const spawner = (0, util_1.JsSpawner)();
const ignore = app_install_component_1.APP_IGNORE_FILES.concat(['alwaysai.target.json'], excludes);
const tarfile = await (0, util_1.tarFiles)(spawner, ignore);
const tarBuffer = await (0, util_1.streamToBuffer)(tarfile);
// Generate release hash
const hashSum = crypto.createHash('sha256');
hashSum.update(tarBuffer);
const releaseHash = hashSum.digest('hex');
const releaseDate = new Date();
const filename = `${projectId}/${releaseHash}.tgz`;
// Generate release manifest
const releaseData = {
releaseHash,
releaseDate,
filename
};
const releaseManifest = JSON.stringify(releaseData, null, 2);
const applicationPackage = {
filename,
tarBuffer,
releaseManifest,
projectId,
releaseHash
};
return applicationPackage;
}
async function publishApplicationPackage(applicationPackage, name) {
const fileData = {
tarFileName: applicationPackage.filename,
tarFile: applicationPackage.tarBuffer,
releaseManifestName: `${applicationPackage.projectId}/release.json`,
releaseManifestFile: applicationPackage.releaseManifest
};
const presignedUrlTarFile = await (0, infrastructure_1.getPresignedUrlAppUpload)(fileData.tarFileName, 'application/octet-stream');
const presignedUrlManifestFile = await (0, infrastructure_1.getPresignedUrlAppUpload)(fileData.releaseManifestName, 'application/octet-stream');
await (0, infrastructure_1.usePresignedUrlAppUpload)(presignedUrlTarFile.uploadURL, fileData.tarFile);
await (0, infrastructure_1.usePresignedUrlAppUpload)(presignedUrlManifestFile.uploadURL, fileData.releaseManifestFile);
const record = {
hash: applicationPackage.releaseHash,
s3Path: applicationPackage.filename,
projectId: await (0, project_1.getCurrentProjectId)(),
name: name !== null && name !== void 0 ? name : ''
};
await (0, infrastructure_1.insertAppRecord)(record);
}
//# sourceMappingURL=app-publish-component.js.map