toystack
Version:

219 lines (218 loc) • 8 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCLIFrontendDeployment = exports.uploadUserCode = exports.deployBranch = exports.listAllRepositories = void 0;
exports.pullForDeploymentStatus = pullForDeploymentStatus;
const client_1 = require("@apollo/client");
const prompts_1 = require("@clack/prompts");
const axios_1 = __importDefault(require("axios"));
const dotenv_1 = __importDefault(require("dotenv"));
const form_data_1 = __importDefault(require("form-data"));
const fs_1 = require("fs");
const process_1 = __importDefault(require("process"));
const __1 = require("..");
dotenv_1.default.config();
const API_BASE_URL = process_1.default.env.API_BASE_URL || 'https://core.toystack.ai';
const listAllRepositories = () => __awaiter(void 0, void 0, void 0, function* () {
const token = yield (0, __1.checkAuth)();
const client = new client_1.ApolloClient({
link: new client_1.HttpLink({
uri: `${API_BASE_URL}/graphql`,
fetch,
headers: { Authorization: `Bearer ${token}` },
}),
cache: new client_1.InMemoryCache(),
});
const { data } = yield client.query({
query: (0, client_1.gql) `
query {
me {
repositories {
nodes {
id
name
}
}
}
}
`,
});
return data.me.repositories.nodes;
});
exports.listAllRepositories = listAllRepositories;
const deployBranch = (repositoryName, branchName) => __awaiter(void 0, void 0, void 0, function* () {
const token = yield (0, __1.checkAuth)();
const client = new client_1.ApolloClient({
link: new client_1.HttpLink({
uri: `${API_BASE_URL}/graphql`,
fetch,
headers: { Authorization: `Bearer ${token}` },
}),
cache: new client_1.InMemoryCache(),
});
const { data } = yield client.mutate({
mutation: (0, client_1.gql) `
mutation DeployBranchViaCLI($repositoryName: String!, $branchName: String!) {
deployBranchViaCLI(repositoryName: $repositoryName, branchName: $branchName) {
id
}
}
`,
variables: {
repositoryName,
branchName,
},
});
return data;
});
exports.deployBranch = deployBranch;
const uploadUserCode = (filePath, repositoryName, branchName) => __awaiter(void 0, void 0, void 0, function* () {
const data = new form_data_1.default();
const operations = JSON.stringify({
query: `
mutation UploadUserCode($file: Upload!, $repositoryName: String, $branchName: String) {
uploadUserCode(file: $file, repositoryName: $repositoryName, branchName: $branchName) {
deploymentId
}
}
`,
variables: {
file: null,
repositoryName: repositoryName || null,
branchName: branchName || null,
},
});
const map = {
'0': ['variables.file'],
};
data.append('operations', operations);
data.append('map', JSON.stringify(map));
data.append('0', (0, fs_1.createReadStream)(filePath));
if (repositoryName)
data.append('1', repositoryName);
if (branchName)
data.append('2', branchName);
const token = yield (0, __1.checkForToken)();
const config = {
method: 'post',
url: `${API_BASE_URL}/graphql`,
headers: Object.assign(Object.assign({ 'apollo-require-preflight': 'true' }, (token ? { authorization: `Bearer ${token}` } : {})), data.getHeaders()),
data: data,
};
try {
const response = yield axios_1.default.request(config);
const { data } = response;
const { uploadUserCode } = data.data;
return {
deploymentId: uploadUserCode.deploymentId,
filename: uploadUserCode.filename,
status: uploadUserCode.status,
};
}
catch (error) {
console.error('Upload failed:', error);
throw new Error('Upload failed');
}
});
exports.uploadUserCode = uploadUserCode;
const createCLIFrontendDeployment = (zipFileName) => __awaiter(void 0, void 0, void 0, function* () {
yield delay(5000);
const client = new client_1.ApolloClient({
link: new client_1.HttpLink({
uri: `${API_BASE_URL}/graphql`,
fetch,
}),
cache: new client_1.InMemoryCache(),
});
const { data } = yield client.mutate({
mutation: (0, client_1.gql) `
mutation CreateCLIFrontendDeployment($zipFileName: String!) {
createCLIFrontendDeployment(zipFileName: $zipFileName) {
id
url
timeToLive
}
}
`,
variables: {
zipFileName,
},
});
return data.createCLIFrontendDeployment;
});
exports.createCLIFrontendDeployment = createCLIFrontendDeployment;
function pullForDeploymentStatus(deploymentId) {
return __awaiter(this, void 0, void 0, function* () {
const pollInterval = 4000;
let isPolling = true;
// Handle SIGINT (Ctrl+C)
process_1.default.on('SIGINT', () => {
isPolling = false;
(0, prompts_1.outro)('Deployment cancelled');
process_1.default.exit(0);
});
// Handle process termination
process_1.default.on('SIGTERM', () => {
isPolling = false;
(0, prompts_1.outro)('Deployment cancelled');
process_1.default.exit(0);
});
try {
const token = yield (0, __1.checkAuth)();
const client = new client_1.ApolloClient({
link: new client_1.HttpLink({
uri: `${API_BASE_URL}/graphql`,
fetch,
headers: { Authorization: `Bearer ${token}` },
}),
cache: new client_1.InMemoryCache(),
});
while (isPolling) {
try {
yield delay(pollInterval);
const { data } = yield client.query({
query: (0, client_1.gql) `
query Deployment($deploymentId: ID!) {
deployment(id: $deploymentId) {
url
id
status
}
}
`,
variables: { deploymentId },
fetchPolicy: 'network-only',
});
if (data.deployment.url) {
return { url: data.deployment.url, status: data.deployment.status };
}
else if (data.deployment.status === 'FAILED') {
return { url: null, status: data.deployment.status };
}
}
catch (error) {
return null;
}
}
}
catch (error) {
return null;
}
finally {
process_1.default.removeAllListeners('SIGINT');
process_1.default.removeAllListeners('SIGTERM');
}
});
}
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));