cloud-run-functions 0.1.1 → 0.1.3
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/main.js +49 -25
- package/dist/targets/dev.js +66 -49
- package/package.json +2 -2
package/dist/main.js
CHANGED
|
@@ -1,29 +1,53 @@
|
|
|
1
1
|
// src/main.ts
|
|
2
|
-
import
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
command,
|
|
4
|
+
option,
|
|
5
|
+
optional,
|
|
6
|
+
positional,
|
|
7
|
+
run,
|
|
8
|
+
string,
|
|
9
|
+
subcommands
|
|
10
|
+
} from "cmd-ts";
|
|
11
|
+
var dev = command({
|
|
12
|
+
name: "dev",
|
|
13
|
+
description: "Start the development server",
|
|
14
|
+
args: {
|
|
15
|
+
root: positional({
|
|
16
|
+
type: optional(string),
|
|
17
|
+
displayName: "root",
|
|
18
|
+
description: "Directory to search for function entrypoints"
|
|
19
|
+
}),
|
|
20
|
+
port: option({
|
|
21
|
+
type: optional(string),
|
|
22
|
+
long: "port",
|
|
23
|
+
short: "p",
|
|
24
|
+
description: "The port to use for the development server"
|
|
25
|
+
})
|
|
26
|
+
},
|
|
27
|
+
async handler({ root, port }) {
|
|
28
|
+
const { dev: dev2 } = await import("./tools/dev.js");
|
|
29
|
+
await dev2(root, { port });
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
var build = command({
|
|
33
|
+
name: "build",
|
|
34
|
+
description: "Generate a bundle for each function",
|
|
35
|
+
args: {
|
|
36
|
+
root: positional({
|
|
37
|
+
type: optional(string),
|
|
38
|
+
displayName: "root",
|
|
39
|
+
description: "Directory to search for function entrypoints"
|
|
40
|
+
})
|
|
41
|
+
},
|
|
42
|
+
async handler() {
|
|
43
|
+
throw new Error("Not implemented");
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
var cli = subcommands({
|
|
4
47
|
name: "cloud-run-functions",
|
|
5
|
-
|
|
6
|
-
dev
|
|
7
|
-
|
|
8
|
-
positionals: { maximum: 1 }
|
|
9
|
-
},
|
|
10
|
-
build: {
|
|
11
|
-
description: "Generate a bundle for each function",
|
|
12
|
-
positionals: { maximum: 1 }
|
|
13
|
-
}
|
|
48
|
+
cmds: {
|
|
49
|
+
dev,
|
|
50
|
+
build
|
|
14
51
|
}
|
|
15
52
|
});
|
|
16
|
-
|
|
17
|
-
console.log(ordana.generateHelpMessage(command));
|
|
18
|
-
} else {
|
|
19
|
-
const subcommands = {
|
|
20
|
-
async dev() {
|
|
21
|
-
const { dev } = await import("./tools/dev.js");
|
|
22
|
-
await dev(command.positionals[0]);
|
|
23
|
-
},
|
|
24
|
-
async build() {
|
|
25
|
-
throw new Error("Not implemented");
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
await subcommands[command.subcommand]();
|
|
29
|
-
}
|
|
53
|
+
await run(cli, process.argv.slice(2));
|
package/dist/targets/dev.js
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
import "source-map-support/register.js";
|
|
3
3
|
import functions from "@google-cloud/functions-framework";
|
|
4
4
|
import esbuild from "esbuild";
|
|
5
|
+
import { findUpSync } from "find-up-simple";
|
|
5
6
|
import fs2 from "node:fs";
|
|
6
7
|
import { Module } from "node:module";
|
|
7
8
|
import os from "node:os";
|
|
8
|
-
import
|
|
9
|
+
import path3 from "node:path";
|
|
9
10
|
|
|
10
11
|
// node_modules/.pnpm/radashi@12.4.0/node_modules/radashi/dist/radashi.js
|
|
11
12
|
var TimeoutError = class extends Error {
|
|
@@ -73,10 +74,54 @@ function isTagged(value, tag) {
|
|
|
73
74
|
return Object.prototype.toString.call(value) === tag;
|
|
74
75
|
}
|
|
75
76
|
|
|
77
|
+
// src/common/emptyDir.ts
|
|
78
|
+
import fs from "node:fs";
|
|
79
|
+
function emptyDir(dir) {
|
|
80
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
81
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
82
|
+
return dir;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/common/functionFilter.ts
|
|
86
|
+
import path from "node:path";
|
|
87
|
+
function getFunctionFilter(options) {
|
|
88
|
+
const functionGlobs = [];
|
|
89
|
+
const functionSuffixes = /* @__PURE__ */ new Set();
|
|
90
|
+
const requiredSuffix = options.entrySuffix?.replace(/^\.?/, ".") ?? "";
|
|
91
|
+
for (const glob of options.globs ?? ["**/*"]) {
|
|
92
|
+
let ext = path.extname(glob);
|
|
93
|
+
if (ext) {
|
|
94
|
+
functionSuffixes.add(requiredSuffix + ext);
|
|
95
|
+
functionGlobs.push(
|
|
96
|
+
requiredSuffix ? glob.replace(ext, requiredSuffix + ext) : glob
|
|
97
|
+
);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
for (ext of options.extensions ?? [".ts", ".js"]) {
|
|
101
|
+
ext = requiredSuffix + ext;
|
|
102
|
+
functionSuffixes.add(ext);
|
|
103
|
+
functionGlobs.push(glob + ext);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const suffixPattern = new RegExp(
|
|
107
|
+
`(${Array.from(functionSuffixes, (e) => e.replace(/\./g, "\\.")).sort((a, b) => b.length - a.length).join("|")})$`
|
|
108
|
+
);
|
|
109
|
+
return {
|
|
110
|
+
globs: functionGlobs,
|
|
111
|
+
suffixPattern
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/common/hash.ts
|
|
116
|
+
import crypto from "node:crypto";
|
|
117
|
+
function hash(data, len) {
|
|
118
|
+
return crypto.createHash("sha256").update(data).digest("hex").slice(0, len);
|
|
119
|
+
}
|
|
120
|
+
|
|
76
121
|
// src/config/index.ts
|
|
77
122
|
import * as z2 from "@zod/mini";
|
|
78
123
|
import Joycon from "joycon";
|
|
79
|
-
import
|
|
124
|
+
import path2 from "node:path";
|
|
80
125
|
|
|
81
126
|
// src/config/schema.ts
|
|
82
127
|
import * as z from "@zod/mini";
|
|
@@ -140,53 +185,19 @@ function loadConfig(cwd) {
|
|
|
140
185
|
}
|
|
141
186
|
return {
|
|
142
187
|
...z2.parse(configSchema, result.data),
|
|
143
|
-
configDir:
|
|
188
|
+
configDir: path2.dirname(result.path)
|
|
144
189
|
};
|
|
145
190
|
}
|
|
146
191
|
|
|
147
|
-
// src/utils/emptyDir.ts
|
|
148
|
-
import fs from "node:fs";
|
|
149
|
-
function emptyDir(dir) {
|
|
150
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
151
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
152
|
-
return dir;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// src/utils/hash.ts
|
|
156
|
-
import crypto from "node:crypto";
|
|
157
|
-
function hash(data, len) {
|
|
158
|
-
return crypto.createHash("sha256").update(data).digest("hex").slice(0, len);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
192
|
// src/targets/dev.ts
|
|
162
193
|
async function createBuild() {
|
|
163
194
|
const options = JSON.parse(process.env.CRF_OPTIONS);
|
|
164
|
-
const searchDir =
|
|
195
|
+
const searchDir = path3.resolve(options.workingDir, options.searchDir ?? "");
|
|
165
196
|
const config = loadConfig(searchDir);
|
|
166
|
-
const root = config.configDir ?
|
|
167
|
-
const
|
|
168
|
-
const requiredSuffix = config.entrySuffix?.replace(/^\.?/, ".") ?? "";
|
|
169
|
-
const knownSuffixes = /* @__PURE__ */ new Set();
|
|
170
|
-
for (const glob of config.globs ?? ["**/*"]) {
|
|
171
|
-
let ext = path2.extname(glob);
|
|
172
|
-
if (ext) {
|
|
173
|
-
knownSuffixes.add(requiredSuffix + ext);
|
|
174
|
-
entryPoints.push(
|
|
175
|
-
requiredSuffix ? glob.replace(ext, requiredSuffix + ext) : glob
|
|
176
|
-
);
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
for (ext of config.extensions ?? [".ts", ".js"]) {
|
|
180
|
-
ext = requiredSuffix + ext;
|
|
181
|
-
knownSuffixes.add(ext);
|
|
182
|
-
entryPoints.push(glob + ext);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
const knownSuffixesRE = new RegExp(
|
|
186
|
-
`(${Array.from(knownSuffixes, (e) => e.replace(/\./g, "\\.")).sort((a, b) => b.length - a.length).join("|")})$`
|
|
187
|
-
);
|
|
197
|
+
const root = config.configDir ? path3.resolve(config.configDir, config.root ?? "") : searchDir;
|
|
198
|
+
const functionFilter = getFunctionFilter(config);
|
|
188
199
|
const cacheDir = emptyDir(
|
|
189
|
-
|
|
200
|
+
path3.join(
|
|
190
201
|
fs2.realpathSync(os.tmpdir()),
|
|
191
202
|
"cloud-run-functions-" + hash(root, 8)
|
|
192
203
|
)
|
|
@@ -194,7 +205,7 @@ async function createBuild() {
|
|
|
194
205
|
let pendingBuild;
|
|
195
206
|
let finishedBuild;
|
|
196
207
|
const context = await esbuild.context({
|
|
197
|
-
entryPoints,
|
|
208
|
+
entryPoints: functionFilter.globs,
|
|
198
209
|
absWorkingDir: root,
|
|
199
210
|
outdir: cacheDir,
|
|
200
211
|
define: options.define,
|
|
@@ -229,11 +240,14 @@ async function createBuild() {
|
|
|
229
240
|
});
|
|
230
241
|
await context.watch();
|
|
231
242
|
console.log("[esbuild] Watching for changes...");
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
243
|
+
const envPath = findUpSync(".env", { cwd: root });
|
|
244
|
+
if (envPath) {
|
|
245
|
+
try {
|
|
246
|
+
const dotenv = await import("dotenv");
|
|
247
|
+
dotenv.config({ path: envPath });
|
|
248
|
+
console.log("[dotenv] Environment variables loaded.");
|
|
249
|
+
} catch {
|
|
250
|
+
}
|
|
237
251
|
}
|
|
238
252
|
const taskStates = /* @__PURE__ */ new Map();
|
|
239
253
|
return {
|
|
@@ -248,7 +262,10 @@ async function createBuild() {
|
|
|
248
262
|
if (!output.entryPoint) {
|
|
249
263
|
continue;
|
|
250
264
|
}
|
|
251
|
-
const taskName = output.entryPoint.replace(
|
|
265
|
+
const taskName = output.entryPoint.replace(
|
|
266
|
+
functionFilter.suffixPattern,
|
|
267
|
+
""
|
|
268
|
+
);
|
|
252
269
|
if (url.pathname === "/" + taskName) {
|
|
253
270
|
const taskState = taskStates.get(taskName) ?? {
|
|
254
271
|
running: 0,
|
|
@@ -270,7 +287,7 @@ async function createBuild() {
|
|
|
270
287
|
taskState.running++;
|
|
271
288
|
taskStates.set(taskName, taskState);
|
|
272
289
|
const require2 = Module.createRequire(import.meta.filename);
|
|
273
|
-
let taskHandler = require2(
|
|
290
|
+
let taskHandler = require2(path3.join(root, file));
|
|
274
291
|
while (taskHandler && typeof taskHandler !== "function") {
|
|
275
292
|
taskHandler = taskHandler.default;
|
|
276
293
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cloud-run-functions",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"bin": "./dist/main.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -35,10 +35,10 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@zod/mini": "4.0.0-beta.0",
|
|
38
|
+
"cmd-ts": "^0.14.3",
|
|
38
39
|
"esbuild": "^0.25.4",
|
|
39
40
|
"find-up-simple": "^1.0.1",
|
|
40
41
|
"joycon": "^3.1.1",
|
|
41
|
-
"ordana": "^0.4.0",
|
|
42
42
|
"picospawn": "^0.3.2",
|
|
43
43
|
"source-map-support": "^0.5.21",
|
|
44
44
|
"tinyglobby": "^0.2.13"
|