-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress-utils.js
More file actions
84 lines (75 loc) · 2.32 KB
/
progress-utils.js
File metadata and controls
84 lines (75 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const path = require("path");
function clampProgressPercent(value) {
if (!Number.isFinite(value)) {
return 0;
}
if (value <= 0) {
return 0;
}
if (value >= 100) {
return 100;
}
return Math.floor(value);
}
function createProgressState({ mode, title, message, percent = null, commandLabel = "" }) {
return {
mode: mode === "spinner" ? "spinner" : "progress",
title: title || "",
message: message || "",
percent: mode === "spinner" ? null : clampProgressPercent(percent),
commandLabel: commandLabel || "",
};
}
function computePercent(index, total) {
if (!total || total <= 0) {
return 100;
}
return clampProgressPercent((index / total) * 100);
}
function buildUploadConfigProgressPlan(draft) {
const steps = [
{ key: "appId", value: draft.appId, args: ["config", "set-app-id", draft.appId] },
{ key: "appSecret", value: draft.appSecret, args: ["config", "set-app-secret", draft.appSecret] },
{ key: "folderToken", value: draft.folderToken, args: ["config", "set-folder", draft.folderToken] },
{ key: "userId", value: draft.userId, args: ["config", "set-user-id", draft.userId] },
].filter((step) => step.value);
return steps.map((step, index) => ({
...step,
percent: computePercent(index + 1, steps.length),
}));
}
function buildEnvironmentRefreshSteps() {
const keys = ["platform", "conda", "condaEnv", "python", "pip", "obsidianCli", "obshareCli", "save"];
return keys.map((key, index) => ({
key,
percent: computePercent(index + 1, keys.length),
}));
}
function buildInstallProgressSteps() {
const keys = ["prepare", "execute", "refresh", "save", "complete"];
return keys.map((key, index) => ({
key,
percent: computePercent(index + 1, keys.length),
}));
}
function shouldUseSpinnerForCliCommand(command) {
if (!Array.isArray(command) || command.length < 5) {
return false;
}
const executableName = path.basename(command[0]).toLowerCase();
return (
(executableName === "conda" || executableName === "conda.exe") &&
command[1] === "run" &&
command[2] === "-n" &&
command[3] === "obsd" &&
command[4] === "obshare-cli"
);
}
module.exports = {
clampProgressPercent,
createProgressState,
buildUploadConfigProgressPlan,
buildEnvironmentRefreshSteps,
buildInstallProgressSteps,
shouldUseSpinnerForCliCommand,
};