@embeddable.com/sdk-core 0.2.0 → 1.0.0

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/scripts/push.js CHANGED
@@ -1,97 +1,153 @@
1
- const fs= require("fs/promises");
2
- const fsSync= require("fs");
1
+ const fs = require("fs/promises");
2
+ const fsSync = require("fs");
3
3
  const path = require("path");
4
- const archiver = require('archiver');
5
- const axios = require('axios');
6
- const oraP = import('ora');
4
+ const archiver = require("archiver");
5
+ const axios = require("axios");
6
+ const oraP = import("ora");
7
+ const inquirerSelect = import("@inquirer/select");
7
8
 
8
- const { findEmbFiles } = require('./findEmbFiles');
9
+ const { findEmbFiles } = require("./findEmbFiles");
9
10
  const { createContext } = require("./createContext");
10
- const { getToken } = require('./login');
11
+ const { getToken } = require("./login");
11
12
 
12
13
  const EMB_YAML_FILE_REGEX = /^(.*)\.emb\.ya?ml$/;
13
14
 
14
- async function push () {
15
- const ora = (await oraP).default;
15
+ let ora;
16
+ async function push() {
17
+ ora = (await oraP).default;
16
18
 
17
- const ctx = createContext(path.resolve(__dirname, '..'), process.cwd());
19
+ const ctx = createContext(path.resolve(__dirname, ".."), process.cwd());
18
20
 
19
- const token = await verify(ctx);
21
+ const token = await verify(ctx);
20
22
 
21
- const spinnerArchive = ora("archivation...").start();
23
+ const { workspaceId, workspaceName } = await selectWorkspace(token);
22
24
 
23
- const filesList = await findEmbFiles(ctx.client.srcDir, EMB_YAML_FILE_REGEX);
25
+ const spinnerArchive = ora("archivation...").start();
24
26
 
25
- await archive(ctx, filesList);
26
- spinnerArchive.succeed("archivation competed");
27
+ const filesList = await findEmbFiles(ctx.client.srcDir, EMB_YAML_FILE_REGEX);
27
28
 
28
- const spinnerPushing = ora("publishing...").start();
29
- await sendBuild(ctx, token);
30
- spinnerPushing.succeed("published!");
29
+ await archive(ctx, filesList);
30
+ spinnerArchive.succeed("archivation competed");
31
+
32
+ const spinnerPushing = ora("publishing...").start();
33
+
34
+ await sendBuild(ctx, { workspaceId, token });
35
+ spinnerPushing.succeed(`published to ${workspaceName}`);
31
36
  }
32
37
 
33
- module.exports = { push }
38
+ module.exports = { push };
39
+
40
+ async function selectWorkspace(token) {
41
+ const workspaceSpinner = ora({
42
+ text: "Fetching workspaces...",
43
+ color: "green",
44
+ discardStdin: false,
45
+ }).start();
46
+
47
+ const availableWorkspaces = await getWorkspaces(token);
48
+
49
+ let selectedWorkspace;
50
+
51
+ if (availableWorkspaces.length === 0) {
52
+ workspaceSpinner.fail("No workspaces found");
53
+ process.exit(1);
54
+ }
55
+
56
+ workspaceSpinner.info(`Found ${availableWorkspaces.length} workspace(s)`);
57
+
58
+ if (availableWorkspaces.length === 1) {
59
+ selectedWorkspace = availableWorkspaces[0];
60
+ } else {
61
+ const select = (await inquirerSelect).default;
62
+ selectedWorkspace = await select({
63
+ message: "Select workspace to push changes",
64
+ choices: availableWorkspaces.map((workspace) => ({
65
+ name: `${workspace.name} (${workspace.workspaceId})`,
66
+ value: workspace,
67
+ })),
68
+ });
69
+ }
70
+
71
+ workspaceSpinner.succeed(`Workspace: ${selectedWorkspace.name} (${selectedWorkspace.workspaceId})`);
72
+
73
+ return selectedWorkspace;
74
+ }
34
75
 
35
76
  async function verify(ctx) {
36
- try {
37
- await fs.access(ctx.client.buildDir);
38
- } catch (_e) {
39
- console.error('No embeddable build was produced.');
40
- process.exit(1);
41
- }
42
-
43
- // TODO: initiate login if no/invalid token.
44
- const token = await getToken();
45
-
46
- if (!token) {
47
- console.error('Expired token. Please login again.');
48
- process.exit(1);
49
- }
50
-
51
- return token;
77
+ try {
78
+ await fs.access(ctx.client.buildDir);
79
+ } catch (_e) {
80
+ console.error("No embeddable build was produced.");
81
+ process.exit(1);
82
+ }
83
+
84
+ // TODO: initiate login if no/invalid token.
85
+ const token = await getToken();
86
+
87
+ if (!token) {
88
+ console.error("Expired token. Please login again.");
89
+ process.exit(1);
90
+ }
91
+
92
+ return token;
52
93
  }
53
94
 
54
95
  function archive(ctx, yamlFiles) {
55
- const output = fsSync.createWriteStream(ctx.client.archiveFile);
56
- const _archiver = archiver('zip', {
57
- zlib: { level: 9 }
58
- });
96
+ const output = fsSync.createWriteStream(ctx.client.archiveFile);
97
+ const _archiver = archiver("zip", {
98
+ zlib: { level: 9 },
99
+ });
100
+
101
+ _archiver.pipe(output);
102
+ _archiver.directory(ctx.client.buildDir, false);
59
103
 
60
- _archiver.pipe(output);
61
- _archiver.directory(ctx.client.buildDir, false);
104
+ for (const fileData of yamlFiles) {
105
+ _archiver.file(fileData[1], { name: `${fileData[0]}.emb.yaml` });
106
+ }
62
107
 
63
- for (const fileData of yamlFiles) {
64
- _archiver.file(fileData[1], { name: `${fileData[0]}.emb.yaml` })
65
- }
108
+ _archiver.finalize();
66
109
 
67
- _archiver.finalize();
110
+ return new Promise((resolve, _reject) => {
111
+ output.on("close", resolve);
112
+ });
113
+ }
68
114
 
69
- return new Promise((resolve, _reject) => {
70
- output.on('close', resolve);
71
- })
115
+ async function sendBuild(ctx, { workspaceId, token }) {
116
+ const { FormData } = await import("formdata-node");
117
+ const { fileFromPath } = await import("formdata-node/file-from-path");
118
+
119
+ const file = await fileFromPath(
120
+ ctx.client.archiveFile,
121
+ "embeddable-build.zip",
122
+ );
123
+
124
+ const form = new FormData();
125
+ form.set("file", file, "embeddable-build.zip");
126
+ form.set("workspaceId", workspaceId);
127
+
128
+ await axios.post("https://metadata.embeddable.com/sdk/upload-files", form, {
129
+ headers: {
130
+ "Content-Type": "multipart/form-data",
131
+ Authorization: `Bearer ${token}`,
132
+ },
133
+ maxContentLength: Infinity,
134
+ maxBodyLength: Infinity,
135
+ });
136
+
137
+ await fs.rm(ctx.client.archiveFile);
72
138
  }
73
139
 
74
- async function sendBuild(ctx, token) {
75
- const {FormData} = await import("formdata-node");
76
- const {fileFromPath} = await import("formdata-node/file-from-path");
77
-
78
- const file = await fileFromPath(ctx.client.archiveFile, "embeddable-build.zip");
79
-
80
- const form = new FormData();
81
- form.set('file', file, 'embeddable-build.zip');
82
-
83
- await axios.post(
84
- 'https://metadata.embeddable.com/sdk/upload-files',
85
- form,
86
- {
87
- headers: {
88
- "Content-Type": "multipart/form-data",
89
- "Authorization" : `Bearer ${token}`
90
- },
91
- maxContentLength: Infinity,
92
- maxBodyLength: Infinity
93
- }
94
- );
95
-
96
- await fs.rm(ctx.client.archiveFile);
97
- }
140
+ async function getWorkspaces(token) {
141
+ try {
142
+ return axios
143
+ .get("https://api.embeddable.com/workspace", {
144
+ headers: {
145
+ Authorization: `Bearer ${token}`,
146
+ },
147
+ })
148
+ .then((res) => res.data);
149
+ } catch (e) {
150
+ console.error(e);
151
+ return [];
152
+ }
153
+ }
@@ -0,0 +1,66 @@
1
+ const fs = require("node:fs/promises");
2
+ const oraP = import("ora");
3
+ const YAML = require("yaml");
4
+ const { findEmbFiles } = require("./findEmbFiles");
5
+
6
+ const EMB_YAML_FILE_REGEX = /^(.*)\.emb\.ya?ml$/;
7
+
8
+ async function validate(ctx) {
9
+ const ora = (await oraP).default;
10
+
11
+ const spinnerValidate = ora("validation...").start();
12
+
13
+ const filesList = await findEmbFiles(ctx.client.srcDir, EMB_YAML_FILE_REGEX);
14
+
15
+ const componentConfigErrors = await componentConfigValidation(filesList);
16
+
17
+ if (componentConfigErrors.length) {
18
+ spinnerValidate.fail("One or more component.emb.yaml files are invalid:");
19
+
20
+ componentConfigErrors.forEach((errorMessage) =>
21
+ spinnerValidate.info(errorMessage),
22
+ );
23
+
24
+ process.exit(1);
25
+ }
26
+
27
+ spinnerValidate.succeed("validation completed");
28
+ }
29
+
30
+ module.exports = { validate };
31
+
32
+ async function componentConfigValidation(filesList) {
33
+ const ARRAY_ALLOWED_TYPES = ["Dimension", "Measure"];
34
+ const errors = [];
35
+
36
+ for (const [fileName, filePath] of filesList) {
37
+ if (!fileName.endsWith(".component")) continue;
38
+
39
+ const fileContentRaw = await fs.readFile(filePath, "utf8");
40
+
41
+ const config = YAML.parse(fileContentRaw);
42
+
43
+ if (!("inputs" in config) || config.inputs.length === 0) continue;
44
+
45
+ config.inputs
46
+ .filter((inputConfig) => inputConfig?.array)
47
+ .forEach((inputConfig) => {
48
+ if (!ARRAY_ALLOWED_TYPES.includes(inputConfig?.type)) {
49
+ const errorMessage = `${fileName} contains invalid type value for \x1b[33m${
50
+ inputConfig.name
51
+ }\x1b[0m input.
52
+ \x1b[33m${
53
+ inputConfig.name
54
+ }\x1b[0m is marked as \x1b[33marray\x1b[0m. Inputs marked as array support the following types: ${ARRAY_ALLOWED_TYPES.join(
55
+ ", ",
56
+ )}.
57
+ Specified \x1b[33m${inputConfig?.type}\x1b[0m type is not supported.
58
+ Please check the following file: \x1b[33m${filePath}\x1b[0m\n`;
59
+
60
+ errors.push(errorMessage);
61
+ }
62
+ });
63
+ }
64
+
65
+ return errors;
66
+ }