@superblocksteam/util 0.0.15 → 0.0.17
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/component-configs.d.ts +1 -1
- package/dist/component-configs.js +19 -10
- package/dist/generate-component-types.d.ts +1 -0
- package/dist/generate-component-types.js +47 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/input-types.d.ts +9 -0
- package/dist/input-types.js +26 -0
- package/dist/login.d.ts +7 -5
- package/dist/login.js +27 -9
- package/dist/resource-configs.d.ts +0 -1
- package/package.json +1 -1
- package/src/component-configs.ts +21 -10
- package/src/generate-component-types.ts +56 -0
- package/src/index.ts +1 -0
- package/src/input-types.ts +34 -0
- package/src/login.ts +37 -14
- package/src/resource-configs.ts +0 -1
|
@@ -7,6 +7,7 @@ const node_os_1 = tslib_1.__importDefault(require("node:os"));
|
|
|
7
7
|
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
8
8
|
const fs = tslib_1.__importStar(require("fs-extra"));
|
|
9
9
|
const constants_1 = require("./constants");
|
|
10
|
+
const generate_component_types_1 = require("./generate-component-types");
|
|
10
11
|
const validation_1 = require("./validation");
|
|
11
12
|
const compileTypeScript = (inputFilePath, outputDir) => {
|
|
12
13
|
return new Promise((resolve, reject) => {
|
|
@@ -66,14 +67,14 @@ async function getFolderPaths() {
|
|
|
66
67
|
throw new Error("Could not access component directory, check your permissions");
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
|
-
async function getComponentConfigs() {
|
|
70
|
+
async function getComponentConfigs(generateTypesFiles) {
|
|
70
71
|
const folderPaths = await getFolderPaths();
|
|
71
72
|
let hasError = false;
|
|
72
73
|
const promiseConfigFiles = folderPaths.map(async (ccpath) => {
|
|
73
74
|
try {
|
|
74
|
-
const
|
|
75
|
+
const config = await fs.readJSON(`${ccpath}/config.json`);
|
|
75
76
|
console.log(`Found component in ${ccpath}`);
|
|
76
|
-
return
|
|
77
|
+
return { ccpath, config };
|
|
77
78
|
}
|
|
78
79
|
catch (e) {
|
|
79
80
|
// Noop because there is a fallback
|
|
@@ -91,15 +92,15 @@ async function getComponentConfigs() {
|
|
|
91
92
|
.slice(2)}`);
|
|
92
93
|
await compileTypeScript(fileName, outputDirectory);
|
|
93
94
|
console.log(`Typescript compiled to ${ccpath}`);
|
|
94
|
-
const
|
|
95
|
-
const isValid = (0, validation_1.validateCustomComponents)(
|
|
95
|
+
const config = (await Promise.resolve(`${outputDirectory + "/config.js"}`).then(s => tslib_1.__importStar(require(s)))).default;
|
|
96
|
+
const isValid = (0, validation_1.validateCustomComponents)(config);
|
|
96
97
|
if (!isValid.valid) {
|
|
97
98
|
// Not throwing because we don't need a stack trace here
|
|
98
99
|
console.log(`Invalid config.ts file found`);
|
|
99
100
|
console.log(isValid.message);
|
|
100
101
|
hasError = true;
|
|
101
102
|
}
|
|
102
|
-
return
|
|
103
|
+
return { ccpath, config };
|
|
103
104
|
}
|
|
104
105
|
catch (e) {
|
|
105
106
|
console.error(e);
|
|
@@ -107,14 +108,22 @@ async function getComponentConfigs() {
|
|
|
107
108
|
throw e;
|
|
108
109
|
}
|
|
109
110
|
});
|
|
110
|
-
const configFiles =
|
|
111
|
-
|
|
112
|
-
|
|
111
|
+
const configFiles = await Promise.all(promiseConfigFiles);
|
|
112
|
+
if (generateTypesFiles) {
|
|
113
|
+
for (const { config, ccpath } of configFiles) {
|
|
114
|
+
const typesFileContents = (0, generate_component_types_1.generateComponentTypesFile)(config);
|
|
115
|
+
await fs.writeFile(`${ccpath}/types.ts`, typesFileContents);
|
|
116
|
+
console.log(`Generated ${ccpath}/types.ts`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const configs = configFiles
|
|
120
|
+
.filter((component) => component.config !== null)
|
|
121
|
+
.reduce((acc, { config }) => {
|
|
113
122
|
acc[config.id] = config;
|
|
114
123
|
return acc;
|
|
115
124
|
}, {});
|
|
116
125
|
return {
|
|
117
|
-
configs
|
|
126
|
+
configs,
|
|
118
127
|
hasError,
|
|
119
128
|
};
|
|
120
129
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function generateComponentTypesFile(config: any): string;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateComponentTypesFile = void 0;
|
|
4
|
+
const input_types_1 = require("./input-types");
|
|
5
|
+
const indent = (str, spaces) => {
|
|
6
|
+
const spacesString = " ".repeat(spaces);
|
|
7
|
+
return str
|
|
8
|
+
.split("\n")
|
|
9
|
+
.map((line) => `${spacesString}${line}`)
|
|
10
|
+
.join("\n");
|
|
11
|
+
};
|
|
12
|
+
function renderInputTypeAsTS(name, type, trailingChars, indentSpaces) {
|
|
13
|
+
return indent(`${name}: ${input_types_1.inputTypeDefinions[type].tsType}${trailingChars}`, indentSpaces);
|
|
14
|
+
}
|
|
15
|
+
function generateComponentTypesFile(config) {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
const statefulProperties = (_a = config.statefulProperties) !== null && _a !== void 0 ? _a : [];
|
|
18
|
+
const eventHandlers = (_b = config.eventHandlers) !== null && _b !== void 0 ? _b : [];
|
|
19
|
+
return `// WARNING: This file is automatically generated and managed by Superblocks CLI and should not be edited manually.
|
|
20
|
+
// Use the \`superblocks components watch\` command to regenerate this file from its source in config.ts
|
|
21
|
+
|
|
22
|
+
// All stateful properties of your component are defined here.
|
|
23
|
+
// These are the properties which are surfaced in the Superblocks properties panel
|
|
24
|
+
// and can be referenced throughout your Superblocks Application
|
|
25
|
+
export interface StatefulProperties {
|
|
26
|
+
${statefulProperties
|
|
27
|
+
.map((v) => renderInputTypeAsTS(v.path, v.inputType, ";", 2) + "\n")
|
|
28
|
+
.join("")}}
|
|
29
|
+
|
|
30
|
+
export interface Props extends StatefulProperties {
|
|
31
|
+
/**
|
|
32
|
+
* Update one (or more) of your component's stateful properties. This will cause the component to rerender,
|
|
33
|
+
* and will also notify any other components in your Application which depend on the updated properties, so they also rerender
|
|
34
|
+
* @param props An object with the properties to update along with their new values
|
|
35
|
+
*/
|
|
36
|
+
updateStatefulProperties: (props: Partial<StatefulProperties>) => void;
|
|
37
|
+
|
|
38
|
+
// Call the subsequent function(s) to trigger event(s) in Superblocks from your component.
|
|
39
|
+
// These events can be wired up to event handlers in your Superblocks App
|
|
40
|
+
${eventHandlers.length === 0
|
|
41
|
+
? " // Note: no event handlers defined\n"
|
|
42
|
+
: eventHandlers
|
|
43
|
+
.map((v) => indent(v.path, 2) + ": () => void;" + "\n")
|
|
44
|
+
.join("")}}
|
|
45
|
+
`;
|
|
46
|
+
}
|
|
47
|
+
exports.generateComponentTypesFile = generateComponentTypesFile;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -5,3 +5,4 @@ tslib_1.__exportStar(require("./constants"), exports);
|
|
|
5
5
|
tslib_1.__exportStar(require("./component-configs"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./login"), exports);
|
|
7
7
|
tslib_1.__exportStar(require("./resource-configs"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./input-types"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const supportedInputTypes: readonly ["text", "number", "boolean", "js"];
|
|
2
|
+
export type InputType = (typeof supportedInputTypes)[number];
|
|
3
|
+
interface TypeInfo {
|
|
4
|
+
tsType: string;
|
|
5
|
+
prompt: string;
|
|
6
|
+
}
|
|
7
|
+
export declare const inputTypeDefinions: Record<InputType, TypeInfo>;
|
|
8
|
+
export declare function isValidInputType(input: string): input is InputType;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidInputType = exports.inputTypeDefinions = exports.supportedInputTypes = void 0;
|
|
4
|
+
exports.supportedInputTypes = ["text", "number", "boolean", "js"];
|
|
5
|
+
exports.inputTypeDefinions = {
|
|
6
|
+
text: {
|
|
7
|
+
tsType: "string",
|
|
8
|
+
prompt: "text",
|
|
9
|
+
},
|
|
10
|
+
number: {
|
|
11
|
+
tsType: "number",
|
|
12
|
+
prompt: "number",
|
|
13
|
+
},
|
|
14
|
+
boolean: {
|
|
15
|
+
tsType: "boolean",
|
|
16
|
+
prompt: "boolean",
|
|
17
|
+
},
|
|
18
|
+
js: {
|
|
19
|
+
tsType: "any",
|
|
20
|
+
prompt: "any (raw js expression)",
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
function isValidInputType(input) {
|
|
24
|
+
return Object.prototype.hasOwnProperty.call(exports.inputTypeDefinions, input);
|
|
25
|
+
}
|
|
26
|
+
exports.isValidInputType = isValidInputType;
|
package/dist/login.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
type
|
|
2
|
-
token: string;
|
|
1
|
+
type BaseUrl = {
|
|
3
2
|
superblocksBaseUrl: string;
|
|
4
3
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
type TokenWithBaseUrl = BaseUrl & {
|
|
5
|
+
token: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function saveApiToken(superblocksBaseUrl: string, token?: string): Promise<void>;
|
|
8
|
+
export declare function getLocalTokenWithUrlIfExists(): Promise<TokenWithBaseUrl | BaseUrl | undefined>;
|
|
9
|
+
export declare function getLocalTokenWithUrl(): Promise<TokenWithBaseUrl | BaseUrl>;
|
|
8
10
|
export {};
|
package/dist/login.js
CHANGED
|
@@ -6,13 +6,22 @@ const node_os_1 = require("node:os");
|
|
|
6
6
|
const node_path_1 = require("node:path");
|
|
7
7
|
const fs = tslib_1.__importStar(require("fs-extra"));
|
|
8
8
|
const constants_1 = require("./constants");
|
|
9
|
-
async function saveApiToken(
|
|
9
|
+
async function saveApiToken(superblocksBaseUrl, token) {
|
|
10
10
|
try {
|
|
11
11
|
await fs.ensureDir((0, node_path_1.join)((0, node_os_1.homedir)(), ".superblocks"));
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
if (token) {
|
|
13
|
+
const tokenConfig = {
|
|
14
|
+
superblocksBaseUrl,
|
|
15
|
+
token,
|
|
16
|
+
};
|
|
17
|
+
await fs.writeJSON((0, node_path_1.join)((0, node_os_1.homedir)(), constants_1.TOKEN_CONFIG_PATH), tokenConfig);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
const tokenConfig = {
|
|
21
|
+
superblocksBaseUrl,
|
|
22
|
+
};
|
|
23
|
+
await fs.writeJSON((0, node_path_1.join)((0, node_os_1.homedir)(), constants_1.TOKEN_CONFIG_PATH), tokenConfig);
|
|
24
|
+
}
|
|
16
25
|
}
|
|
17
26
|
catch {
|
|
18
27
|
throw new constants_1.FileAccessError("Could not save token");
|
|
@@ -31,10 +40,19 @@ exports.getLocalTokenWithUrlIfExists = getLocalTokenWithUrlIfExists;
|
|
|
31
40
|
async function getLocalTokenWithUrl() {
|
|
32
41
|
try {
|
|
33
42
|
const tokenConfig = await fs.readJSON((0, node_path_1.join)((0, node_os_1.homedir)(), constants_1.TOKEN_CONFIG_PATH));
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
43
|
+
if (tokenConfig.token) {
|
|
44
|
+
// user logged in
|
|
45
|
+
return {
|
|
46
|
+
token: tokenConfig.token,
|
|
47
|
+
superblocksBaseUrl: tokenConfig.superblocksBaseUrl,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
// user not logged in, but set domain
|
|
52
|
+
return {
|
|
53
|
+
superblocksBaseUrl: tokenConfig.superblocksBaseUrl,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
38
56
|
}
|
|
39
57
|
catch {
|
|
40
58
|
throw new Error("No local API key found");
|
package/package.json
CHANGED
package/src/component-configs.ts
CHANGED
|
@@ -3,6 +3,7 @@ import os from "node:os";
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import * as fs from "fs-extra";
|
|
5
5
|
import { CUSTOM_COMPONENTS_PATH } from "./constants";
|
|
6
|
+
import { generateComponentTypesFile } from "./generate-component-types";
|
|
6
7
|
import { validateCustomComponents } from "./validation";
|
|
7
8
|
|
|
8
9
|
const compileTypeScript = (inputFilePath: string, outputDir: string) => {
|
|
@@ -75,7 +76,9 @@ async function getFolderPaths() {
|
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
78
|
|
|
78
|
-
export async function getComponentConfigs(
|
|
79
|
+
export async function getComponentConfigs(
|
|
80
|
+
generateTypesFiles: boolean
|
|
81
|
+
): Promise<{
|
|
79
82
|
configs: Record<string, any>;
|
|
80
83
|
hasError: boolean;
|
|
81
84
|
}> {
|
|
@@ -83,9 +86,9 @@ export async function getComponentConfigs(): Promise<{
|
|
|
83
86
|
let hasError = false;
|
|
84
87
|
const promiseConfigFiles = folderPaths.map(async (ccpath) => {
|
|
85
88
|
try {
|
|
86
|
-
const
|
|
89
|
+
const config = await fs.readJSON(`${ccpath}/config.json`);
|
|
87
90
|
console.log(`Found component in ${ccpath}`);
|
|
88
|
-
return
|
|
91
|
+
return { ccpath, config };
|
|
89
92
|
} catch (e: any) {
|
|
90
93
|
// Noop because there is a fallback
|
|
91
94
|
}
|
|
@@ -105,30 +108,38 @@ export async function getComponentConfigs(): Promise<{
|
|
|
105
108
|
);
|
|
106
109
|
await compileTypeScript(fileName, outputDirectory);
|
|
107
110
|
console.log(`Typescript compiled to ${ccpath}`);
|
|
108
|
-
const
|
|
111
|
+
const config = (await import(outputDirectory + "/config.js")).default;
|
|
109
112
|
|
|
110
|
-
const isValid = validateCustomComponents(
|
|
113
|
+
const isValid = validateCustomComponents(config);
|
|
111
114
|
if (!isValid.valid) {
|
|
112
115
|
// Not throwing because we don't need a stack trace here
|
|
113
116
|
console.log(`Invalid config.ts file found`);
|
|
114
117
|
console.log(isValid.message);
|
|
115
118
|
hasError = true;
|
|
116
119
|
}
|
|
117
|
-
return
|
|
120
|
+
return { ccpath, config };
|
|
118
121
|
} catch (e: any) {
|
|
119
122
|
console.error(e);
|
|
120
123
|
console.error(e.message);
|
|
121
124
|
throw e;
|
|
122
125
|
}
|
|
123
126
|
});
|
|
124
|
-
const configFiles =
|
|
125
|
-
|
|
126
|
-
|
|
127
|
+
const configFiles = await Promise.all(promiseConfigFiles);
|
|
128
|
+
if (generateTypesFiles) {
|
|
129
|
+
for (const { config, ccpath } of configFiles) {
|
|
130
|
+
const typesFileContents = generateComponentTypesFile(config);
|
|
131
|
+
await fs.writeFile(`${ccpath}/types.ts`, typesFileContents);
|
|
132
|
+
console.log(`Generated ${ccpath}/types.ts`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const configs = configFiles
|
|
136
|
+
.filter((component) => component.config !== null)
|
|
137
|
+
.reduce((acc: Record<string, any>, { config }) => {
|
|
127
138
|
acc[config.id] = config;
|
|
128
139
|
return acc;
|
|
129
140
|
}, {});
|
|
130
141
|
return {
|
|
131
|
-
configs
|
|
142
|
+
configs,
|
|
132
143
|
hasError,
|
|
133
144
|
};
|
|
134
145
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { inputTypeDefinions, type InputType } from "./input-types";
|
|
2
|
+
|
|
3
|
+
const indent = (str: string, spaces: number) => {
|
|
4
|
+
const spacesString = " ".repeat(spaces);
|
|
5
|
+
|
|
6
|
+
return str
|
|
7
|
+
.split("\n")
|
|
8
|
+
.map((line) => `${spacesString}${line}`)
|
|
9
|
+
.join("\n");
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function renderInputTypeAsTS(
|
|
13
|
+
name: string,
|
|
14
|
+
type: InputType,
|
|
15
|
+
trailingChars: string,
|
|
16
|
+
indentSpaces: number
|
|
17
|
+
) {
|
|
18
|
+
return indent(
|
|
19
|
+
`${name}: ${inputTypeDefinions[type].tsType}${trailingChars}`,
|
|
20
|
+
indentSpaces
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function generateComponentTypesFile(config: any) {
|
|
25
|
+
const statefulProperties = config.statefulProperties ?? [];
|
|
26
|
+
const eventHandlers = config.eventHandlers ?? [];
|
|
27
|
+
return `// WARNING: This file is automatically generated and managed by Superblocks CLI and should not be edited manually.
|
|
28
|
+
// Use the \`superblocks components watch\` command to regenerate this file from its source in config.ts
|
|
29
|
+
|
|
30
|
+
// All stateful properties of your component are defined here.
|
|
31
|
+
// These are the properties which are surfaced in the Superblocks properties panel
|
|
32
|
+
// and can be referenced throughout your Superblocks Application
|
|
33
|
+
export interface StatefulProperties {
|
|
34
|
+
${statefulProperties
|
|
35
|
+
.map((v: any) => renderInputTypeAsTS(v.path, v.inputType, ";", 2) + "\n")
|
|
36
|
+
.join("")}}
|
|
37
|
+
|
|
38
|
+
export interface Props extends StatefulProperties {
|
|
39
|
+
/**
|
|
40
|
+
* Update one (or more) of your component's stateful properties. This will cause the component to rerender,
|
|
41
|
+
* and will also notify any other components in your Application which depend on the updated properties, so they also rerender
|
|
42
|
+
* @param props An object with the properties to update along with their new values
|
|
43
|
+
*/
|
|
44
|
+
updateStatefulProperties: (props: Partial<StatefulProperties>) => void;
|
|
45
|
+
|
|
46
|
+
// Call the subsequent function(s) to trigger event(s) in Superblocks from your component.
|
|
47
|
+
// These events can be wired up to event handlers in your Superblocks App
|
|
48
|
+
${
|
|
49
|
+
eventHandlers.length === 0
|
|
50
|
+
? " // Note: no event handlers defined\n"
|
|
51
|
+
: eventHandlers
|
|
52
|
+
.map((v: any) => indent(v.path, 2) + ": () => void;" + "\n")
|
|
53
|
+
.join("")
|
|
54
|
+
}}
|
|
55
|
+
`;
|
|
56
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const supportedInputTypes = ["text", "number", "boolean", "js"] as const;
|
|
2
|
+
|
|
3
|
+
export type InputType = (typeof supportedInputTypes)[number];
|
|
4
|
+
|
|
5
|
+
interface TypeInfo {
|
|
6
|
+
tsType: string;
|
|
7
|
+
prompt: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const inputTypeDefinions: Record<InputType, TypeInfo> = {
|
|
11
|
+
text: {
|
|
12
|
+
tsType: "string",
|
|
13
|
+
prompt: "text",
|
|
14
|
+
},
|
|
15
|
+
number: {
|
|
16
|
+
tsType: "number",
|
|
17
|
+
prompt: "number",
|
|
18
|
+
},
|
|
19
|
+
boolean: {
|
|
20
|
+
tsType: "boolean",
|
|
21
|
+
prompt: "boolean",
|
|
22
|
+
},
|
|
23
|
+
js: {
|
|
24
|
+
tsType: "any",
|
|
25
|
+
prompt: "any (raw js expression)",
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export function isValidInputType(input: string): input is InputType {
|
|
30
|
+
return Object.prototype.hasOwnProperty.call(
|
|
31
|
+
inputTypeDefinions,
|
|
32
|
+
input as InputType
|
|
33
|
+
);
|
|
34
|
+
}
|
package/src/login.ts
CHANGED
|
@@ -3,25 +3,36 @@ import { join } from "node:path";
|
|
|
3
3
|
import * as fs from "fs-extra";
|
|
4
4
|
import { FileAccessError, TOKEN_CONFIG_PATH } from "./constants";
|
|
5
5
|
|
|
6
|
-
type
|
|
7
|
-
token: string;
|
|
6
|
+
type BaseUrl = {
|
|
8
7
|
superblocksBaseUrl: string;
|
|
9
8
|
};
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
type TokenWithBaseUrl = BaseUrl & {
|
|
11
|
+
token: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export async function saveApiToken(superblocksBaseUrl: string, token?: string) {
|
|
12
15
|
try {
|
|
13
16
|
await fs.ensureDir(join(homedir(), ".superblocks"));
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
if (token) {
|
|
18
|
+
const tokenConfig: TokenWithBaseUrl = {
|
|
19
|
+
superblocksBaseUrl,
|
|
20
|
+
token,
|
|
21
|
+
};
|
|
22
|
+
await fs.writeJSON(join(homedir(), TOKEN_CONFIG_PATH), tokenConfig);
|
|
23
|
+
} else {
|
|
24
|
+
const tokenConfig: BaseUrl = {
|
|
25
|
+
superblocksBaseUrl,
|
|
26
|
+
};
|
|
27
|
+
await fs.writeJSON(join(homedir(), TOKEN_CONFIG_PATH), tokenConfig);
|
|
28
|
+
}
|
|
18
29
|
} catch {
|
|
19
30
|
throw new FileAccessError("Could not save token");
|
|
20
31
|
}
|
|
21
32
|
}
|
|
22
33
|
|
|
23
34
|
export async function getLocalTokenWithUrlIfExists(): Promise<
|
|
24
|
-
TokenWithBaseUrl | undefined
|
|
35
|
+
TokenWithBaseUrl | BaseUrl | undefined
|
|
25
36
|
> {
|
|
26
37
|
try {
|
|
27
38
|
return await getLocalTokenWithUrl();
|
|
@@ -30,13 +41,25 @@ export async function getLocalTokenWithUrlIfExists(): Promise<
|
|
|
30
41
|
}
|
|
31
42
|
}
|
|
32
43
|
|
|
33
|
-
export async function getLocalTokenWithUrl(): Promise<
|
|
44
|
+
export async function getLocalTokenWithUrl(): Promise<
|
|
45
|
+
TokenWithBaseUrl | BaseUrl
|
|
46
|
+
> {
|
|
34
47
|
try {
|
|
35
|
-
const tokenConfig = await fs.readJSON(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
48
|
+
const tokenConfig: TokenWithBaseUrl = await fs.readJSON(
|
|
49
|
+
join(homedir(), TOKEN_CONFIG_PATH)
|
|
50
|
+
);
|
|
51
|
+
if (tokenConfig.token) {
|
|
52
|
+
// user logged in
|
|
53
|
+
return {
|
|
54
|
+
token: tokenConfig.token,
|
|
55
|
+
superblocksBaseUrl: tokenConfig.superblocksBaseUrl,
|
|
56
|
+
};
|
|
57
|
+
} else {
|
|
58
|
+
// user not logged in, but set domain
|
|
59
|
+
return {
|
|
60
|
+
superblocksBaseUrl: tokenConfig.superblocksBaseUrl,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
40
63
|
} catch {
|
|
41
64
|
throw new Error("No local API key found");
|
|
42
65
|
}
|