windmill-utils-internal 1.0.0 → 1.0.1

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.
@@ -0,0 +1,11 @@
1
+ export declare const WINDMILL_CONFIG_DIR = "windmill";
2
+ export declare const WINDMILL_ACTIVE_WORKSPACE_FILE = "activeWorkspace";
3
+ export declare const WINDMILL_WORKSPACE_CONFIG_FILE = "remotes.ndjson";
4
+ export declare const INSTANCES_CONFIG_FILE = "instances.ndjson";
5
+ export declare const WINDMILL_ACTIVE_INSTANCE_FILE = "activeInstance";
6
+ export declare function getBaseConfigDir(configDirOverride?: string): Promise<string>;
7
+ export declare function getConfigDirPath(configDirOverride?: string): Promise<string>;
8
+ export declare function getWorkspaceConfigFilePath(configDirOverride?: string): Promise<string>;
9
+ export declare function getActiveWorkspaceConfigFilePath(configDirOverride?: string): Promise<string>;
10
+ export declare function getInstancesConfigFilePath(configDirOverride?: string): Promise<string>;
11
+ export declare function getActiveInstanceFilePath(configDirOverride?: string): Promise<string>;
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.WINDMILL_ACTIVE_INSTANCE_FILE = exports.INSTANCES_CONFIG_FILE = exports.WINDMILL_WORKSPACE_CONFIG_FILE = exports.WINDMILL_ACTIVE_WORKSPACE_FILE = exports.WINDMILL_CONFIG_DIR = void 0;
37
+ exports.getBaseConfigDir = getBaseConfigDir;
38
+ exports.getConfigDirPath = getConfigDirPath;
39
+ exports.getWorkspaceConfigFilePath = getWorkspaceConfigFilePath;
40
+ exports.getActiveWorkspaceConfigFilePath = getActiveWorkspaceConfigFilePath;
41
+ exports.getInstancesConfigFilePath = getInstancesConfigFilePath;
42
+ exports.getActiveInstanceFilePath = getActiveInstanceFilePath;
43
+ // Runtime detection
44
+ // @ts-ignore - Cross-platform runtime detection
45
+ const isDeno = typeof Deno !== "undefined";
46
+ // @ts-ignore - Cross-platform runtime detection
47
+ const isNode = typeof process !== "undefined" && process.versions?.node;
48
+ exports.WINDMILL_CONFIG_DIR = "windmill";
49
+ exports.WINDMILL_ACTIVE_WORKSPACE_FILE = "activeWorkspace";
50
+ exports.WINDMILL_WORKSPACE_CONFIG_FILE = "remotes.ndjson";
51
+ exports.INSTANCES_CONFIG_FILE = "instances.ndjson";
52
+ exports.WINDMILL_ACTIVE_INSTANCE_FILE = "activeInstance";
53
+ // Cross-platform environment variable access
54
+ function getEnv(key) {
55
+ if (isDeno) {
56
+ // @ts-ignore - Deno API
57
+ return Deno.env.get(key);
58
+ }
59
+ else {
60
+ // @ts-ignore - Node API
61
+ return process.env[key];
62
+ }
63
+ }
64
+ // Cross-platform OS detection with normalization
65
+ function getOS() {
66
+ if (isDeno) {
67
+ // @ts-ignore - Deno API
68
+ return Deno.build.os;
69
+ }
70
+ else if (isNode) {
71
+ // @ts-ignore - Node API
72
+ const platform = process.platform;
73
+ switch (platform) {
74
+ case "linux": return "linux";
75
+ case "darwin": return "darwin";
76
+ case "win32": return "windows"; // Normalize win32 to windows
77
+ default: return null;
78
+ }
79
+ }
80
+ return null;
81
+ }
82
+ // Cross-platform file system operations
83
+ async function stat(path) {
84
+ if (isDeno) {
85
+ // @ts-ignore - Deno API
86
+ return await Deno.stat(path);
87
+ }
88
+ else {
89
+ // @ts-ignore - Node API
90
+ const fs = await Promise.resolve().then(() => __importStar(require('fs/promises')));
91
+ return await fs.stat(path);
92
+ }
93
+ }
94
+ async function mkdir(path, options) {
95
+ if (isDeno) {
96
+ // @ts-ignore - Deno API
97
+ await Deno.mkdir(path, options);
98
+ }
99
+ else {
100
+ // @ts-ignore - Node API
101
+ const fs = await Promise.resolve().then(() => __importStar(require('fs/promises')));
102
+ await fs.mkdir(path, options);
103
+ }
104
+ }
105
+ function throwIfNotDirectory(fileInfo) {
106
+ if (!fileInfo.isDirectory) {
107
+ throw new Error("Path is not a directory");
108
+ }
109
+ }
110
+ function config_dir() {
111
+ const os = getOS();
112
+ switch (os) {
113
+ case "linux": {
114
+ const xdg = getEnv("XDG_CONFIG_HOME");
115
+ if (xdg)
116
+ return xdg;
117
+ const home = getEnv("HOME");
118
+ if (home)
119
+ return `${home}/.config`;
120
+ break;
121
+ }
122
+ case "darwin": {
123
+ const home = getEnv("HOME");
124
+ if (home)
125
+ return `${home}/Library/Preferences`;
126
+ break;
127
+ }
128
+ case "windows":
129
+ return getEnv("APPDATA") ?? null;
130
+ }
131
+ return null;
132
+ }
133
+ function tmp_dir() {
134
+ const os = getOS();
135
+ switch (os) {
136
+ case "linux": {
137
+ const xdg = getEnv("XDG_RUNTIME_DIR");
138
+ if (xdg)
139
+ return `${xdg}-/tmp`;
140
+ const tmpDir = getEnv("TMPDIR");
141
+ if (tmpDir)
142
+ return tmpDir;
143
+ const tempDir = getEnv("TEMP");
144
+ if (tempDir)
145
+ return tempDir;
146
+ const tmp = getEnv("TMP");
147
+ if (tmp)
148
+ return tmp;
149
+ return "/var/tmp";
150
+ }
151
+ case "darwin":
152
+ return getEnv("TMPDIR") ?? null;
153
+ case "windows":
154
+ return getEnv("TMP") ?? getEnv("TEMP") ?? null;
155
+ }
156
+ return null;
157
+ }
158
+ async function ensureDir(dir) {
159
+ try {
160
+ const fileInfo = await stat(dir);
161
+ throwIfNotDirectory(fileInfo);
162
+ return;
163
+ }
164
+ catch (err) {
165
+ // Check for file not found error in cross-platform way
166
+ if (isDeno) {
167
+ // @ts-ignore - Deno API
168
+ if (!(err instanceof Deno.errors.NotFound)) {
169
+ throw err;
170
+ }
171
+ }
172
+ else {
173
+ // Node.js error codes
174
+ if (err.code !== 'ENOENT') {
175
+ throw err;
176
+ }
177
+ }
178
+ }
179
+ // The dir doesn't exist. Create it.
180
+ // This can be racy. So we catch AlreadyExists and check stat again.
181
+ try {
182
+ await mkdir(dir, { recursive: true });
183
+ }
184
+ catch (err) {
185
+ // Check for already exists error in cross-platform way
186
+ if (isDeno) {
187
+ // @ts-ignore - Deno API
188
+ if (!(err instanceof Deno.errors.AlreadyExists)) {
189
+ throw err;
190
+ }
191
+ }
192
+ else {
193
+ // Node.js error codes
194
+ if (err.code !== 'EEXIST') {
195
+ throw err;
196
+ }
197
+ }
198
+ const fileInfo = await stat(dir);
199
+ throwIfNotDirectory(fileInfo);
200
+ }
201
+ }
202
+ async function getBaseConfigDir(configDirOverride) {
203
+ const baseDir = configDirOverride ??
204
+ getEnv("WMILL_CONFIG_DIR") ??
205
+ config_dir() ??
206
+ tmp_dir() ??
207
+ "/tmp/";
208
+ return baseDir;
209
+ }
210
+ async function getConfigDirPath(configDirOverride) {
211
+ const baseDir = await getBaseConfigDir(configDirOverride);
212
+ const store = baseDir + `/${exports.WINDMILL_CONFIG_DIR}/`;
213
+ await ensureDir(store);
214
+ return store;
215
+ }
216
+ async function getWorkspaceConfigFilePath(configDirOverride) {
217
+ const configDir = await getConfigDirPath(configDirOverride);
218
+ return `${configDir}/${exports.WINDMILL_WORKSPACE_CONFIG_FILE}`;
219
+ }
220
+ async function getActiveWorkspaceConfigFilePath(configDirOverride) {
221
+ const configDir = await getConfigDirPath(configDirOverride);
222
+ return `${configDir}/${exports.WINDMILL_ACTIVE_WORKSPACE_FILE}`;
223
+ }
224
+ async function getInstancesConfigFilePath(configDirOverride) {
225
+ const configDir = await getConfigDirPath(configDirOverride);
226
+ return `${configDir}/${exports.INSTANCES_CONFIG_FILE}`;
227
+ }
228
+ async function getActiveInstanceFilePath(configDirOverride) {
229
+ const configDir = await getConfigDirPath(configDirOverride);
230
+ return `${configDir}/${exports.WINDMILL_ACTIVE_INSTANCE_FILE}`;
231
+ }
@@ -0,0 +1 @@
1
+ export * from "./config";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./config"), exports);
@@ -30,7 +30,7 @@ exports.OpenAPI = {
30
30
  PASSWORD: undefined,
31
31
  TOKEN: getEnv("WM_TOKEN"),
32
32
  USERNAME: undefined,
33
- VERSION: '1.516.0',
33
+ VERSION: '1.518.2',
34
34
  WITH_CREDENTIALS: true,
35
35
  interceptors: {
36
36
  request: new Interceptors(),
package/dist/index.d.ts CHANGED
@@ -10,4 +10,5 @@
10
10
  export * from "./inline-scripts";
11
11
  export * from "./path-utils";
12
12
  export * from "./parse";
13
+ export * from "./config";
13
14
  export { SEP, DELIMITER } from "./constants";
package/dist/index.js CHANGED
@@ -27,6 +27,7 @@ exports.DELIMITER = exports.SEP = void 0;
27
27
  __exportStar(require("./inline-scripts"), exports);
28
28
  __exportStar(require("./path-utils"), exports);
29
29
  __exportStar(require("./parse"), exports);
30
+ __exportStar(require("./config"), exports);
30
31
  var constants_1 = require("./constants");
31
32
  Object.defineProperty(exports, "SEP", { enumerable: true, get: function () { return constants_1.SEP; } });
32
33
  Object.defineProperty(exports, "DELIMITER", { enumerable: true, get: function () { return constants_1.DELIMITER; } });
@@ -13,9 +13,11 @@ const path_assigner_1 = require("../path-utils/path-assigner");
13
13
  * @returns Array of inline scripts with their paths and content
14
14
  */
15
15
  function extractInlineScripts(modules, mapping = {}, separator = "/", defaultTs) {
16
+ const pathAssigner = (0, path_assigner_1.newPathAssigner)(defaultTs ?? "bun");
16
17
  return modules.flatMap((m) => {
17
18
  if (m.value.type == "rawscript") {
18
- const [basePath, ext] = (0, path_assigner_1.assignPath)(m.id, m.value.language, defaultTs);
19
+ let basePath, ext;
20
+ [basePath, ext] = pathAssigner.assignPath(m.summary, m.value.language);
19
21
  const path = mapping[m.id] ?? basePath + ext;
20
22
  const content = m.value.content;
21
23
  const r = [{ path: path, content: content }];
@@ -40,32 +40,30 @@ async function replaceInlineScripts(modules, fileReader, logger = {
40
40
  }
41
41
  }
42
42
  // rename the file if the prefix is different from the module id (fix old naming)
43
- if (pathPrefix != module.id && renamer) {
44
- logger.info(`Renaming ${path} to ${module.id}.${pathSuffix}`);
45
- try {
46
- renamer(localPath + path, localPath + module.id + "." + pathSuffix);
47
- }
48
- catch {
49
- logger.info(`Failed to rename ${path} to ${module.id}.${pathSuffix}`);
50
- }
51
- }
43
+ // if (pathPrefix != module.id && renamer) {
44
+ // logger.info(`Renaming ${path} to ${module.id}.${pathSuffix}`);
45
+ // try {
46
+ // renamer(localPath + path, localPath + module.id + "." + pathSuffix);
47
+ // } catch {
48
+ // logger.info(`Failed to rename ${path} to ${module.id}.${pathSuffix}`);
49
+ // }
50
+ // }
52
51
  const lock = module.value.lock;
53
52
  if (removeLocks && removeLocks.includes(path)) {
54
53
  module.value.lock = undefined;
55
54
  // delete the file if the prefix is different from the module id (fix old naming)
56
- if (lock && lock != "") {
57
- const path = lock.split(" ")[1];
58
- const pathPrefix = path.split(".")[0];
59
- if (pathPrefix != module.id && deleter) {
60
- logger.info(`Deleting ${path}`);
61
- try {
62
- deleter(localPath + path);
63
- }
64
- catch {
65
- logger.error(`Failed to delete ${path}`);
66
- }
67
- }
68
- }
55
+ // if (lock && lock != "") {
56
+ // const path = lock.split(" ")[1];
57
+ // const pathPrefix = path.split(".")[0];
58
+ // if (pathPrefix != module.id && deleter) {
59
+ // logger.info(`Deleting ${path}`);
60
+ // try {
61
+ // deleter(localPath + path);
62
+ // } catch {
63
+ // logger.error(`Failed to delete ${path}`);
64
+ // }
65
+ // }
66
+ // }
69
67
  }
70
68
  else if (lock &&
71
69
  typeof lock == "string" &&
@@ -16,13 +16,13 @@ export declare const LANGUAGE_EXTENSIONS: Record<SupportedLanguage, string>;
16
16
  * @returns File extension string (without the dot)
17
17
  */
18
18
  export declare function getLanguageExtension(language: SupportedLanguage, defaultTs?: "bun" | "deno"): string;
19
+ export interface PathAssigner {
20
+ assignPath(summary: string | undefined, language: SupportedLanguage): [string, string];
21
+ }
19
22
  /**
20
- * Assigns a file path for an inline script based on its ID and language.
21
- * Returns both the base path and extension as separate components.
23
+ * Creates a new path assigner for inline scripts.
22
24
  *
23
- * @param id - Unique identifier for the script
24
- * @param language - Programming language of the script
25
25
  * @param defaultTs - Default TypeScript runtime ("bun" or "deno")
26
- * @returns Tuple containing [basePath, extension]
26
+ * @returns Path assigner function
27
27
  */
28
- export declare function assignPath(id: string, language: SupportedLanguage, defaultTs?: "bun" | "deno"): [string, string];
28
+ export declare function newPathAssigner(defaultTs: "bun" | "deno"): PathAssigner;
@@ -2,7 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LANGUAGE_EXTENSIONS = void 0;
4
4
  exports.getLanguageExtension = getLanguageExtension;
5
- exports.assignPath = assignPath;
5
+ exports.newPathAssigner = newPathAssigner;
6
+ const INLINE_SCRIPT_PREFIX = "inline_script";
6
7
  /**
7
8
  * Mapping of supported languages to their file extensions
8
9
  */
@@ -29,7 +30,8 @@ exports.LANGUAGE_EXTENSIONS = {
29
30
  ansible: "playbook.yml",
30
31
  java: "java",
31
32
  duckdb: "duckdb.sql",
32
- bunnative: "ts"
33
+ bunnative: "ts",
34
+ // for related places search: ADD_NEW_LANG
33
35
  };
34
36
  /**
35
37
  * Gets the appropriate file extension for a given programming language.
@@ -46,15 +48,29 @@ function getLanguageExtension(language, defaultTs = "bun") {
46
48
  return exports.LANGUAGE_EXTENSIONS[language] || "no_ext";
47
49
  }
48
50
  /**
49
- * Assigns a file path for an inline script based on its ID and language.
50
- * Returns both the base path and extension as separate components.
51
+ * Creates a new path assigner for inline scripts.
51
52
  *
52
- * @param id - Unique identifier for the script
53
- * @param language - Programming language of the script
54
53
  * @param defaultTs - Default TypeScript runtime ("bun" or "deno")
55
- * @returns Tuple containing [basePath, extension]
54
+ * @returns Path assigner function
56
55
  */
57
- function assignPath(id, language, defaultTs = "bun") {
58
- const ext = getLanguageExtension(language, defaultTs);
59
- return [`${id}.inline_script.`, ext];
56
+ function newPathAssigner(defaultTs) {
57
+ let counter = 0;
58
+ const seen_names = new Set();
59
+ function assignPath(summary, language) {
60
+ let name;
61
+ name = summary?.toLowerCase()?.replaceAll(" ", "_") ?? "";
62
+ let original_name = name;
63
+ if (name == "") {
64
+ original_name = INLINE_SCRIPT_PREFIX;
65
+ name = `${INLINE_SCRIPT_PREFIX}_0`;
66
+ }
67
+ while (seen_names.has(name)) {
68
+ counter++;
69
+ name = `${original_name}_${counter}`;
70
+ }
71
+ seen_names.add(name);
72
+ const ext = getLanguageExtension(language, defaultTs);
73
+ return [`${name}.inline_script.`, ext];
74
+ }
75
+ return { assignPath };
60
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "windmill-utils-internal",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Internal utility functions for Windmill",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",