@wolfstar/create-http-framework 2.9.0-next-20261006173153 → 2.10.0-next-20261009130446
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/README.md +11 -0
- package/dist/index.js +114 -17
- package/package.json +1 -1
- package/template/base/AGENTS.md.hbs +3 -0
- package/template/base/README.md.hbs +1 -1
- package/template/features/varlock/.env.schema.hbs +35 -0
- package/template/features/varlock/src/@types/env.d.ts.hbs +20 -0
- package/template/features/varlock/src/lib/types/augments.ts.hbs +9 -0
package/README.md
CHANGED
|
@@ -67,6 +67,7 @@ The CLI will guide you through the following prompts:
|
|
|
67
67
|
| `--redis` / `--no-redis` | | Cache in Redis instead of memory (turns `--cache` on) |
|
|
68
68
|
| `--sharder` / `--no-sharder` | | Enable or disable `@wolfstar/plugin-sharder` (turns `--gateway` on) |
|
|
69
69
|
| `--tunnel` / `--no-tunnel` | | Open a cloudflared quick tunnel in `stars dev` (`dev.tunnel` in the config) |
|
|
70
|
+
| `--env <loader>` | | `varlock`: scaffold a `.env.schema` and derive `Env` from it |
|
|
70
71
|
| `--install` / `--no-install` | | Enable or disable dependency installation |
|
|
71
72
|
| `--help` | `-h` | Print usage and exit |
|
|
72
73
|
|
|
@@ -125,6 +126,16 @@ my-discord-bot/
|
|
|
125
126
|
- With a linter, the generated configuration (`.oxlintrc.json` or `eslint.config.mjs`) enables the rules of [`@wolfstar/eslint-plugin-http-framework`](../eslint-plugin-http-framework): decorator order, raw Discord fetches, dynamic translation keys and the other mistakes TypeScript cannot catch.
|
|
126
127
|
- `--tunnel` (or the **Dev tunnel** feature in the prompt) writes `dev: { tunnel: true }` to `stars.config`, so `stars dev` opens a cloudflared quick tunnel and Discord reaches the bot on your machine. Without it, press `t` in `stars dev` to open one on demand.
|
|
127
128
|
|
|
129
|
+
### varlock
|
|
130
|
+
|
|
131
|
+
`--env varlock` (or the **Environment schema** feature in the prompt) sets the project up with [varlock](https://varlock.dev), which validates the environment and types it:
|
|
132
|
+
|
|
133
|
+
- `.env.schema` declares `DISCORD_TOKEN`, `DISCORD_PUBLIC_KEY`, `DISCORD_CLIENT_ID`, the HTTP port and whatever the other features need (`REDIS_URL`, `SHARDER_CLUSTERS`). The values stay in `.env`.
|
|
134
|
+
- `varlock` is added as a dependency, and `@wolfstar/env-utilities` loads the schema by itself (it picks varlock when a `.env.schema` and `varlock` are there).
|
|
135
|
+
- `src/lib/types/augments.ts` derives `Env` from `src/@types/env.d.ts` with `EnvFromVarlock` (from `@wolfstar/env-utilities/varlock`) instead of declaring each variable by hand. The scaffold ships a stand-in for that file so the project type-checks right away; `@generateTsTypes` makes varlock replace it, with the same shape, whenever it loads the schema (`npm run dev`, or `npx varlock load`).
|
|
136
|
+
- For `tsdown` and `vite` projects `stars.config.ts` also gets `env: { loader: 'varlock' }`, so `stars dev` and `stars doctor` read the variables the way the bot does. `tsc`, JavaScript and Nitro projects load the environment themselves, so they get the schema and the dependency but no `env` entry.
|
|
137
|
+
- Running it against a directory that already has a varlock `.env.schema` (`--ignore`) needs no flag: the schema is kept as it is, `varlock` stays a dependency, and `stars.config.ts` sets `env.loader`. An existing `.env.schema` the generator did not write is never replaced, with or without `--env varlock`; with the flag it is kept and reported, and `Env` stays hand-written because that schema may not generate types. A `varlock.loadPath` in `package.json` is kept. Rerunning without the flag over a project this generator scaffolded with varlock keeps it on; delete `.env.schema` to turn it off.
|
|
138
|
+
|
|
128
139
|
### Vite and Nitro
|
|
129
140
|
|
|
130
141
|
`--build vite` bundles the bot with [Vite](https://vite.dev) into a single `dist/main.js`, and `--build vite-nitro` hands it to [Nitro](https://nitro.build), which writes a deployable `.output/` (the `node-server` preset, pinned in `stars.config.ts`). Both are TypeScript only and turn on `experimental.enableVite` (and `enableNitro`) in the generated `stars.config.ts`.
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { cancel, confirm, intro, isCancel, log, multiselect, outro, select, spinner, text } from "@clack/prompts";
|
|
3
3
|
import { determineAgent } from "@vercel/detect-agent";
|
|
4
4
|
import mri from "mri";
|
|
5
|
-
import { dirname, join, relative, resolve } from "node:path";
|
|
5
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
6
6
|
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
7
7
|
import spawn from "cross-spawn";
|
|
8
8
|
import { resolveCommand } from "package-manager-detector/commands";
|
|
@@ -95,6 +95,8 @@ const FORMATTERS = [
|
|
|
95
95
|
* package only has dev builds).
|
|
96
96
|
*/
|
|
97
97
|
const TYPESCRIPT_RC_VERSION = "7.0.1-rc";
|
|
98
|
+
/** The environment loaders `--env` can scaffold. `varlock` adds a starter `.env.schema` and derives `Env` from it. */
|
|
99
|
+
const ENV_LOADERS = ["varlock"];
|
|
98
100
|
|
|
99
101
|
//#endregion
|
|
100
102
|
//#region src/tools/npmHelpers.ts
|
|
@@ -124,6 +126,7 @@ async function fetchDependencyVersions(selections) {
|
|
|
124
126
|
if (selections.cache) names.add("@wolfstar/plugin-cache");
|
|
125
127
|
if (selections.redis) names.add("ioredis");
|
|
126
128
|
if (selections.sharder) names.add("@wolfstar/plugin-sharder");
|
|
129
|
+
if (selections.varlock) names.add("varlock");
|
|
127
130
|
if (selections.language === "ts") {
|
|
128
131
|
names.add("@types/node");
|
|
129
132
|
switch (selections.buildTool) {
|
|
@@ -287,6 +290,7 @@ function buildDependencies(ctx) {
|
|
|
287
290
|
if (ctx.cache) dependencies["@wolfstar/plugin-cache"] = caret(v["@wolfstar/plugin-cache"]);
|
|
288
291
|
if (ctx.redis) dependencies["ioredis"] = caret(v["ioredis"]);
|
|
289
292
|
if (ctx.sharder) dependencies["@wolfstar/plugin-sharder"] = caret(v["@wolfstar/plugin-sharder"]);
|
|
293
|
+
if (ctx.varlock) dependencies["varlock"] = caret(v["varlock"]);
|
|
290
294
|
return sortKeys(dependencies);
|
|
291
295
|
}
|
|
292
296
|
function buildDevDependencies(ctx) {
|
|
@@ -338,6 +342,7 @@ function packageJson(ctx) {
|
|
|
338
342
|
main,
|
|
339
343
|
scripts: buildScripts(ctx),
|
|
340
344
|
dependencies: buildDependencies(ctx),
|
|
345
|
+
...ctx.varlockLoadPath ? { varlock: { loadPath: ctx.varlockLoadPath } } : {},
|
|
341
346
|
...Object.keys(devDependencies).length > 0 ? { devDependencies } : {},
|
|
342
347
|
engines: { node: ctx.gateway ? ">=24.17.0" : ">=20" }
|
|
343
348
|
});
|
|
@@ -445,9 +450,17 @@ function writeTsconfig(targetDir, ctx) {
|
|
|
445
450
|
* entry transform that registers it, so `src/lib/setup` loads the environment itself (see `registersEnv` in the
|
|
446
451
|
* template processor).
|
|
447
452
|
*/
|
|
453
|
+
/**
|
|
454
|
+
* Whether `stars` loads `env` from `stars.config`: the entry of a `tsdown` or Vite build passes through its transform. A
|
|
455
|
+
* `tsc` or JavaScript project loads the environment itself (`env: false`), and Nitro leaves `env` off by default.
|
|
456
|
+
*/
|
|
457
|
+
function registersEnvFromConfig(ctx) {
|
|
458
|
+
return ctx.language === "ts" && (ctx.buildTool === "tsdown" || ctx.buildTool === "vite");
|
|
459
|
+
}
|
|
448
460
|
function writeStarsConfig(targetDir, ctx) {
|
|
449
461
|
const isJs = ctx.language === "js";
|
|
450
462
|
const parts = !isJs && (ctx.buildTool === "tsc6" || ctx.buildTool === "tsc7") ? ["build: { tool: 'tsc' }", "env: false"] : isJs ? ["env: false"] : [];
|
|
463
|
+
if (ctx.varlockLoader && registersEnvFromConfig(ctx)) parts.push("env: { loader: 'varlock' }");
|
|
451
464
|
if (!isJs && isViteBuild(ctx.buildTool)) {
|
|
452
465
|
const nitro = isNitroBuild(ctx.buildTool) ? ", enableNitro: true, nitro: { preset: 'node-server' }" : "";
|
|
453
466
|
parts.push("build: { tool: 'vite' }", `experimental: { enableVite: true${nitro} }`);
|
|
@@ -567,11 +580,21 @@ function removeI18nDeclaration(outputDir) {
|
|
|
567
580
|
if (existsSync(typesDir) && readdirSync(typesDir).length === 0) rmSync(typesDir, { recursive: true });
|
|
568
581
|
}
|
|
569
582
|
/**
|
|
570
|
-
* The files that describe a project to coding agents. A project often has its own already, and
|
|
571
|
-
* they are
|
|
572
|
-
* edited it since.
|
|
583
|
+
* The files that describe a project to coding agents, the varlock schema and the types varlock generates from it. A project often has its own already, and
|
|
584
|
+
* unlike the sources they are not something a rerun can merge: an existing one is only replaced when this generator
|
|
585
|
+
* wrote it and nobody edited it since.
|
|
586
|
+
*/
|
|
587
|
+
const USER_OWNED_FILES = /* @__PURE__ */ new Set([
|
|
588
|
+
"AGENTS.md",
|
|
589
|
+
"llms.txt",
|
|
590
|
+
".env.schema",
|
|
591
|
+
"src/@types/env.d.ts"
|
|
592
|
+
]);
|
|
593
|
+
/**
|
|
594
|
+
* Owned by a tool, not by the user: varlock overwrites it whenever it loads the schema. It is kept as it is, without the
|
|
595
|
+
* "delete it and run again" report, which would only bring back the stand-in.
|
|
573
596
|
*/
|
|
574
|
-
const
|
|
597
|
+
const TOOL_GENERATED_FILES = /* @__PURE__ */ new Set(["src/@types/env.d.ts"]);
|
|
575
598
|
/**
|
|
576
599
|
* What the Handlebars sources see: the persisted {@link TemplateContext} plus values derived from it. Deriving at render
|
|
577
600
|
* time keeps the manifest small and lets manifests written before these fields existed still render.
|
|
@@ -622,6 +645,7 @@ function resolveFeatureDirs(ctx) {
|
|
|
622
645
|
if (ctx.cache) dirs.push("cache");
|
|
623
646
|
if (ctx.redis) dirs.push("redis");
|
|
624
647
|
if (ctx.sharder) dirs.push("sharder");
|
|
648
|
+
if (ctx.varlock) dirs.push("varlock");
|
|
625
649
|
if (ctx.subcommandsAdvanced) dirs.push(ctx.i18n ? "subcommands-advanced-i18n" : "subcommands-advanced");
|
|
626
650
|
else if (ctx.subcommands) dirs.push(ctx.i18n ? "subcommands-i18n" : "subcommands");
|
|
627
651
|
if (ctx.testing) {
|
|
@@ -754,6 +778,7 @@ function removeStaleGeneratedFiles(outputDir, context) {
|
|
|
754
778
|
const preserved = [];
|
|
755
779
|
for (const [path, sources] of candidates) {
|
|
756
780
|
if (keepPaths.has(path)) continue;
|
|
781
|
+
if (path === ".env.schema") continue;
|
|
757
782
|
const target = join(outputDir, path);
|
|
758
783
|
if (!existsSync(target)) continue;
|
|
759
784
|
const actual = readFileSync(target, "utf-8");
|
|
@@ -761,7 +786,7 @@ function removeStaleGeneratedFiles(outputDir, context) {
|
|
|
761
786
|
const matcher = buildStaleFileMatcher(source);
|
|
762
787
|
return (matcher ? [matcher] : buildLegacyVariantMatchers(source, context)).some((candidate) => candidate.test(actual));
|
|
763
788
|
})) rmSync(target);
|
|
764
|
-
else preserved.push(path);
|
|
789
|
+
else if (!TOOL_GENERATED_FILES.has(path.split(sep).join("/"))) preserved.push(path);
|
|
765
790
|
}
|
|
766
791
|
return preserved;
|
|
767
792
|
}
|
|
@@ -786,34 +811,86 @@ function processDir(root, outputDir, context, keep) {
|
|
|
786
811
|
}
|
|
787
812
|
}
|
|
788
813
|
/**
|
|
814
|
+
* Settles whether the varlock scaffold applies, from the schema that is on disk rather than from the flag alone:
|
|
815
|
+
* - a schema this generator wrote earlier (the manifest says so) keeps varlock on without the flag, so a rerun does not
|
|
816
|
+
* take `Env` away from a schema that is still there, and deleting the schema is how to turn it off;
|
|
817
|
+
* - a schema somebody else wrote is kept as it is and `Env` stays hand-written, because the types the scaffold derives
|
|
818
|
+
* `Env` from are only generated when the schema asks for them.
|
|
819
|
+
*/
|
|
820
|
+
function resolveVarlock(outputDir, context, manifestContext, onKept) {
|
|
821
|
+
if (!existsSync(join(outputDir, ".env.schema"))) return context;
|
|
822
|
+
if (manifestContext?.varlock) return {
|
|
823
|
+
...context,
|
|
824
|
+
varlock: true
|
|
825
|
+
};
|
|
826
|
+
if (!context.varlock) return context;
|
|
827
|
+
onKept?.(".env.schema");
|
|
828
|
+
return {
|
|
829
|
+
...context,
|
|
830
|
+
varlock: false
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
/**
|
|
789
834
|
* Renders `template/base/` into `outputDir`, then layers the feature directories resolved by
|
|
790
835
|
* {@link resolveFeatureDirs} on top, in order — feature files overwrite base files at the same
|
|
791
836
|
* output-relative path (e.g. `features/i18n/src/main.ts.hbs` overwrites `base/src/main.ts.hbs`).
|
|
792
837
|
*
|
|
793
|
-
* @param onKept Called with each of `AGENTS.md`/`llms.txt` that was left as it is, because it exists and is not this
|
|
838
|
+
* @param onKept Called with each of `AGENTS.md`/`llms.txt`/`.env.schema` that was left as it is, because it exists and is not this
|
|
794
839
|
* generator's own unedited output.
|
|
795
840
|
* @returns Output-relative paths that looked stale (belong to a disabled feature or the other
|
|
796
841
|
* language) but were left in place because they'd been hand-edited since the last run.
|
|
797
842
|
*/
|
|
798
|
-
async function processTemplate(outputDir,
|
|
843
|
+
async function processTemplate(outputDir, requested, onKept) {
|
|
799
844
|
const manifestContext = readManifest(outputDir);
|
|
845
|
+
const context = resolveVarlock(outputDir, requested, manifestContext, onKept);
|
|
800
846
|
const preserved = removeStaleGeneratedFiles(outputDir, context);
|
|
801
847
|
if (!context.i18n) removeI18nDeclaration(outputDir);
|
|
802
|
-
|
|
803
|
-
|
|
848
|
+
const keepEdited = (root) => (outputRelative, content) => {
|
|
849
|
+
const key = outputRelative.split(sep).join("/");
|
|
850
|
+
if (!USER_OWNED_FILES.has(key)) return false;
|
|
804
851
|
const target = join(outputDir, outputRelative);
|
|
805
852
|
if (!existsSync(target)) return false;
|
|
806
853
|
const actual = readFileSync(target, "utf-8");
|
|
807
|
-
const source = join(
|
|
808
|
-
const pristine = actual === content || manifestContext !== void 0 && renderSource(source, manifestContext) === actual;
|
|
809
|
-
if (!pristine) onKept?.(
|
|
854
|
+
const source = join(root, `${outputRelative}.hbs`);
|
|
855
|
+
const pristine = actual === content || manifestContext !== void 0 && existsSync(source) && renderSource(source, manifestContext) === actual;
|
|
856
|
+
if (!pristine && !TOOL_GENERATED_FILES.has(key)) onKept?.(key);
|
|
810
857
|
return !pristine;
|
|
811
|
-
}
|
|
812
|
-
|
|
858
|
+
};
|
|
859
|
+
processDir(baseDir, outputDir, context, keepEdited(baseDir));
|
|
860
|
+
for (const feature of resolveFeatureDirs(context)) {
|
|
861
|
+
const root = join(featuresDir, feature);
|
|
862
|
+
processDir(root, outputDir, context, keepEdited(root));
|
|
863
|
+
}
|
|
813
864
|
writeManifest(outputDir, context);
|
|
814
865
|
return preserved;
|
|
815
866
|
}
|
|
816
867
|
|
|
868
|
+
//#endregion
|
|
869
|
+
//#region src/tools/varlock.ts
|
|
870
|
+
function readPackageJson(directory) {
|
|
871
|
+
try {
|
|
872
|
+
const parsed = JSON.parse(readFileSync(join(directory, "package.json"), "utf-8"));
|
|
873
|
+
return parsed !== null && typeof parsed === "object" ? parsed : {};
|
|
874
|
+
} catch {
|
|
875
|
+
return {};
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* Looks for varlock in a directory that already has a project (`--ignore`), so the generated `stars.config` can say
|
|
880
|
+
* `env: { loader: 'varlock' }` instead of dropping what the project already uses. The schema lookup mirrors
|
|
881
|
+
* `detectVarlock` of `@wolfstar/schema` (`tests/varlock.test.ts` runs both over the same fixtures); the scaffold
|
|
882
|
+
* does not depend on that package.
|
|
883
|
+
*/
|
|
884
|
+
function detectExistingVarlock(directory) {
|
|
885
|
+
const packageJson = readPackageJson(directory);
|
|
886
|
+
const loadPath = packageJson.varlock?.loadPath;
|
|
887
|
+
return {
|
|
888
|
+
...typeof loadPath === "string" && loadPath !== "" ? { loadPath } : {},
|
|
889
|
+
schema: existsSync(join(directory, "src", ".env.schema")) || existsSync(join(directory, ".env.schema")) || Boolean(packageJson.varlock?.loadPath),
|
|
890
|
+
dependency: Boolean(packageJson.dependencies?.varlock ?? packageJson.devDependencies?.varlock)
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
|
|
817
894
|
//#endregion
|
|
818
895
|
//#region src/index.ts
|
|
819
896
|
function isValidPackageName(name) {
|
|
@@ -845,6 +922,7 @@ Options:
|
|
|
845
922
|
--redis / --no-redis Store the cache in Redis instead of memory (default: off; enables --cache)
|
|
846
923
|
--sharder / --no-sharder Toggle @wolfstar/plugin-sharder, gateway shards across cluster workers (default: off; enables --gateway)
|
|
847
924
|
--tunnel / --no-tunnel Open a cloudflared quick tunnel in \`stars dev\`, so Discord reaches the bot (default: off)
|
|
925
|
+
--env <loader> varlock — scaffold a .env.schema and derive Env from it (an existing .env.schema or varlock dependency is detected without it)
|
|
848
926
|
--install / --no-install Toggle dependency installation (default: on)
|
|
849
927
|
--ignore Write into an existing, non-empty directory without clearing it
|
|
850
928
|
--help, -h Print this message and exit
|
|
@@ -873,7 +951,8 @@ async function main() {
|
|
|
873
951
|
"build",
|
|
874
952
|
"lint",
|
|
875
953
|
"format",
|
|
876
|
-
"port"
|
|
954
|
+
"port",
|
|
955
|
+
"env"
|
|
877
956
|
],
|
|
878
957
|
alias: {
|
|
879
958
|
h: "help",
|
|
@@ -904,6 +983,7 @@ async function main() {
|
|
|
904
983
|
const cliRedis = argv["redis"];
|
|
905
984
|
const cliSharder = argv["sharder"];
|
|
906
985
|
const cliTunnel = argv["tunnel"];
|
|
986
|
+
const cliEnv = parseEnum(argv["env"], ENV_LOADERS, "--env");
|
|
907
987
|
const cliInstall = argv["install"];
|
|
908
988
|
const { isAgent, agent } = await determineAgent();
|
|
909
989
|
const agentMode = isAgent && flagInteractive !== true;
|
|
@@ -1132,6 +1212,7 @@ async function main() {
|
|
|
1132
1212
|
let wantsRedis;
|
|
1133
1213
|
let wantsSharder;
|
|
1134
1214
|
let wantsTunnel;
|
|
1215
|
+
let wantsVarlock;
|
|
1135
1216
|
const nitro = language === "ts" && isNitroBuild(buildTool);
|
|
1136
1217
|
if (nonInteractive) {
|
|
1137
1218
|
wantsI18n = cliI18n ?? false;
|
|
@@ -1143,6 +1224,7 @@ async function main() {
|
|
|
1143
1224
|
wantsRedis = cliRedis ?? false;
|
|
1144
1225
|
wantsSharder = cliSharder ?? false;
|
|
1145
1226
|
wantsTunnel = cliTunnel ?? false;
|
|
1227
|
+
wantsVarlock = cliEnv === "varlock";
|
|
1146
1228
|
} else {
|
|
1147
1229
|
const featuresResult = await multiselect({
|
|
1148
1230
|
message: "Which optional features would you like to add?",
|
|
@@ -1178,9 +1260,13 @@ async function main() {
|
|
|
1178
1260
|
{
|
|
1179
1261
|
value: "tunnel",
|
|
1180
1262
|
label: "Dev tunnel (a cloudflared quick tunnel, so Discord reaches the bot while developing)"
|
|
1263
|
+
},
|
|
1264
|
+
{
|
|
1265
|
+
value: "varlock",
|
|
1266
|
+
label: "Environment schema (varlock: a validated, typed .env.schema)"
|
|
1181
1267
|
}
|
|
1182
1268
|
],
|
|
1183
|
-
initialValues: cliTunnel ? ["tunnel"] : [],
|
|
1269
|
+
initialValues: [...cliTunnel ? ["tunnel"] : [], ...cliEnv === "varlock" ? ["varlock"] : []],
|
|
1184
1270
|
required: false
|
|
1185
1271
|
});
|
|
1186
1272
|
if (isCancel(featuresResult)) {
|
|
@@ -1196,6 +1282,7 @@ async function main() {
|
|
|
1196
1282
|
wantsCache = features.has("cache");
|
|
1197
1283
|
wantsSharder = features.has("sharder");
|
|
1198
1284
|
wantsTunnel = features.has("tunnel");
|
|
1285
|
+
wantsVarlock = features.has("varlock");
|
|
1199
1286
|
wantsRedis = cliRedis ?? false;
|
|
1200
1287
|
if (wantsCache && cliRedis === void 0) {
|
|
1201
1288
|
const redisResult = await confirm({
|
|
@@ -1225,6 +1312,10 @@ async function main() {
|
|
|
1225
1312
|
});
|
|
1226
1313
|
if (gatewayFeatures.implied.length > 0) log.warn(`Enabling ${gatewayFeatures.implied.map((feature) => `--${feature}`).join(" and ")}, which the selected features depend on.`);
|
|
1227
1314
|
({gateway: wantsGateway, cache: wantsCache, redis: wantsRedis, sharder: wantsSharder} = gatewayFeatures.features);
|
|
1315
|
+
const existingVarlock = detectExistingVarlock(targetDir);
|
|
1316
|
+
const usesVarlock = wantsVarlock || existingVarlock.schema || existingVarlock.dependency;
|
|
1317
|
+
const varlockLoader = wantsVarlock || existingVarlock.schema;
|
|
1318
|
+
if (!wantsVarlock && usesVarlock) log.info(`Found varlock in "${projectName}": ${existingVarlock.schema ? "its .env.schema is kept and stars.config sets env.loader to varlock" : "it stays a dependency"}.`);
|
|
1228
1319
|
let wantsInstall;
|
|
1229
1320
|
if (nonInteractive) wantsInstall = cliInstall;
|
|
1230
1321
|
else {
|
|
@@ -1249,6 +1340,7 @@ async function main() {
|
|
|
1249
1340
|
cache: wantsCache,
|
|
1250
1341
|
redis: wantsRedis,
|
|
1251
1342
|
sharder: wantsSharder,
|
|
1343
|
+
varlock: usesVarlock,
|
|
1252
1344
|
language,
|
|
1253
1345
|
buildTool,
|
|
1254
1346
|
linter,
|
|
@@ -1269,6 +1361,7 @@ async function main() {
|
|
|
1269
1361
|
cache: wantsCache,
|
|
1270
1362
|
redis: wantsRedis,
|
|
1271
1363
|
sharder: wantsSharder,
|
|
1364
|
+
varlock: wantsVarlock,
|
|
1272
1365
|
buildTool,
|
|
1273
1366
|
tunnel: wantsTunnel,
|
|
1274
1367
|
autoEnv: true,
|
|
@@ -1286,6 +1379,9 @@ async function main() {
|
|
|
1286
1379
|
redis: wantsRedis,
|
|
1287
1380
|
sharder: wantsSharder,
|
|
1288
1381
|
tunnel: wantsTunnel,
|
|
1382
|
+
varlock: usesVarlock,
|
|
1383
|
+
varlockLoader,
|
|
1384
|
+
varlockLoadPath: existingVarlock.loadPath,
|
|
1289
1385
|
compatibilityVersion: 6,
|
|
1290
1386
|
packageManager,
|
|
1291
1387
|
language,
|
|
@@ -1339,6 +1435,7 @@ async function main() {
|
|
|
1339
1435
|
if (wantsI18n) extraNotes.push(` After editing locale files, regenerate i18next types with: ${getRunScript(packageManager, "generate:i18n")}`);
|
|
1340
1436
|
if (wantsRedis) extraNotes.push(" Start a local Redis server with: docker compose up -d");
|
|
1341
1437
|
if (wantsTesting) extraNotes.push(` Run the test suite with: ${getRunScript(packageManager, "test")}`);
|
|
1438
|
+
if (wantsVarlock) extraNotes.push(" The environment is described in .env.schema (varlock); the values stay in .env");
|
|
1342
1439
|
outro(`Done! To get started:\n\n cd ${projectName}\n${wantsInstall ? "" : ` ${getInstallScript(packageManager)}\n`} ${getRunScript(packageManager, "dev")}${extraNotes.length ? `\n\n${extraNotes.join("\n")}` : ""}`);
|
|
1343
1440
|
}
|
|
1344
1441
|
main().catch((error) => {
|
package/package.json
CHANGED
|
@@ -47,6 +47,9 @@ The project is bundled into one file, so there is no `commands` directory at run
|
|
|
47
47
|
## Rules
|
|
48
48
|
|
|
49
49
|
- Secrets live in `.env` (`DISCORD_TOKEN`, `DISCORD_PUBLIC_KEY`, `DISCORD_CLIENT_ID`). Never print or commit them.
|
|
50
|
+
{{#if varlock}}
|
|
51
|
+
- The environment is described in `.env.schema` ([varlock](https://varlock.dev)); the values stay in `.env`. To add a variable, declare it there with its `@type`, then run `stars dev` (or `npx varlock load`) to regenerate `src/@types/env.d.ts`, which types `Env` (do not edit it by hand).
|
|
52
|
+
{{/if}}
|
|
50
53
|
- Call Discord through `container.rest`, not `fetch`: it handles rate limits and authentication.
|
|
51
54
|
- An interaction must be answered within 3 seconds: `reply()`, or `defer()` first and `edit()` later. `edit()` before
|
|
52
55
|
either one fails.
|
|
@@ -8,7 +8,7 @@ A Discord bot built with [`@wolfstar/http-framework`](https://www.npmjs.com/pack
|
|
|
8
8
|
|
|
9
9
|
1. Install dependencies (skip this if the generator already installed them for you).
|
|
10
10
|
|
|
11
|
-
2. This project was generated with a `.env` file already in place — it's written with placeholder values directly as `.env` (there is no separate `.env.example`), and `.gitignore` keeps it out of version control. Open `.env` and replace the placeholders with your bot's real `DISCORD_TOKEN`, `DISCORD_PUBLIC_KEY`, and `DISCORD_CLIENT_ID`.
|
|
11
|
+
2. This project was generated with a `.env` file already in place — it's written with placeholder values directly as `.env` (there is no separate `.env.example`), and `.gitignore` keeps it out of version control. Open `.env` and replace the placeholders with your bot's real `DISCORD_TOKEN`, `DISCORD_PUBLIC_KEY`, and `DISCORD_CLIENT_ID`.{{#if varlock}} The environment is described in `.env.schema` and loaded through [varlock](https://varlock.dev), which validates the values and regenerates `src/@types/env.d.ts` (the types of `Env`) whenever it loads the schema (`npm run dev`, or `npx varlock load`).{{/if}}
|
|
12
12
|
{{#if i18n}}
|
|
13
13
|
3. This project uses [`@wolfstar/plugin-i18next`](https://www.npmjs.com/package/@wolfstar/plugin-i18next). Locale resources live under `src/locales/`; after editing them, regenerate the typed i18next resources with `npm run generate:i18n`.
|
|
14
14
|
{{/if}}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Describes the environment of the bot: varlock validates it and types it, `@wolfstar/env-utilities` loads it.
|
|
2
|
+
# The values themselves stay in `.env` (and `.env.local`); this file only says what each variable must look like.
|
|
3
|
+
# @defaultRequired=true @defaultSensitive=false
|
|
4
|
+
# @generateTsTypes(path=src/@types/env.d.ts)
|
|
5
|
+
# ---
|
|
6
|
+
|
|
7
|
+
# Credentials from https://discord.com/developers/applications
|
|
8
|
+
# @type=string @sensitive
|
|
9
|
+
DISCORD_TOKEN=
|
|
10
|
+
# @type=string
|
|
11
|
+
DISCORD_PUBLIC_KEY=
|
|
12
|
+
# @type=string
|
|
13
|
+
DISCORD_CLIENT_ID=
|
|
14
|
+
|
|
15
|
+
{{#if nitro}}
|
|
16
|
+
# @type=port
|
|
17
|
+
PORT={{port}}
|
|
18
|
+
{{else}}
|
|
19
|
+
# @type=string
|
|
20
|
+
HTTP_ADDRESS=0.0.0.0
|
|
21
|
+
# @type=port
|
|
22
|
+
HTTP_PORT={{port}}
|
|
23
|
+
{{/if}}
|
|
24
|
+
# @type=string
|
|
25
|
+
HTTP_POST_PATH=/
|
|
26
|
+
{{#if redis}}
|
|
27
|
+
|
|
28
|
+
# @type=string
|
|
29
|
+
REDIS_URL=redis://localhost:6379
|
|
30
|
+
{{/if}}
|
|
31
|
+
{{#if sharder}}
|
|
32
|
+
|
|
33
|
+
# @type=number
|
|
34
|
+
SHARDER_CLUSTERS=2
|
|
35
|
+
{{/if}}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// A stand-in for the file varlock generates from `.env.schema` (`@generateTsTypes`), so `Env` type-checks before it has
|
|
2
|
+
// run. Varlock replaces it, with the same shape, the first time it loads the schema (`stars dev`, or `npx varlock load`).
|
|
3
|
+
export type CoercedEnvSchema = {
|
|
4
|
+
DISCORD_TOKEN: string;
|
|
5
|
+
DISCORD_PUBLIC_KEY: string;
|
|
6
|
+
DISCORD_CLIENT_ID: string;
|
|
7
|
+
{{#if nitro}}
|
|
8
|
+
PORT: number;
|
|
9
|
+
{{else}}
|
|
10
|
+
HTTP_ADDRESS: string;
|
|
11
|
+
HTTP_PORT: number;
|
|
12
|
+
{{/if}}
|
|
13
|
+
HTTP_POST_PATH: string;
|
|
14
|
+
{{#if redis}}
|
|
15
|
+
REDIS_URL: string;
|
|
16
|
+
{{/if}}
|
|
17
|
+
{{#if sharder}}
|
|
18
|
+
SHARDER_CLUSTERS: number;
|
|
19
|
+
{{/if}}
|
|
20
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// The `/varlock` entry is the one meant for this augmentation: the root entry shares a declaration chunk with `Env`.
|
|
2
|
+
import type { EnvFromVarlock } from '@wolfstar/env-utilities/varlock';
|
|
3
|
+
// Varlock regenerates this file from `.env.schema` (`@generateTsTypes`) whenever it loads it (`stars dev`, or `npx varlock load`).
|
|
4
|
+
import type { CoercedEnvSchema } from '../../@types/env.js';
|
|
5
|
+
|
|
6
|
+
declare module '@wolfstar/env-utilities' {
|
|
7
|
+
// `.env.schema` is the only place a variable is declared.
|
|
8
|
+
interface Env extends EnvFromVarlock<CoercedEnvSchema> {}
|
|
9
|
+
}
|