forked from windmill-labs/windmill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ts
More file actions
1049 lines (974 loc) · 28.1 KB
/
Copy pathscript.ts
File metadata and controls
1049 lines (974 loc) · 28.1 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// deno-lint-ignore-file no-explicit-any
import { GlobalOptions } from "./types.ts";
import { requireLogin, resolveWorkspace, validatePath } from "./context.ts";
import {
colors,
Command,
Confirm,
log,
readAll,
SEP,
Table,
writeAllSync,
yamlStringify,
} from "./deps.ts";
import { deepEqual } from "./utils.ts";
import * as wmill from "./gen/services.gen.ts";
import {
defaultScriptMetadata,
scriptBootstrapCode,
} from "./bootstrap/script_bootstrap.ts";
import { Workspace } from "./workspace.ts";
import {
generateScriptMetadataInternal,
parseMetadataFile,
} from "./metadata.ts";
import {
ScriptLanguage,
inferContentTypeFromFilePath,
} from "./script_common.ts";
import {
elementsToMap,
findCodebase,
readDirRecursiveWithIgnore,
Skips,
yamlOptions,
} from "./sync.ts";
import { ignoreF } from "./sync.ts";
import { FSFSElement } from "./sync.ts";
import {
SyncOptions,
mergeConfigWithConfigFile,
readConfigFile,
} from "./conf.ts";
import { SyncCodebase, listSyncCodebases } from "./codebase.ts";
import fs from "node:fs";
import { type Tarball } from "npm:@ayonli/jsext/archive";
import { execSync } from "node:child_process";
import { NewScript, Script } from "./gen/types.gen.ts";
export interface ScriptFile {
parent_hash?: string;
summary: string;
description: string;
schema?: any;
is_template?: boolean;
lock?: Array<string>;
kind?: "script" | "failure" | "trigger" | "command" | "approval";
}
type PushOptions = GlobalOptions;
async function push(opts: PushOptions, filePath: string) {
opts = await mergeConfigWithConfigFile(opts);
const workspace = await resolveWorkspace(opts);
if (!validatePath(filePath)) {
return;
}
const fstat = await Deno.stat(filePath);
if (!fstat.isFile) {
throw new Error("file path must refer to a file.");
}
if (filePath.endsWith(".script.json") || filePath.endsWith(".script.yaml")) {
throw Error(
"Cannot push a script metadata file, point to the script content file instead (.py, .ts, .go|.sh)"
);
}
await requireLogin(opts);
const codebases = await listSyncCodebases(opts as SyncOptions);
const globalDeps = await findGlobalDeps();
await handleFile(
filePath,
workspace,
[],
undefined,
opts,
globalDeps,
codebases
);
log.info(colors.bold.underline.green(`Script ${filePath} pushed`));
}
export async function findResourceFile(path: string) {
const splitPath = path.split(".");
const contentBasePathJSON = splitPath[0] + "." + splitPath[1] + ".json";
const contentBasePathYAML = splitPath[0] + "." + splitPath[1] + ".yaml";
const validCandidates = (
await Promise.all(
[contentBasePathJSON, contentBasePathYAML].map((x) => {
return Deno.stat(x)
.catch(() => undefined)
.then((x) => x?.isFile)
.then((e) => {
return { path: x, file: e };
});
})
)
)
.filter((x) => x.file)
.map((x) => x.path);
if (validCandidates.length > 1) {
throw new Error(
"Found two resource files for the same resource" +
validCandidates.join(", ")
);
}
if (validCandidates.length < 1) {
throw new Error(`No resource matching file resource: ${path}.`);
}
return validCandidates[0];
}
export async function handleScriptMetadata(
path: string,
workspace: Workspace,
alreadySynced: string[],
message: string | undefined,
globalDeps: GlobalDeps,
codebases: SyncCodebase[],
opts: GlobalOptions
): Promise<boolean> {
if (
path.endsWith(".script.json") ||
path.endsWith(".script.yaml") ||
path.endsWith(".script.lock")
) {
const contentPath = await findContentFile(path);
return handleFile(
contentPath,
workspace,
alreadySynced,
message,
opts,
globalDeps,
codebases
);
} else {
return false;
}
}
export async function handleFile(
path: string,
workspace: Workspace,
alreadySynced: string[],
message: string | undefined,
opts: (GlobalOptions & { defaultTs?: "bun" | "deno" } & Skips) | undefined,
globalDeps: GlobalDeps,
codebases: SyncCodebase[]
): Promise<boolean> {
if (
!path.includes(".inline_script.") &&
exts.some((exts) => path.endsWith(exts))
) {
if (alreadySynced.includes(path)) {
return true;
}
log.debug(`Processing local script ${path}`);
alreadySynced.push(path);
const remotePath = path
.substring(0, path.indexOf("."))
.replaceAll(SEP, "/");
const language = inferContentTypeFromFilePath(path, opts?.defaultTs);
const codebase =
language == "bun" ? findCodebase(path, codebases) : undefined;
let bundleContent: string | Tarball | undefined = undefined;
if (codebase) {
if (codebase.customBundler) {
log.info(`Using custom bundler ${codebase.customBundler} for ${path}`);
bundleContent = execSync(
codebase.customBundler + " " + path
).toString();
log.info("Custom bundler executed for " + path);
} else {
const esbuild = await import("npm:esbuild");
log.info(`Started bundling ${path} ...`);
const startTime = performance.now();
const out = await esbuild.build({
entryPoints: [path],
format: "cjs",
bundle: true,
write: false,
external: codebase.external,
inject: codebase.inject,
define: codebase.define,
platform: "node",
packages: "bundle",
target: "node20.15.1",
});
const endTime = performance.now();
bundleContent = out.outputFiles[0].text;
log.info(
`Finished bundling ${path}: ${(bundleContent.length / 1024).toFixed(
0
)}kB (${(endTime - startTime).toFixed(0)}ms)`
);
}
if (Array.isArray(codebase.assets) && codebase.assets.length > 0) {
const archiveNpm = await import("npm:@ayonli/jsext/archive");
log.info(
`Using the following asset configuration for ${path}: ${JSON.stringify(
codebase.assets
)}`
);
const startTime = performance.now();
const tarball = new archiveNpm.Tarball();
tarball.append(
new File([bundleContent], "main.js", { type: "text/plain" })
);
for (const asset of codebase.assets) {
const data = fs.readFileSync(asset.from);
const blob = new Blob([data], { type: "text/plain" });
const file = new File([blob], asset.to);
tarball.append(file);
}
const endTime = performance.now();
log.info(
`Finished creating tarball for ${path}: ${(
tarball.size / 1024
).toFixed(0)}kB (${(endTime - startTime).toFixed(0)}ms)`
);
bundleContent = tarball;
}
}
let typed = opts?.skipScriptsMetadata
? undefined
: (
await parseMetadataFile(
remotePath,
opts
? {
...opts,
path,
workspaceRemote: workspace,
schemaOnly: codebase ? true : undefined,
}
: undefined,
globalDeps,
codebases
)
)?.payload;
const workspaceId = workspace.workspaceId;
let remote = undefined;
try {
remote = await wmill.getScriptByPath({
workspace: workspaceId,
path: remotePath,
});
log.debug(`Script ${remotePath} exists on remote`);
} catch {
log.debug(`Script ${remotePath} does not exist on remote`);
}
const content = await Deno.readTextFile(path);
if (opts?.skipScriptsMetadata) {
// if (codebase) {
// const typedBefore = JSON.parse(JSON.stringify(typed.schema));
// await updateScriptSchema(content, language, typed, path);
// if (typedBefore != typed.schema) {
// log.info(`Updated metadata for bundle ${path}`);
// showDiff(
// yamlStringify(typedBefore, yamlOptions),
// yamlStringify(typed.schema, yamlOptions)
// );
// await Deno.writeTextFile(
// remotePath + ".script.yaml",
// yamlStringify(typed as Record<string, any>, yamlOptions)
// );
// }
// }
// else {
typed = structuredClone(remote);
// }
}
if (typed && codebase) {
typed.codebase = await codebase.getDigest();
}
const requestBodyCommon: NewScript = {
content,
description: typed?.description ?? "",
language: language as NewScript["language"],
path: remotePath.replaceAll(SEP, "/"),
summary: typed?.summary ?? "",
kind: typed?.kind,
lock: typed?.lock,
schema: typed?.schema,
tag: typed?.tag,
ws_error_handler_muted: typed?.ws_error_handler_muted,
dedicated_worker: typed?.dedicated_worker,
cache_ttl: typed?.cache_ttl,
concurrency_time_window_s: typed?.concurrency_time_window_s,
concurrent_limit: typed?.concurrent_limit,
deployment_message: message,
restart_unless_cancelled: typed?.restart_unless_cancelled,
visible_to_runner_only: typed?.visible_to_runner_only,
no_main_func: typed?.no_main_func,
has_preprocessor: typed?.has_preprocessor,
priority: typed?.priority,
concurrency_key: typed?.concurrency_key,
codebase: await codebase?.getDigest(),
timeout: typed?.timeout,
on_behalf_of_email: typed?.on_behalf_of_email,
};
// log.info(JSON.stringify(requestBodyCommon, null, 2))
// log.info(JSON.stringify(opts, null, 2))
if (remote) {
if (content === remote.content) {
if (
typed == undefined ||
(typed.description === remote.description &&
typed.summary === remote.summary &&
typed.kind == remote.kind &&
!remote.archived &&
(Array.isArray(remote?.lock)
? remote?.lock?.join("\n")
: remote?.lock ?? ""
).trim() == (typed?.lock ?? "").trim() &&
deepEqual(typed.schema, remote.schema) &&
typed.tag == remote.tag &&
(typed.ws_error_handler_muted ?? false) ==
remote.ws_error_handler_muted &&
typed.dedicated_worker == remote.dedicated_worker &&
typed.cache_ttl == remote.cache_ttl &&
typed.concurrency_time_window_s ==
remote.concurrency_time_window_s &&
typed.concurrent_limit == remote.concurrent_limit &&
Boolean(typed.restart_unless_cancelled) ==
Boolean(remote.restart_unless_cancelled) &&
Boolean(typed.visible_to_runner_only) ==
Boolean(remote.visible_to_runner_only) &&
Boolean(typed.no_main_func) == Boolean(remote.no_main_func) &&
Boolean(typed.has_preprocessor) ==
Boolean(remote.has_preprocessor) &&
typed.priority == Boolean(remote.priority) &&
typed.timeout == remote.timeout &&
//@ts-ignore
typed.concurrency_key == remote["concurrency_key"] &&
typed.codebase == remote.codebase &&
typed.on_behalf_of_email == remote.on_behalf_of_email)
) {
log.info(colors.green(`Script ${remotePath} is up to date`));
return true;
}
}
log.info(`Updating script ${remotePath} ...`);
const body = {
...requestBodyCommon,
parent_hash: remote.hash,
};
const execTime = await createScript(
bundleContent,
workspaceId,
body,
workspace
);
log.info(
colors.yellow.bold(
`Updated script ${remotePath} (${execTime.toFixed(0)}ms)`
)
);
} else {
log.info(`Creating new script ${remotePath} ...`);
const body = {
...requestBodyCommon,
parent_hash: undefined,
};
const execTime = await createScript(
bundleContent,
workspaceId,
body,
workspace
);
log.info(
colors.yellow.bold(
`Created new script ${remotePath} (${execTime.toFixed(0)}ms)`
)
);
}
return true;
}
return false;
}
async function streamToBlob(stream: ReadableStream<Uint8Array>): Promise<Blob> {
// Create a reader from the stream
const reader = stream.getReader();
const chunks = [];
// Read the data from the stream
while (true) {
const { done, value } = await reader.read();
if (done) {
// If stream is finished, break the loop
break;
}
// Push the chunk to the array
chunks.push(value);
}
// Create a Blob from the chunks
const blob = new Blob(chunks);
return blob;
}
async function createScript(
bundleContent: string | Tarball | undefined,
workspaceId: string,
body: NewScript,
workspace: Workspace
): Promise<number> {
const start = performance.now();
if (!bundleContent) {
try {
// no parent hash
await wmill.createScript({
workspace: workspaceId,
requestBody: body,
});
} catch (e: any) {
throw Error(
`Script creation for ${body.path} with parent ${
body.parent_hash
} was not successful: ${e.body ?? e.message} `
);
}
} else {
const form = new FormData();
form.append("script", JSON.stringify(body));
form.append(
"file",
typeof bundleContent == "string"
? bundleContent
: await streamToBlob(bundleContent.stream())
);
const url =
workspace.remote +
"api/w/" +
workspace.workspaceId +
"/scripts/create_snapshot";
const req = await fetch(url, {
method: "POST",
headers: { Authorization: `Bearer ${workspace.token} ` },
body: form,
});
if (req.status != 201) {
throw Error(
`Script snapshot creation was not successful: ${req.status} - ${
req.statusText
} - ${await req.text()} `
);
}
}
return performance.now() - start;
}
export async function findContentFile(filePath: string) {
const candidates = filePath.endsWith("script.json")
? exts.map((x) => filePath.replace(".script.json", x))
: filePath.endsWith("script.lock")
? exts.map((x) => filePath.replace(".script.lock", x))
: exts.map((x) => filePath.replace(".script.yaml", x));
const validCandidates = (
await Promise.all(
candidates.map((x) => {
return Deno.stat(x)
.catch(() => undefined)
.then((x) => x?.isFile)
.then((e) => {
return { path: x, file: e };
});
})
)
)
.filter((x) => x.file)
.map((x) => x.path);
if (validCandidates.length > 1) {
throw new Error(
"No content path given and more than one candidate found: " +
validCandidates.join(", ")
);
}
if (validCandidates.length < 1) {
throw new Error(
`No content path given and no content file found for ${filePath}.`
);
}
return validCandidates[0];
}
export function filePathExtensionFromContentType(
language: ScriptLanguage,
defaultTs: "bun" | "deno" | undefined
): string {
if (language === "python3") {
return ".py";
} else if (language === "nativets") {
return ".fetch.ts";
} else if (language === "bun") {
if (defaultTs == "deno") {
return ".bun.ts";
} else {
return ".ts";
}
} else if (language === "deno") {
if (defaultTs == undefined || defaultTs == "bun") {
return ".deno.ts";
} else {
return ".ts";
}
} else if (language === "go") {
return ".go";
} else if (language === "mysql") {
return ".my.sql";
} else if (language === "bigquery") {
return ".bq.sql";
} else if (language === "oracledb") {
return ".odb.sql";
} else if (language === "snowflake") {
return ".sf.sql";
} else if (language === "mssql") {
return ".ms.sql";
} else if (language === "postgresql") {
return ".pg.sql";
} else if (language === "graphql") {
return ".gql";
} else if (language === "bash") {
return ".sh";
} else if (language === "powershell") {
return ".ps1";
} else if (language === "php") {
return ".php";
} else if (language === "rust") {
return ".rs";
} else if (language === "ansible") {
return ".playbook.yml";
} else if (language === "csharp") {
return ".cs";
} else if (language === "nu") {
return ".nu";
} else if (language === "java") {
return ".java";
// for related places search: ADD_NEW_LANG
} else {
throw new Error("Invalid language: " + language);
}
}
export const exts = [
".fetch.ts",
".deno.ts",
".bun.ts",
".ts",
".py",
".go",
".sh",
".pg.sql",
".my.sql",
".bq.sql",
".odb.sql",
".sf.sql",
".ms.sql",
".sql",
".gql",
".ps1",
".php",
".rs",
".cs",
".nu",
".playbook.yml",
".java"
// for related places search: ADD_NEW_LANG
];
export function removeExtensionToPath(path: string): string {
for (const ext of exts) {
if (path.endsWith(ext)) {
return path.substring(0, path.length - ext.length);
}
}
throw new Error("Invalid extension: " + path);
}
async function list(
opts: GlobalOptions & {
showArchived?: boolean;
includeWithoutMain?: boolean;
includeDraftOnly?: boolean;
}
) {
const workspace = await resolveWorkspace(opts);
await requireLogin(opts);
let page = 0;
const perPage = 10;
const total: Script[] = [];
while (true) {
const res = await wmill.listScripts({
workspace: workspace.workspaceId,
page,
perPage,
showArchived: opts.showArchived ?? false,
includeWithoutMain: opts.includeWithoutMain ?? false,
includeDraftOnly: opts.includeDraftOnly ?? true,
});
page += 1;
total.push(...res);
if (res.length < perPage) {
break;
}
}
new Table()
.header(["path", "summary", "language", "created by"])
.padding(2)
.border(true)
.body(total.map((x) => [x.path, x.summary, x.language, x.created_by]))
.render();
}
export async function resolve(input: string): Promise<Record<string, any>> {
if (!input) {
throw new Error("No data given");
}
if (input == "@-") {
input = new TextDecoder().decode(await readAll(Deno.stdin));
}
if (input[0] == "@") {
input = await Deno.readTextFile(input.substring(1));
}
try {
return JSON.parse(input);
} catch (e) {
console.error("Impossible to parse input as JSON", input);
throw e;
}
}
async function run(
opts: GlobalOptions & {
data?: string;
silent: boolean;
},
path: string
) {
const workspace = await resolveWorkspace(opts);
await requireLogin(opts);
const input = opts.data ? await resolve(opts.data) : {};
const id = await wmill.runScriptByPath({
workspace: workspace.workspaceId,
path,
requestBody: input,
});
if (!opts.silent) {
await track_job(workspace.workspaceId, id);
}
while (true) {
try {
const result =
(
await wmill.getCompletedJob({
workspace: workspace.workspaceId,
id,
})
).result ?? {};
if (opts.silent) {
console.log(result);
} else {
log.info(result);
}
break;
} catch {
new Promise((resolve, _) => setTimeout(() => resolve(undefined), 100));
}
}
}
export async function track_job(workspace: string, id: string) {
try {
const result = await wmill.getCompletedJob({ workspace, id });
log.info(result.logs);
log.info("\n");
log.info(colors.bold.underline.green("Job Completed"));
log.info("\n");
return;
} catch {
/* ignore */
}
log.info(colors.yellow("Waiting for Job " + id + " to start..."));
let logOffset = 0;
let running = false;
let retry = 0;
while (true) {
let updates: {
running?: boolean | undefined;
completed?: boolean | undefined;
new_logs?: string | undefined;
};
try {
updates = await wmill.getJobUpdates({
workspace,
id,
logOffset,
running,
});
} catch {
retry++;
if (retry > 3) {
log.info("failed to get job updated. skipping log streaming.");
break;
}
continue;
}
if (!running && updates.running === true) {
running = true;
log.info(colors.green("Job running. Streaming logs..."));
}
if (updates.new_logs) {
writeAllSync(Deno.stdout, new TextEncoder().encode(updates.new_logs));
logOffset += updates.new_logs.length;
}
if (updates.completed === true) {
running = false;
break;
}
if (running && updates.running === false) {
running = false;
log.info(colors.yellow("Job suspended. Waiting for it to continue..."));
}
}
await new Promise((resolve, _) => setTimeout(() => resolve(undefined), 1000));
try {
const final_job = await wmill.getCompletedJob({ workspace, id });
if ((final_job.logs?.length ?? -1) > logOffset) {
log.info(final_job.logs!.substring(logOffset));
}
log.info("\n");
if (final_job.success) {
log.info(colors.bold.underline.green("Job Completed"));
} else {
log.info(colors.bold.underline.red("Job Completed"));
}
log.info("\n");
} catch {
log.info("Job appears to have completed, but no data can be retrieved");
}
}
async function show(opts: GlobalOptions, path: string) {
const workspace = await resolveWorkspace(opts);
await requireLogin(opts);
const s = await wmill.getScriptByPath({
workspace: workspace.workspaceId,
path,
});
log.info(colors.underline(s.path));
if (s.description) log.info(s.description);
log.info("");
log.info(s.content);
}
async function bootstrap(
opts: GlobalOptions & { summary: string; description: string },
scriptPath: string,
language: ScriptLanguage
) {
if (!validatePath(scriptPath)) {
return;
}
const scriptInitialCode = scriptBootstrapCode[language];
if (scriptInitialCode === undefined) {
throw new Error("Language unknown");
}
const config = await readConfigFile();
const extension = filePathExtensionFromContentType(
language,
config.defaultTs
);
const scriptCodeFileFullPath = scriptPath + extension;
const scriptMetadataFileFullPath = scriptPath + ".script.yaml";
try {
await Deno.stat(scriptCodeFileFullPath);
await Deno.stat(scriptMetadataFileFullPath);
throw new Error("File already exists in repository");
} catch {
// file does not exist, we can continue
}
const scriptMetadata = defaultScriptMetadata();
if (opts.summary !== undefined) {
scriptMetadata.summary = opts.summary;
}
if (opts.description !== undefined) {
scriptMetadata.description = opts.description;
}
const scriptInitialMetadataYaml = yamlStringify(
scriptMetadata as Record<string, any>,
yamlOptions
);
await Deno.writeTextFile(scriptCodeFileFullPath, scriptInitialCode, {
createNew: true,
});
await Deno.writeTextFile(
scriptMetadataFileFullPath,
scriptInitialMetadataYaml,
{
createNew: true,
}
);
}
export type GlobalDeps = {
pkgs: Record<string, string>;
reqs: Record<string, string>;
composers: Record<string, string>;
};
export async function findGlobalDeps(): Promise<GlobalDeps> {
const pkgs: { [key: string]: string } = {};
const reqs: { [key: string]: string } = {};
const composers: { [key: string]: string } = {};
const els = await FSFSElement(Deno.cwd(), [], false);
for await (const entry of readDirRecursiveWithIgnore((p, isDir) => {
p = SEP + p;
return (
!isDir &&
!(
p.endsWith(SEP + "package.json") ||
p.endsWith(SEP + "requirements.txt") ||
p.endsWith(SEP + "composer.json")
)
);
}, els)) {
if (entry.isDirectory || entry.ignored) continue;
const content = await entry.getContentText();
if (entry.path.endsWith("package.json")) {
pkgs[entry.path.substring(0, entry.path.length - 12)] = content;
} else if (entry.path.endsWith("requirements.txt")) {
reqs[entry.path.substring(0, entry.path.length - 16)] = content;
} else if (entry.path.endsWith("composer.json")) {
composers[entry.path.substring(0, entry.path.length - 13)] = content;
}
}
return { pkgs, reqs, composers };
}
async function generateMetadata(
opts: GlobalOptions & {
lockOnly?: boolean;
schemaOnly?: boolean;
yes?: boolean;
} & SyncOptions,
scriptPath: string | undefined
) {
log.info(
"This command only works for workspace scripts, for flows inline scripts use `wmill flow generate - locks`"
);
if (scriptPath == "") {
scriptPath = undefined;
}
if (scriptPath && !validatePath(scriptPath)) {
return;
}
const workspace = await resolveWorkspace(opts);
await requireLogin(opts);
opts = await mergeConfigWithConfigFile(opts);
const codebases = await listSyncCodebases(opts);
const globalDeps = await findGlobalDeps();
if (scriptPath) {
// read script metadata file
await generateScriptMetadataInternal(
scriptPath,
workspace,
opts,
false,
false,
globalDeps,
codebases,
false
);
} else {
const ignore = await ignoreF(opts);
const elems = await elementsToMap(
await FSFSElement(Deno.cwd(), codebases, false),
(p, isD) => {
return (
(!isD && !exts.some((ext) => p.endsWith(ext))) ||
ignore(p, isD) ||
p.includes(".flow" + SEP) ||
p.includes(".app" + SEP)
);
},
false,
{}
);
let hasAny = false;
log.info("Generating metadata for all stale scripts:");
for (const e of Object.keys(elems)) {
const candidate = await generateScriptMetadataInternal(
e,
workspace,
opts,
true,
true,
globalDeps,
codebases,
false
);
if (candidate) {
hasAny = true;
log.info(colors.green(`+ ${candidate} `));
}
}
if (hasAny) {
if (opts.dryRun) {
log.info(colors.gray(`Dry run complete.`));
return;
}
if (
!opts.yes &&
!(await Confirm.prompt({
message: "Update the metadata of the above scripts?",
default: true,
}))
) {
return;
}
} else {
log.info(colors.green.bold("No metadata to update"));
return;
}
for (const e of Object.keys(elems)) {
await generateScriptMetadataInternal(
e,
workspace,
opts,
false,
true,
globalDeps,
codebases,
false
);
}
}
}