@newcode-team/config 0.0.0 → 0.2.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.
- package/README.md +86 -1
- package/dist/index.d.mts +44 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +76 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +41 -2
package/README.md
CHANGED
|
@@ -1,3 +1,88 @@
|
|
|
1
1
|
# @newcode-team/config
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Finds, loads and types `newcode.config.ts`, the one config file of the NewCode tools. Each tool reads its own section. No command, no validation of sections: the tools do that.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add -D @newcode-team/config
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Requires Node 24+.
|
|
10
|
+
|
|
11
|
+
## The file
|
|
12
|
+
|
|
13
|
+
One file in the project root, a section per tool. Every section is optional; without a file or a section, a tool uses its defaults.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { defineConfig } from "@newcode-team/config";
|
|
17
|
+
import { env } from "@newcode-team/env";
|
|
18
|
+
|
|
19
|
+
export default defineConfig({
|
|
20
|
+
env: env({
|
|
21
|
+
confirmScripts: ["db:migrate", "db:seed"],
|
|
22
|
+
forbiddenInBase: ["DATABASE_URL"],
|
|
23
|
+
}),
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- Names: `newcode.config.ts`, `.mjs` or `.js`. Two of them in one folder is an error.
|
|
28
|
+
- `export default` an object, wrapped in `defineConfig` for types.
|
|
29
|
+
- Plain values only, no functions. Settings name variables (`url: "DATABASE_URL"`), never hold their values: those stay in `.envs/`.
|
|
30
|
+
- `.ts` needs no build step: Node 24 strips the types itself.
|
|
31
|
+
|
|
32
|
+
## Finding
|
|
33
|
+
|
|
34
|
+
From the current folder up to the first folder with a config file, stopping at the git root or the filesystem root. So a tool finds it from a subfolder or a monorepo package. No file anywhere means `{}`.
|
|
35
|
+
|
|
36
|
+
## Types
|
|
37
|
+
|
|
38
|
+
`Config` is an empty interface. Each tool adds its section through module augmentation, so a new tool never needs a change here:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
// inside a tool
|
|
42
|
+
declare module "@newcode-team/config" {
|
|
43
|
+
interface Config {
|
|
44
|
+
sync?: SyncConfig;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
TypeScript sees a section once its tool is imported, which the section helper (`env()`, `sync()`) takes care of. Installed tools get full types and autocomplete, a typo like `snyc:` is an error, and a project without a tool has no key for it.
|
|
50
|
+
|
|
51
|
+
## API
|
|
52
|
+
|
|
53
|
+
| Export | Does |
|
|
54
|
+
|---|---|
|
|
55
|
+
| `defineConfig(config)` | Returns the config unchanged, typed as `Config` |
|
|
56
|
+
| `interface Config` | Empty. Tools add their section through module augmentation |
|
|
57
|
+
| `findConfig({ cwd? })` | Path of the config file, or `undefined`. Synchronous |
|
|
58
|
+
| `loadConfig({ cwd? })` | The default export with `root` (its folder) and `file` (its path). `{}` without a file |
|
|
59
|
+
| `ConfigError` | Thrown for every problem with the file itself |
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { loadConfig } from "@newcode-team/config";
|
|
63
|
+
|
|
64
|
+
const { env, root, file } = await loadConfig();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
- `loadConfig` imports each file once per process, so tools in one process share the result.
|
|
68
|
+
- An import that throws, such as a syntax error or a missing package, becomes a `ConfigError` naming the file, with the original error as `cause`. So does a file without a default export object.
|
|
69
|
+
- `cwd` defaults to `process.cwd()`.
|
|
70
|
+
|
|
71
|
+
## Development
|
|
72
|
+
|
|
73
|
+
Part of the [NewCode tools](../..) monorepo. From the repository root:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pnpm build # tsdown, with publint and attw
|
|
77
|
+
pnpm test # Vitest
|
|
78
|
+
pnpm lint # Oxlint
|
|
79
|
+
pnpm format # Oxfmt
|
|
80
|
+
pnpm typecheck
|
|
81
|
+
pnpm changeset # describe a change for the next release
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Releases go through Changesets: merging to `main` opens a release PR, and merging that PR publishes to npm.
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared config of the NewCode tools. Empty here: each tool adds its own section through
|
|
3
|
+
* module augmentation, e.g. `declare module "@newcode-team/config" { interface Config { … } }`
|
|
4
|
+
*/
|
|
5
|
+
export interface Config {}
|
|
6
|
+
/**
|
|
7
|
+
* A loaded config, with the folder and file it was found in. Both are missing without a file
|
|
8
|
+
*/
|
|
9
|
+
export type LoadedConfig = Config & {
|
|
10
|
+
/**
|
|
11
|
+
* The folder the config file was found in
|
|
12
|
+
*/
|
|
13
|
+
root?: string;
|
|
14
|
+
/**
|
|
15
|
+
* The absolute path of the config file
|
|
16
|
+
*/
|
|
17
|
+
file?: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A problem with the config file itself: two files in one folder, an import that throws, no
|
|
21
|
+
* default export
|
|
22
|
+
*/
|
|
23
|
+
export declare class ConfigError extends Error {
|
|
24
|
+
name: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns the config unchanged, typed as `Config`
|
|
28
|
+
*/
|
|
29
|
+
export declare const defineConfig: (config: Config) => Config;
|
|
30
|
+
/**
|
|
31
|
+
* The path of the config file: the first one walking up from cwd, stopping at the git root or the
|
|
32
|
+
* filesystem root. `undefined` without one
|
|
33
|
+
*/
|
|
34
|
+
export declare function findConfig({ cwd }?: {
|
|
35
|
+
cwd?: string;
|
|
36
|
+
}): string | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* The config of the project at cwd, `{}` without a file. Imports each file once per process.
|
|
39
|
+
* Doesn't validate sections: each tool validates its own.
|
|
40
|
+
*/
|
|
41
|
+
export declare function loadConfig({ cwd }?: {
|
|
42
|
+
cwd?: string;
|
|
43
|
+
}): Promise<LoadedConfig>;
|
|
44
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;iBASiB;;;;YAKL,eAAe;;;;EAIzB;;;;EAIA;;;;;;qBAYW,oBAAoB;EACtB;;;;;qBAME,eAAgB,QAAQ,WAAS;;;;;wBAM9B,aAAa;EAAyB;;;;;;wBA+ChC,aACpB;EACG;IAAsB,QAAQ"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
// src/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* The file names, in order of preference. Only one may exist per folder
|
|
7
|
+
*/
|
|
8
|
+
const configFiles = [
|
|
9
|
+
"newcode.config.ts",
|
|
10
|
+
"newcode.config.mjs",
|
|
11
|
+
"newcode.config.js"
|
|
12
|
+
];
|
|
13
|
+
/**
|
|
14
|
+
* A problem with the config file itself: two files in one folder, an import that throws, no
|
|
15
|
+
* default export
|
|
16
|
+
*/
|
|
17
|
+
var ConfigError = class extends Error {
|
|
18
|
+
name = "ConfigError";
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Returns the config unchanged, typed as `Config`
|
|
22
|
+
*/
|
|
23
|
+
const defineConfig = (config) => config;
|
|
24
|
+
/**
|
|
25
|
+
* The path of the config file: the first one walking up from cwd, stopping at the git root or the
|
|
26
|
+
* filesystem root. `undefined` without one
|
|
27
|
+
*/
|
|
28
|
+
function findConfig({ cwd = process.cwd() } = {}) {
|
|
29
|
+
let dir = resolve(cwd);
|
|
30
|
+
while (true) {
|
|
31
|
+
const found = configFiles.filter((name) => existsSync(join(dir, name)));
|
|
32
|
+
if (found.length > 1) throw new ConfigError(`${found.join(" and ")} in ${dir}. Keep one.`);
|
|
33
|
+
if (found[0]) return join(dir, found[0]);
|
|
34
|
+
const parent = dirname(dir);
|
|
35
|
+
if (parent === dir || existsSync(join(dir, ".git"))) return;
|
|
36
|
+
dir = parent;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const cache = /* @__PURE__ */ new Map();
|
|
40
|
+
async function importConfig(file) {
|
|
41
|
+
let module;
|
|
42
|
+
try {
|
|
43
|
+
module = await import(
|
|
44
|
+
/* @vite-ignore */
|
|
45
|
+
/* webpackIgnore: true */
|
|
46
|
+
/* turbopackIgnore: true */
|
|
47
|
+
pathToFileURL(file).href
|
|
48
|
+
);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
throw new ConfigError(`Can't load ${file}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
51
|
+
}
|
|
52
|
+
const config = module.default;
|
|
53
|
+
if (typeof config !== "object" || config === null || Array.isArray(config)) throw new ConfigError(`${file} must export default an object, e.g. defineConfig({ … })`);
|
|
54
|
+
return config;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The config of the project at cwd, `{}` without a file. Imports each file once per process.
|
|
58
|
+
* Doesn't validate sections: each tool validates its own.
|
|
59
|
+
*/
|
|
60
|
+
async function loadConfig({ cwd = process.cwd() } = {}) {
|
|
61
|
+
const file = findConfig({ cwd });
|
|
62
|
+
if (!file) return {};
|
|
63
|
+
let config = cache.get(file);
|
|
64
|
+
if (!config) {
|
|
65
|
+
config = importConfig(file);
|
|
66
|
+
cache.set(file, config);
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
...await config,
|
|
70
|
+
root: dirname(file),
|
|
71
|
+
file
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export { ConfigError, defineConfig, findConfig, loadConfig };
|
|
75
|
+
|
|
76
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { dirname, join, resolve } from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\n\n/**\n * The shared config of the NewCode tools. Empty here: each tool adds its own section through\n * module augmentation, e.g. `declare module \"@newcode-team/config\" { interface Config { … } }`\n */\n// oxlint-disable-next-line typescript/no-empty-interface, typescript/no-empty-object-type\nexport interface Config {}\n\n/**\n * A loaded config, with the folder and file it was found in. Both are missing without a file\n */\nexport type LoadedConfig = Config & {\n /**\n * The folder the config file was found in\n */\n root?: string;\n /**\n * The absolute path of the config file\n */\n file?: string;\n};\n\n/**\n * The file names, in order of preference. Only one may exist per folder\n */\nconst configFiles = [\"newcode.config.ts\", \"newcode.config.mjs\", \"newcode.config.js\"];\n\n/**\n * A problem with the config file itself: two files in one folder, an import that throws, no\n * default export\n */\nexport class ConfigError extends Error {\n override name = \"ConfigError\";\n}\n\n/**\n * Returns the config unchanged, typed as `Config`\n */\nexport const defineConfig = (config: Config): Config => config;\n\n/**\n * The path of the config file: the first one walking up from cwd, stopping at the git root or the\n * filesystem root. `undefined` without one\n */\nexport function findConfig({ cwd = process.cwd() }: { cwd?: string } = {}): string | undefined {\n let dir = resolve(cwd);\n while (true) {\n const found = configFiles.filter((name) => existsSync(join(dir, name)));\n if (found.length > 1) {\n throw new ConfigError(`${found.join(\" and \")} in ${dir}. Keep one.`);\n }\n if (found[0]) {\n return join(dir, found[0]);\n }\n\n const parent = dirname(dir);\n if (parent === dir || existsSync(join(dir, \".git\"))) {\n return undefined;\n }\n dir = parent;\n }\n}\n\nconst cache = new Map<string, Promise<Config>>();\n\nasync function importConfig(file: string): Promise<Config> {\n let module: { default?: unknown };\n try {\n // Left to Node: a bundler that follows this import can't resolve a path known only at runtime\n module = await import(\n /* @vite-ignore */ /* webpackIgnore: true */ /* turbopackIgnore: true */ pathToFileURL(file)\n .href\n );\n } catch (error) {\n throw new ConfigError(\n `Can't load ${file}: ${error instanceof Error ? error.message : String(error)}`,\n { cause: error },\n );\n }\n\n const config = module.default;\n if (typeof config !== \"object\" || config === null || Array.isArray(config)) {\n throw new ConfigError(`${file} must export default an object, e.g. defineConfig({ … })`);\n }\n return config;\n}\n\n/**\n * The config of the project at cwd, `{}` without a file. Imports each file once per process.\n * Doesn't validate sections: each tool validates its own.\n */\nexport async function loadConfig({\n cwd = process.cwd(),\n}: { cwd?: string } = {}): Promise<LoadedConfig> {\n const file = findConfig({ cwd });\n if (!file) {\n return {};\n }\n\n let config = cache.get(file);\n if (!config) {\n config = importConfig(file);\n cache.set(file, config);\n }\n return { ...(await config), root: dirname(file), file };\n}\n"],"mappings":";;;;;;;AA4BA,MAAM,cAAc;CAAC;CAAqB;CAAsB;AAAmB;;;;;AAMnF,IAAa,cAAb,cAAiC,MAAM;CAC5B,OAAO;AAClB;;;;AAKA,MAAa,gBAAgB,WAA2B;;;;;AAMxD,SAAgB,WAAW,EAAE,MAAM,QAAQ,IAAI,MAAwB,CAAC,GAAuB;CAC7F,IAAI,MAAM,QAAQ,GAAG;CACrB,OAAO,MAAM;EACX,MAAM,QAAQ,YAAY,QAAQ,SAAS,WAAW,KAAK,KAAK,IAAI,CAAC,CAAC;EACtE,IAAI,MAAM,SAAS,GACjB,MAAM,IAAI,YAAY,GAAG,MAAM,KAAK,OAAO,EAAE,MAAM,IAAI,YAAY;EAErE,IAAI,MAAM,IACR,OAAO,KAAK,KAAK,MAAM,EAAE;EAG3B,MAAM,SAAS,QAAQ,GAAG;EAC1B,IAAI,WAAW,OAAO,WAAW,KAAK,KAAK,MAAM,CAAC,GAChD;EAEF,MAAM;CACR;AACF;AAEA,MAAM,wBAAQ,IAAI,IAA6B;AAE/C,eAAe,aAAa,MAA+B;CACzD,IAAI;CACJ,IAAI;EAEF,SAAS,MAAM;;;;GAC4D,cAAc,IAAI,CAAC,CACzF;;CAEP,SAAS,OAAO;EACd,MAAM,IAAI,YACR,cAAc,KAAK,IAAI,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,KAC5E,EAAE,OAAO,MAAM,CACjB;CACF;CAEA,MAAM,SAAS,OAAO;CACtB,IAAI,OAAO,WAAW,YAAY,WAAW,QAAQ,MAAM,QAAQ,MAAM,GACvE,MAAM,IAAI,YAAY,GAAG,KAAK,yDAAyD;CAEzF,OAAO;AACT;;;;;AAMA,eAAsB,WAAW,EAC/B,MAAM,QAAQ,IAAI,MACE,CAAC,GAA0B;CAC/C,MAAM,OAAO,WAAW,EAAE,IAAI,CAAC;CAC/B,IAAI,CAAC,MACH,OAAO,CAAC;CAGV,IAAI,SAAS,MAAM,IAAI,IAAI;CAC3B,IAAI,CAAC,QAAQ;EACX,SAAS,aAAa,IAAI;EAC1B,MAAM,IAAI,MAAM,MAAM;CACxB;CACA,OAAO;EAAE,GAAI,MAAM;EAAS,MAAM,QAAQ,IAAI;EAAG;CAAK;AACxD"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@newcode-team/config",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Finds, loads and types newcode.config.ts, the shared config of the NewCode tools",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
5
7
|
"license": "MIT",
|
|
6
8
|
"author": "NewCode",
|
|
7
9
|
"repository": {
|
|
@@ -10,7 +12,44 @@
|
|
|
10
12
|
"directory": "packages/config"
|
|
11
13
|
},
|
|
12
14
|
"homepage": "https://github.com/NewCodeDevelopment/tools/tree/main/packages/config#readme",
|
|
15
|
+
"bugs": "https://github.com/NewCodeDevelopment/tools/issues",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"config",
|
|
18
|
+
"newcode"
|
|
19
|
+
],
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@arethetypeswrong/core": "^0.18.5",
|
|
22
|
+
"@types/node": "^24.19.2",
|
|
23
|
+
"publint": "^0.3.25",
|
|
24
|
+
"tsdown": "^0.23.7",
|
|
25
|
+
"typescript": "5.9.3",
|
|
26
|
+
"unrun": "^0.3.1",
|
|
27
|
+
"vitest": "^5.1.0"
|
|
28
|
+
},
|
|
13
29
|
"publishConfig": {
|
|
14
30
|
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=24"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"exports": {
|
|
39
|
+
".": "./dist/index.mjs",
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"dev": "tsdown --config tsdown.config.ts --config-loader unrun --watch --no-clean",
|
|
44
|
+
"build": "tsdown --config tsdown.config.ts --config-loader unrun --no-clean",
|
|
45
|
+
"build:release": "tsdown --config tsdown.config.ts --config-loader unrun",
|
|
46
|
+
"lint": "oxlint",
|
|
47
|
+
"lint:fix": "oxlint --fix",
|
|
48
|
+
"format": "oxfmt",
|
|
49
|
+
"format:check": "oxfmt --check",
|
|
50
|
+
"typecheck": "tsc --noEmit",
|
|
51
|
+
"test": "vitest",
|
|
52
|
+
"test:ci": "vitest run",
|
|
53
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
15
54
|
}
|
|
16
55
|
}
|