jitsu-cli 2.14.0-beta.85 → 2.14.0-beta.93

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jitsu-cli",
3
- "version": "2.14.0-beta.85",
3
+ "version": "2.14.0-beta.93",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/jitsucom/jitsu",
@@ -102,12 +102,54 @@ async function deployFunctions({ host, apikey, name: names }, projectDir, packag
102
102
  }
103
103
  profileBuilders = (await res.json()).profileBuilders;
104
104
  }
105
+ const existingFunctions = await fetchExistingFunctions({ host, apikey, workspaceId: workspace.id });
105
106
  for (const file of selectedFiles) {
106
107
  console.log(`${b(`𝑓`)} Deploying function ${b(path.basename(file))} to workspace ${workspace.name} (${host}/${workspace.slug || workspace.id})`);
107
- await deployFunction(projectDir, { host, apikey }, packageJson, workspace, kind, path.resolve(functionsDir, file), profileBuilders);
108
+ await deployFunction(projectDir, { host, apikey }, packageJson, workspace, kind, path.resolve(functionsDir, file), profileBuilders, existingFunctions);
108
109
  }
109
110
  }
110
- async function deployFunction(projectDir, { host, apikey }, packageJson, workspace, kind, file, profileBuilders = []) {
111
+ async function fetchExistingFunctions({ host, apikey, workspaceId, }) {
112
+ const res = await fetch(`${host}/api/${workspaceId}/config/function`, {
113
+ headers: { Authorization: `Bearer ${apikey}` },
114
+ });
115
+ if (!res.ok) {
116
+ console.error(red(`Cannot list existing functions:\n${b(await res.text())}`));
117
+ process.exit(1);
118
+ }
119
+ const { objects } = (await res.json());
120
+ const bySlug = new Map();
121
+ const byId = new Map();
122
+ for (const f of objects) {
123
+ const entry = { id: f.id, slug: f.slug };
124
+ byId.set(f.id, entry);
125
+ if (f.slug)
126
+ bySlug.set(f.slug, entry);
127
+ }
128
+ return { bySlug, byId };
129
+ }
130
+ function cacheAfterCreate(cache, id, slug) {
131
+ const entry = { id, slug };
132
+ cache.byId.set(id, entry);
133
+ if (slug)
134
+ cache.bySlug.set(slug, entry);
135
+ }
136
+ function cacheAfterUpdate(cache, id, newSlug) {
137
+ const entry = cache.byId.get(id);
138
+ if (!entry) {
139
+ cacheAfterCreate(cache, id, newSlug);
140
+ return;
141
+ }
142
+ if (entry.slug && entry.slug !== newSlug) {
143
+ cache.bySlug.delete(entry.slug);
144
+ }
145
+ entry.slug = newSlug;
146
+ if (newSlug)
147
+ cache.bySlug.set(newSlug, entry);
148
+ }
149
+ async function deployFunction(projectDir, { host, apikey }, packageJson, workspace, kind, file, profileBuilders = [], existingFunctions = {
150
+ bySlug: new Map(),
151
+ byId: new Map(),
152
+ }) {
111
153
  const code = readFileSync(file, "utf-8");
112
154
  const wrapped = await getFunctionFromFilePath(projectDir, file, kind, profileBuilders);
113
155
  const meta = wrapped.meta;
@@ -120,19 +162,8 @@ async function deployFunction(projectDir, { host, apikey }, packageJson, workspa
120
162
  }
121
163
  let existingFunctionId;
122
164
  if (meta.slug) {
123
- const res = await fetch(`${host}/api/${workspace.id}/config/function`, {
124
- headers: {
125
- Authorization: `Bearer ${apikey}`,
126
- },
127
- });
128
- if (!res.ok) {
129
- console.error(red(`Cannot add function to workspace:\n${b(await res.text())}`));
130
- process.exit(1);
131
- }
132
- else {
133
- const existing = (await res.json());
134
- existingFunctionId = existing.objects.find(f => f.slug === meta.slug || f.id === meta.id)?.id;
135
- }
165
+ existingFunctionId =
166
+ existingFunctions.bySlug.get(meta.slug)?.id ?? (meta.id ? existingFunctions.byId.get(meta.id)?.id : undefined);
136
167
  }
137
168
  let functionPayload = {};
138
169
  if (kind === "profile") {
@@ -171,6 +202,7 @@ async function deployFunction(projectDir, { host, apikey }, packageJson, workspa
171
202
  process.exit(1);
172
203
  }
173
204
  else {
205
+ cacheAfterCreate(existingFunctions, id, meta.slug);
174
206
  console.log(`Function ${b(meta.name)} was successfully added to workspace ${workspace.name} with id: ${b(id)}`);
175
207
  }
176
208
  }
@@ -198,6 +230,7 @@ async function deployFunction(projectDir, { host, apikey }, packageJson, workspa
198
230
  process.exit(1);
199
231
  }
200
232
  else {
233
+ cacheAfterUpdate(existingFunctions, id, meta.slug);
201
234
  console.log(`${green(`✓`)} ${b(meta.name)} deployed successfully!`);
202
235
  }
203
236
  }
package/dist/main.js CHANGED
@@ -43189,6 +43189,7 @@ ${b(await res.text())}`));
43189
43189
  }
43190
43190
  profileBuilders2 = (await res.json()).profileBuilders;
43191
43191
  }
43192
+ const existingFunctions = await fetchExistingFunctions({ host, apikey, workspaceId: workspace.id });
43192
43193
  for (const file of selectedFiles) {
43193
43194
  console.log(
43194
43195
  `${b(`\u{1D453}`)} Deploying function ${b(import_path2.default.basename(file))} to workspace ${workspace.name} (${host}/${workspace.slug || workspace.id})`
@@ -43200,11 +43201,55 @@ ${b(await res.text())}`));
43200
43201
  workspace,
