trainfabric 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +110 -31
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -3462,6 +3462,7 @@ var import_node_http = __toESM(require("node:http"), 1);
|
|
|
3462
3462
|
var import_node_os2 = __toESM(require("node:os"), 1);
|
|
3463
3463
|
var import_node_path3 = __toESM(require("node:path"), 1);
|
|
3464
3464
|
var import_node_child_process = require("node:child_process");
|
|
3465
|
+
var import_promises2 = require("node:timers/promises");
|
|
3465
3466
|
|
|
3466
3467
|
// ../../node_modules/.pnpm/commander@14.0.3/node_modules/commander/esm.mjs
|
|
3467
3468
|
var import_index = __toESM(require_commander(), 1);
|
|
@@ -8890,11 +8891,61 @@ function buildComputeSpec(options) {
|
|
|
8890
8891
|
|
|
8891
8892
|
// src/index.ts
|
|
8892
8893
|
var DEFAULT_TRAINFABRIC_API_URL2 = "https://api.trainfabric.com";
|
|
8893
|
-
var CLI_VERSION = "0.1.
|
|
8894
|
+
var CLI_VERSION = "0.1.3";
|
|
8894
8895
|
var CONFIG_DIR = import_node_path3.default.join(import_node_os2.default.homedir(), ".trainfabric");
|
|
8895
8896
|
var CONFIG_PATH = import_node_path3.default.join(CONFIG_DIR, "config.json");
|
|
8896
8897
|
var FALLBACK_SECRET_PATH = import_node_path3.default.join(CONFIG_DIR, "session.enc");
|
|
8897
8898
|
var KEYCHAIN_SERVICE = "trainfabric-cli";
|
|
8899
|
+
function printLoginBanner() {
|
|
8900
|
+
const logo = [
|
|
8901
|
+
" _______ __ _ _ _ ____ _____",
|
|
8902
|
+
" |__ __| [ | (_) | | (_) / __ \\ |_ _|",
|
|
8903
|
+
" | |_ __ _ _ _ __ | | ____ _ _ __ _ __ | |_ _ __ _ _ __ | | | | | | ",
|
|
8904
|
+
" | | '__| | | | '_ \\ | |/ / _` | '_ \\| '_ \\ | __| |/ _` | '_ \\ | | | | | | ",
|
|
8905
|
+
" | | | | |_| | | | || < (_| | |_) | |_) | | |_| | (_| | | | | | |__| |_ | | ",
|
|
8906
|
+
" |_|_| \\__,_|_| |_| |_|\\_\\| .__/| .__/ \\__|_|\\__,_|_| |_| \\____/|_____|",
|
|
8907
|
+
" | | | | "
|
|
8908
|
+
];
|
|
8909
|
+
console.log("");
|
|
8910
|
+
for (const row of logo) {
|
|
8911
|
+
console.log(row);
|
|
8912
|
+
}
|
|
8913
|
+
console.log(" TrainFabric CLI Login");
|
|
8914
|
+
console.log("");
|
|
8915
|
+
}
|
|
8916
|
+
function sleep(ms) {
|
|
8917
|
+
return (0, import_promises2.setTimeout)(ms);
|
|
8918
|
+
}
|
|
8919
|
+
async function runSpinnerUntil(label, action) {
|
|
8920
|
+
const frames = ["/", "-", "\\", "|"];
|
|
8921
|
+
let i = 0;
|
|
8922
|
+
const timer = setInterval(() => {
|
|
8923
|
+
process.stdout.write(`\r${frames[i++ % frames.length]} ${label}`);
|
|
8924
|
+
}, 90);
|
|
8925
|
+
try {
|
|
8926
|
+
const result = await action();
|
|
8927
|
+
clearInterval(timer);
|
|
8928
|
+
process.stdout.write(`\r[ok] ${label}... done${" ".repeat(16)}
|
|
8929
|
+
`);
|
|
8930
|
+
return result;
|
|
8931
|
+
} catch (error) {
|
|
8932
|
+
clearInterval(timer);
|
|
8933
|
+
process.stdout.write(`\r[x] ${label} failed${" ".repeat(16)}
|
|
8934
|
+
`);
|
|
8935
|
+
throw error;
|
|
8936
|
+
}
|
|
8937
|
+
}
|
|
8938
|
+
function printLoginFallbackHint(baseUrl) {
|
|
8939
|
+
console.error("\nCould not complete browser login (Unauthorized or callback failure).");
|
|
8940
|
+
console.error("If this is due to a blocked browser flow, use an API key instead:");
|
|
8941
|
+
console.error("");
|
|
8942
|
+
console.error(" export TRAINFABRIC_API_KEY=<api-key>");
|
|
8943
|
+
console.error(` trainfabric config:set-api-key <api-key>`);
|
|
8944
|
+
console.error(` trainfabric --version`);
|
|
8945
|
+
console.error("");
|
|
8946
|
+
console.error(`To verify dashboard link and API route, run: trainfabric config:set-base-url ${baseUrl}`);
|
|
8947
|
+
console.error("then run trainfabric login again.");
|
|
8948
|
+
}
|
|
8898
8949
|
function ensureConfigDir() {
|
|
8899
8950
|
import_node_fs2.default.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
8900
8951
|
import_node_fs2.default.chmodSync(CONFIG_DIR, 448);
|
|
@@ -9165,8 +9216,15 @@ function createClient(config) {
|
|
|
9165
9216
|
});
|
|
9166
9217
|
}
|
|
9167
9218
|
async function login(config) {
|
|
9168
|
-
|
|
9219
|
+
printLoginBanner();
|
|
9220
|
+
const loginClient = new TrainingClient({ baseUrl: config.baseUrl });
|
|
9169
9221
|
const server = import_node_http.default.createServer();
|
|
9222
|
+
const closeServer = () => {
|
|
9223
|
+
try {
|
|
9224
|
+
server.close();
|
|
9225
|
+
} catch {
|
|
9226
|
+
}
|
|
9227
|
+
};
|
|
9170
9228
|
const port = await new Promise((resolve) => {
|
|
9171
9229
|
server.listen(0, "127.0.0.1", () => {
|
|
9172
9230
|
const address = server.address();
|
|
@@ -9177,36 +9235,57 @@ async function login(config) {
|
|
|
9177
9235
|
});
|
|
9178
9236
|
});
|
|
9179
9237
|
const callbackUrl = `http://127.0.0.1:${port}/callback`;
|
|
9180
|
-
const loginUrl = await
|
|
9181
|
-
|
|
9182
|
-
|
|
9183
|
-
|
|
9184
|
-
|
|
9185
|
-
|
|
9186
|
-
|
|
9187
|
-
|
|
9188
|
-
|
|
9189
|
-
|
|
9190
|
-
|
|
9191
|
-
|
|
9192
|
-
|
|
9193
|
-
|
|
9194
|
-
|
|
9238
|
+
const loginUrl = await runSpinnerUntil("Preparing browser login", () => loginClient.auth.browserLoginUrl(callbackUrl));
|
|
9239
|
+
console.log("Waiting for authentication in your browser...");
|
|
9240
|
+
console.log(`Callback URL: ${callbackUrl}`);
|
|
9241
|
+
openBrowser(loginUrl.authorizationUrl);
|
|
9242
|
+
try {
|
|
9243
|
+
const tokenResult = await Promise.race([
|
|
9244
|
+
new Promise((resolve, reject) => {
|
|
9245
|
+
const timeoutId = setTimeout(() => {
|
|
9246
|
+
reject(new Error("Login callback timed out after 5 minutes. Please retry."));
|
|
9247
|
+
}, 5 * 60 * 1e3);
|
|
9248
|
+
server.on("request", (request, response) => {
|
|
9249
|
+
const url = new URL(request.url ?? "/", callbackUrl);
|
|
9250
|
+
const accessToken = url.searchParams.get("access_token");
|
|
9251
|
+
const refreshToken = url.searchParams.get("refresh_token") ?? void 0;
|
|
9252
|
+
const organizationId = url.searchParams.get("organization_id") ?? void 0;
|
|
9253
|
+
if (!accessToken) {
|
|
9254
|
+
response.statusCode = 400;
|
|
9255
|
+
response.end("Missing access token.");
|
|
9256
|
+
clearTimeout(timeoutId);
|
|
9257
|
+
reject(new Error("Login callback did not contain an access token."));
|
|
9258
|
+
return;
|
|
9259
|
+
}
|
|
9260
|
+
response.end("Authentication completed. You can close this window.");
|
|
9261
|
+
clearTimeout(timeoutId);
|
|
9262
|
+
resolve({ accessToken, refreshToken, organizationId });
|
|
9263
|
+
});
|
|
9264
|
+
}),
|
|
9265
|
+
(0, import_promises2.setTimeout)(5 * 60 * 1e3).then(() => {
|
|
9266
|
+
throw new Error("Login callback timed out after 5 minutes. Please retry.");
|
|
9267
|
+
})
|
|
9268
|
+
]);
|
|
9269
|
+
const authedClient = new TrainingClient({
|
|
9270
|
+
baseUrl: config.baseUrl,
|
|
9271
|
+
accessToken: tokenResult.accessToken
|
|
9195
9272
|
});
|
|
9196
|
-
|
|
9197
|
-
|
|
9198
|
-
|
|
9199
|
-
|
|
9200
|
-
|
|
9201
|
-
|
|
9202
|
-
|
|
9203
|
-
|
|
9204
|
-
|
|
9205
|
-
|
|
9206
|
-
|
|
9207
|
-
|
|
9208
|
-
|
|
9209
|
-
|
|
9273
|
+
const session = await runSpinnerUntil("Finalizing login", async () => {
|
|
9274
|
+
await sleep(100);
|
|
9275
|
+
return authedClient.auth.me();
|
|
9276
|
+
});
|
|
9277
|
+
config.accessToken = tokenResult.accessToken;
|
|
9278
|
+
config.refreshToken = tokenResult.refreshToken;
|
|
9279
|
+
config.orgId = tokenResult.organizationId ?? session.organizationId;
|
|
9280
|
+
config.apiKey = void 0;
|
|
9281
|
+
saveConfig(config);
|
|
9282
|
+
console.log(`Logged in as ${session.user.email} for organization ${config.orgId}.`);
|
|
9283
|
+
} catch (error) {
|
|
9284
|
+
printLoginFallbackHint(config.baseUrl);
|
|
9285
|
+
throw error;
|
|
9286
|
+
} finally {
|
|
9287
|
+
closeServer();
|
|
9288
|
+
}
|
|
9210
9289
|
}
|
|
9211
9290
|
var program2 = new Command();
|
|
9212
9291
|
program2.name("trainfabric").description("Trainfabric CLI for launching and monitoring GPU training jobs").version(CLI_VERSION);
|