43201
43202
  kind2,
43202
43203
  import_path2.default.resolve(functionsDir, file),
43203
- profileBuilders2
43204
+ profileBuilders2,
43205
+ existingFunctions
43204
43206
  );
43205
43207
  }
43206
43208
  }
43207
- async function deployFunction(projectDir2, { host, apikey }, packageJson, workspace, kind2, file, profileBuilders2 = []) {
43209
+ async function fetchExistingFunctions({
43210
+ host,
43211
+ apikey,
43212
+ workspaceId
43213
+ }) {
43214
+ const res = await fetch(`${host}/api/${workspaceId}/config/function`, {
43215
+ headers: { Authorization: `Bearer ${apikey}` }
43216
+ });
43217
+ if (!res.ok) {
43218
+ console.error(red(`Cannot list existing functions:
43219
+ ${b(await res.text())}`));
43220
+ process.exit(1);
43221
+ }
43222
+ const { objects } = await res.json();
43223
+ const bySlug = /* @__PURE__ */ new Map();
43224
+ const byId = /* @__PURE__ */ new Map();
43225
+ for (const f of objects) {
43226
+ const entry = { id: f.id, slug: f.slug };
43227
+ byId.set(f.id, entry);
43228
+ if (f.slug) bySlug.set(f.slug, entry);
43229
+ }
43230
+ return { bySlug, byId };
43231
+ }
43232
+ function cacheAfterCreate(cache, id2, slug) {
43233
+ const entry = { id: id2, slug };
43234
+ cache.byId.set(id2, entry);
43235
+ if (slug) cache.bySlug.set(slug, entry);
43236
+ }
43237
+ function cacheAfterUpdate(cache, id2, newSlug) {
43238
+ const entry = cache.byId.get(id2);
43239
+ if (!entry) {
43240
+ cacheAfterCreate(cache, id2, newSlug);
43241
+ return;
43242
+ }
43243
+ if (entry.slug && entry.slug !== newSlug) {
43244
+ cache.bySlug.delete(entry.slug);
43245
+ }
43246
+ entry.slug = newSlug;
43247
+ if (newSlug) cache.bySlug.set(newSlug, entry);
43248
+ }
43249
+ async function deployFunction(projectDir2, { host, apikey }, packageJson, workspace, kind2, file, profileBuilders2 = [], existingFunctions = {
43250
+ bySlug: /* @__PURE__ */ new Map(),
43251
+ byId: /* @__PURE__ */ new Map()
43252
+ }) {
43208
43253
  const code2 = (0, import_fs5.readFileSync)(file, "utf-8");
43209
43254
  const wrapped = await getFunctionFromFilePath(projectDir2, file, kind2, profileBuilders2);
43210
43255
  const meta = wrapped.meta;
@@ -43216,19 +43261,7 @@ async function deployFunction(projectDir2, { host, apikey }, packageJson, worksp
43216
43261
  }
43217
43262
  let existingFunctionId;
43218
43263
  if (meta.slug) {
43219
- const res = await fetch(`${host}/api/${workspace.id}/config/function`, {
43220
- headers: {
43221
- Authorization: `Bearer ${apikey}`
43222
- }
43223
- });
43224
- if (!res.ok) {
43225
- console.error(red(`Cannot add function to workspace:
43226
- ${b(await res.text())}`));
43227
- process.exit(1);
43228
- } else {
43229
- const existing = await res.json();
43230
- existingFunctionId = existing.objects.find((f) => f.slug === meta.slug || f.id === meta.id)?.id;
43231
- }
43264
+ existingFunctionId = existingFunctions.bySlug.get(meta.slug)?.id ?? (meta.id ? existingFunctions.byId.get(meta.id)?.id : void 0);
43232
43265
  }
43233
43266
  let functionPayload = {};
43234
43267
  if (kind2 === "profile") {
@@ -43267,6 +43300,7 @@ ${b(await res.text())}`));
43267
43300
  ${b(await res.text())}`));
43268
43301
  process.exit(1);
43269
43302
  } else {
43303
+ cacheAfterCreate(existingFunctions, id2, meta.slug);
43270
43304
  console.log(`Function ${b(meta.name)} was successfully added to workspace ${workspace.name} with id: ${b(id2)}`);
43271
43305
  }
43272
43306
  } else {
@@ -43293,6 +43327,7 @@ ${b(await res.text())}`));
43293
43327
  ${b(await res.text())}`));
43294
43328
  process.exit(1);
43295
43329
  } else {
43330
+ cacheAfterUpdate(existingFunctions, id2, meta.slug);
43296
43331
  console.log(`${green(`\u2713`)} ${b(meta.name)} deployed successfully!`);
43297
43332
  }
43298
43333
  }
@@ -43305,7 +43340,7 @@ var fs4 = __toESM(require("fs"));
43305
43340
  // package.json
43306
43341
  var package_default = {
43307
43342
  name: "jitsu-cli",
43308
- version: "2.14.0-beta.85",
43343
+ version: "2.14.0-beta.93",
43309
43344
  repository: {
43310
43345
  type: "git",
43311
43346
  url: "https://github.com/jitsucom/jitsu",