@subf/config 0.3.0 → 0.4.0
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 +10 -11
- package/bin/subf.js +2 -170
- package/dist/cli.d.ts +9 -0
- package/dist/cli.js +285 -0
- package/dist/tsdown.d.ts +2 -2
- package/package.json +12 -14
- package/tsconfig.base.json +0 -19
- package/tsconfig.lib.json +0 -8
- package/tsconfig.node.json +0 -8
- package/tsconfig.web.json +0 -9
package/README.md
CHANGED
|
@@ -12,18 +12,15 @@ bun i -D @subf/config
|
|
|
12
12
|
|
|
13
13
|
### `tsconfig`
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
{
|
|
17
|
-
"extends": "@subf/config/tsc-lib"
|
|
18
|
-
}
|
|
19
|
-
```
|
|
15
|
+
Run `subf` to create or update `tsconfig.json` (the default type is `lib`):
|
|
20
16
|
|
|
21
|
-
|
|
17
|
+
```sh
|
|
18
|
+
subf
|
|
19
|
+
subf --type node # Node.js apps
|
|
20
|
+
subf --type web # SolidJS apps
|
|
21
|
+
```
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
- `tsc-node`: for Node.js apps.
|
|
25
|
-
- `tsc-web`: for SolidJS apps.
|
|
26
|
-
- `tsc-base`: base config for custom setups.
|
|
23
|
+
When `tsconfig.json` already exists, missing settings are added while existing settings are preserved. Use `--force` to overwrite generated settings. Legacy `@subf/config` entries in `extends` are removed automatically.
|
|
27
24
|
|
|
28
25
|
### `tsdown`
|
|
29
26
|
|
|
@@ -148,6 +145,8 @@ bun subf # or just `subf` if on PATH
|
|
|
148
145
|
bun subf -f # force overwrite existing files
|
|
149
146
|
```
|
|
150
147
|
|
|
148
|
+
When `oxlint` or `oxfmt` is missing, `subf` resolves the latest version from the registry and adds it to `devDependencies` (fallback chain: `$npm_config_registry` -> `$NPM_CONFIG_REGISTRY` -> the public npm registry). Registry failures are reported as warnings and do not prevent config generation.
|
|
149
|
+
|
|
151
150
|
## License
|
|
152
151
|
|
|
153
|
-
MIT
|
|
152
|
+
MIT
|
package/bin/subf.js
CHANGED
|
@@ -1,171 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { fileURLToPath } from 'node:url'
|
|
5
|
-
|
|
6
|
-
function createConfigs({ cwd = process.cwd(), force = false } = {}) {
|
|
7
|
-
const files = [
|
|
8
|
-
{
|
|
9
|
-
name: 'oxfmt.config.ts',
|
|
10
|
-
content: "import { subfFmt } from '@subf/config/oxfmt'\n\nexport default subfFmt()\n",
|
|
11
|
-
},
|
|
12
|
-
{
|
|
13
|
-
name: 'oxlint.config.ts',
|
|
14
|
-
content: "import { subfLint } from '@subf/config/oxlint'\n\nexport default subfLint()\n",
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
name: 'tsconfig.json',
|
|
18
|
-
content: `${JSON.stringify({ extends: '@subf/config/tsconfig-lib' }, null, 2)}\n`,
|
|
19
|
-
},
|
|
20
|
-
]
|
|
21
|
-
|
|
22
|
-
const created = []
|
|
23
|
-
|
|
24
|
-
for (const file of files) {
|
|
25
|
-
const full = path.join(cwd, file.name)
|
|
26
|
-
if (existsSync(full) && !force) {
|
|
27
|
-
console.log(`Skipped ${file.name} (exists). Use --force to overwrite.`)
|
|
28
|
-
continue
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
writeFileSync(full, file.content, 'utf8')
|
|
32
|
-
created.push(file.name)
|
|
33
|
-
console.log(`Wrote ${file.name}`)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (created.length === 0) {
|
|
37
|
-
console.log('No files created.')
|
|
38
|
-
} else {
|
|
39
|
-
console.log('Created:', created.join(', '))
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return created
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function getSelfVersionRange() {
|
|
46
|
-
try {
|
|
47
|
-
const currentDir = path.dirname(fileURLToPath(import.meta.url))
|
|
48
|
-
const selfPkgPath = path.resolve(currentDir, '..', 'package.json')
|
|
49
|
-
const selfPkg = JSON.parse(readFileSync(selfPkgPath, 'utf8'))
|
|
50
|
-
|
|
51
|
-
if (selfPkg && typeof selfPkg.version === 'string' && selfPkg.version.length > 0) {
|
|
52
|
-
return `^${selfPkg.version}`
|
|
53
|
-
}
|
|
54
|
-
} catch {}
|
|
55
|
-
|
|
56
|
-
return 'latest'
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function sortObjectKeys(obj) {
|
|
60
|
-
return Object.fromEntries(Object.entries(obj).sort(([a], [b]) => a.localeCompare(b)))
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function ensureDevDependency(pkg, name, versionRange) {
|
|
64
|
-
const existingDev = pkg.devDependencies && pkg.devDependencies[name]
|
|
65
|
-
if (existingDev) {
|
|
66
|
-
return { changed: false, location: 'devDependencies', version: existingDev }
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const existingDep = pkg.dependencies && pkg.dependencies[name]
|
|
70
|
-
if (existingDep) {
|
|
71
|
-
return { changed: false, location: 'dependencies', version: existingDep }
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (!pkg.devDependencies || typeof pkg.devDependencies !== 'object') {
|
|
75
|
-
pkg.devDependencies = {}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
pkg.devDependencies[name] = versionRange
|
|
79
|
-
pkg.devDependencies = sortObjectKeys(pkg.devDependencies)
|
|
80
|
-
|
|
81
|
-
return { changed: true, location: 'devDependencies', version: versionRange }
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const projectRoot = process.env.INIT_CWD || process.cwd()
|
|
85
|
-
const args = new Set(process.argv.slice(2))
|
|
86
|
-
const force = args.has('--force') || args.has('-f')
|
|
87
|
-
const help = args.has('--help') || args.has('-h')
|
|
88
|
-
|
|
89
|
-
if (help) {
|
|
90
|
-
console.log('Usage: subf [-f|--force]')
|
|
91
|
-
console.log('Create helper config files in the current project:')
|
|
92
|
-
console.log(' oxfmt.config.ts, oxlint.config.ts, tsconfig.json')
|
|
93
|
-
console.log('Use -f or --force to overwrite existing files.')
|
|
94
|
-
process.exit(0)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
try {
|
|
98
|
-
createConfigs({ cwd: projectRoot, force })
|
|
99
|
-
} catch (err) {
|
|
100
|
-
console.error('Failed to create config files:', err)
|
|
101
|
-
process.exit(1)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// If project has package.json, suggest installing @subf/config when it's not listed
|
|
105
|
-
const projectPkgPath = path.join(projectRoot, 'package.json')
|
|
106
|
-
if (!existsSync(projectPkgPath)) {
|
|
107
|
-
console.log('No package.json found in project root.')
|
|
108
|
-
console.log('Skipping dependency injection because package.json is missing.')
|
|
109
|
-
process.exit(0)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
let projectPkg
|
|
113
|
-
try {
|
|
114
|
-
projectPkg = JSON.parse(readFileSync(projectPkgPath, 'utf8'))
|
|
115
|
-
} catch (err) {
|
|
116
|
-
console.log(
|
|
117
|
-
'Created helper configs. Could not read package.json to add @subf/config.',
|
|
118
|
-
err && (err.message || err),
|
|
119
|
-
)
|
|
120
|
-
process.exit(0)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const depName = '@subf/config'
|
|
124
|
-
const depVersion = getSelfVersionRange()
|
|
125
|
-
const depResult = ensureDevDependency(projectPkg, depName, depVersion)
|
|
126
|
-
|
|
127
|
-
if (depResult.changed) {
|
|
128
|
-
try {
|
|
129
|
-
writeFileSync(projectPkgPath, `${JSON.stringify(projectPkg, null, 2)}\n`, 'utf8')
|
|
130
|
-
console.log(`Added ${depName}@${depResult.version} to devDependencies in package.json.`)
|
|
131
|
-
} catch (err) {
|
|
132
|
-
console.log(
|
|
133
|
-
`Created helper configs, but failed to write ${depName} into package.json.`,
|
|
134
|
-
err && (err.message || err),
|
|
135
|
-
)
|
|
136
|
-
process.exit(0)
|
|
137
|
-
}
|
|
138
|
-
} else {
|
|
139
|
-
console.log(
|
|
140
|
-
`${depName} already exists in ${depResult.location} (${depResult.version}). No dependency changes needed.`,
|
|
141
|
-
)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// If either oxfmt or oxlint aren't listed, hint how to install them
|
|
145
|
-
try {
|
|
146
|
-
const missingTools = []
|
|
147
|
-
const hasOxfmt = projectPkg.devDependencies?.oxfmt || projectPkg.dependencies?.oxfmt
|
|
148
|
-
const hasOxlint = projectPkg.devDependencies?.oxlint || projectPkg.dependencies?.oxlint
|
|
149
|
-
const hasTypescript =
|
|
150
|
-
projectPkg.devDependencies?.typescript || projectPkg.dependencies?.typescript
|
|
151
|
-
|
|
152
|
-
if (!hasOxfmt) {
|
|
153
|
-
missingTools.push('oxfmt')
|
|
154
|
-
}
|
|
155
|
-
if (!hasOxlint) {
|
|
156
|
-
missingTools.push('oxlint')
|
|
157
|
-
}
|
|
158
|
-
if (!hasTypescript) {
|
|
159
|
-
missingTools.push('typescript')
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (missingTools.length > 0) {
|
|
163
|
-
console.log(
|
|
164
|
-
`Hint: Install ${missingTools.join(' and ')} to use the generated configs. Example: npm install -D ${missingTools.join(
|
|
165
|
-
' ',
|
|
166
|
-
)}`,
|
|
167
|
-
)
|
|
168
|
-
}
|
|
169
|
-
} catch {}
|
|
170
|
-
|
|
171
|
-
process.exit(0)
|
|
2
|
+
import { main } from '../dist/cli.js'
|
|
3
|
+
main(process.argv.slice(2))
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/utils/tsconfig.d.ts
|
|
2
|
+
declare const TSCONFIG_TYPE: readonly ["node", "lib", "solid", "solid-lib"];
|
|
3
|
+
type TsconfigType = (typeof TSCONFIG_TYPE)[number];
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/cli.d.ts
|
|
6
|
+
declare function parseType(argv: readonly string[]): TsconfigType;
|
|
7
|
+
declare function main(argv?: readonly string[]): Promise<number>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { main, parseType };
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { createDefu } from "defu";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
//#region src/utils/tsconfig.ts
|
|
6
|
+
const TSCONFIG_TYPE = [
|
|
7
|
+
"node",
|
|
8
|
+
"lib",
|
|
9
|
+
"solid",
|
|
10
|
+
"solid-lib"
|
|
11
|
+
];
|
|
12
|
+
const BASE_COMPILER_OPTIONS = {
|
|
13
|
+
target: "esnext",
|
|
14
|
+
module: "esnext",
|
|
15
|
+
moduleResolution: "Bundler",
|
|
16
|
+
moduleDetection: "force",
|
|
17
|
+
strict: true,
|
|
18
|
+
strictNullChecks: true,
|
|
19
|
+
noUnusedLocals: true,
|
|
20
|
+
noImplicitOverride: true,
|
|
21
|
+
esModuleInterop: true,
|
|
22
|
+
isolatedModules: true,
|
|
23
|
+
skipLibCheck: true,
|
|
24
|
+
verbatimModuleSyntax: true,
|
|
25
|
+
resolveJsonModule: true,
|
|
26
|
+
noEmit: true,
|
|
27
|
+
allowImportingTsExtensions: true
|
|
28
|
+
};
|
|
29
|
+
function solidConfigs(options) {
|
|
30
|
+
options.jsx = "preserve";
|
|
31
|
+
options.jsxImportSource = "solid-js";
|
|
32
|
+
options.lib = [
|
|
33
|
+
"ES2022",
|
|
34
|
+
"DOM",
|
|
35
|
+
"DOM.Iterable"
|
|
36
|
+
];
|
|
37
|
+
options.types = ["vite/client"];
|
|
38
|
+
}
|
|
39
|
+
function getCompilerOptions(type) {
|
|
40
|
+
const options = { ...BASE_COMPILER_OPTIONS };
|
|
41
|
+
switch (type) {
|
|
42
|
+
case "solid":
|
|
43
|
+
solidConfigs(options);
|
|
44
|
+
break;
|
|
45
|
+
case "solid-lib":
|
|
46
|
+
solidConfigs(options);
|
|
47
|
+
options.noUncheckedIndexedAccess = true;
|
|
48
|
+
break;
|
|
49
|
+
case "node":
|
|
50
|
+
options.target = "ES2023";
|
|
51
|
+
options.lib = ["ES2023"];
|
|
52
|
+
options.module = "NodeNext";
|
|
53
|
+
options.moduleResolution = "NodeNext";
|
|
54
|
+
options.types = ["node"];
|
|
55
|
+
options.erasableSyntaxOnly = true;
|
|
56
|
+
break;
|
|
57
|
+
case "lib":
|
|
58
|
+
options.target = "ES2023";
|
|
59
|
+
options.lib = ["ES2023"];
|
|
60
|
+
options.noUncheckedIndexedAccess = true;
|
|
61
|
+
}
|
|
62
|
+
return options;
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/utils/config-files.ts
|
|
66
|
+
function isRecord(value) {
|
|
67
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
68
|
+
}
|
|
69
|
+
function mergeConfig(existing, generated, overwrite) {
|
|
70
|
+
const merge = createDefu((object, key, value) => {
|
|
71
|
+
if (overwrite) {
|
|
72
|
+
if (isRecord(object[key]) && isRecord(value)) return false;
|
|
73
|
+
object[key] = value;
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
if (Array.isArray(object[key]) && Array.isArray(value)) {
|
|
77
|
+
object[key] = value;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
});
|
|
82
|
+
const result = overwrite ? merge(generated, existing) : merge(existing, generated);
|
|
83
|
+
if (!overwrite) restoreNullValues(result, existing);
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
function restoreNullValues(result, source) {
|
|
87
|
+
for (const [key, value] of Object.entries(source)) if (value === null) result[key] = null;
|
|
88
|
+
else if (isRecord(value) && isRecord(result[key])) restoreNullValues(result[key], value);
|
|
89
|
+
}
|
|
90
|
+
function getTsconfigContent(fullPath, force, type) {
|
|
91
|
+
const generated = { compilerOptions: getCompilerOptions(type) };
|
|
92
|
+
if (!existsSync(fullPath)) return `${JSON.stringify(generated, null, 2)}\n`;
|
|
93
|
+
let existing;
|
|
94
|
+
try {
|
|
95
|
+
existing = JSON.parse(readFileSync(fullPath, "utf8"));
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (!force) throw new Error(`Failed to parse ${path.basename(fullPath)}: ${String(error)}`);
|
|
98
|
+
return `${JSON.stringify(generated, null, 2)}\n`;
|
|
99
|
+
}
|
|
100
|
+
if (!isRecord(existing)) {
|
|
101
|
+
if (!force) throw new Error(`${path.basename(fullPath)} must contain a JSON object`);
|
|
102
|
+
return `${JSON.stringify(generated, null, 2)}\n`;
|
|
103
|
+
}
|
|
104
|
+
const merged = mergeConfig(existing, generated, force);
|
|
105
|
+
if (typeof merged.extends === "string" && merged.extends.includes("@subf/config")) delete merged.extends;
|
|
106
|
+
const content = `${JSON.stringify(merged, null, 2)}\n`;
|
|
107
|
+
return content === readFileSync(fullPath, "utf8") ? void 0 : content;
|
|
108
|
+
}
|
|
109
|
+
function createConfigs({ cwd, force, type }) {
|
|
110
|
+
const files = [{
|
|
111
|
+
name: "oxfmt.config.ts",
|
|
112
|
+
content: "import { subfFmt } from '@subf/config/oxfmt'\n\nexport default subfFmt()\n"
|
|
113
|
+
}, {
|
|
114
|
+
name: "oxlint.config.ts",
|
|
115
|
+
content: "import { subfLint } from '@subf/config/oxlint'\n\nexport default subfLint()\n"
|
|
116
|
+
}];
|
|
117
|
+
const created = [];
|
|
118
|
+
for (const file of files) {
|
|
119
|
+
const fullPath = path.join(cwd, file.name);
|
|
120
|
+
if (existsSync(fullPath)) {
|
|
121
|
+
if (!force) {
|
|
122
|
+
console.log(`Skipped ${file.name} (exists). Use --force to overwrite.`);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
} else created.push(file.name);
|
|
126
|
+
writeFileSync(fullPath, file.content, "utf8");
|
|
127
|
+
console.log(`Wrote ${file.name}`);
|
|
128
|
+
}
|
|
129
|
+
const tsconfigPath = path.join(cwd, "tsconfig.json");
|
|
130
|
+
const tsconfigContent = getTsconfigContent(tsconfigPath, force, type);
|
|
131
|
+
if (tsconfigContent === void 0) console.log("Skipped tsconfig.json (up to date).");
|
|
132
|
+
else {
|
|
133
|
+
writeFileSync(tsconfigPath, tsconfigContent, "utf8");
|
|
134
|
+
created.push("tsconfig.json");
|
|
135
|
+
console.log("Wrote tsconfig.json");
|
|
136
|
+
}
|
|
137
|
+
if (created.length === 0) console.log("No files created.");
|
|
138
|
+
else console.log("Created:", created.join(", "));
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region src/utils/registry.ts
|
|
142
|
+
function getRegistry(env = process.env) {
|
|
143
|
+
return (env.npm_config_registry || env.NPM_CONFIG_REGISTRY || "https://registry.npmjs.org").replace(/\/+$/, "");
|
|
144
|
+
}
|
|
145
|
+
async function getLatestVersion(name, registry) {
|
|
146
|
+
const controller = new AbortController();
|
|
147
|
+
const timeout = setTimeout(() => controller.abort(), 1e4);
|
|
148
|
+
try {
|
|
149
|
+
const response = await fetch(`${registry}/${encodeURIComponent(name)}`, { signal: controller.signal });
|
|
150
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
151
|
+
const version = (await response.json())["dist-tags"]?.latest;
|
|
152
|
+
if (typeof version !== "string" || !version.trim()) throw new Error("missing dist-tags.latest");
|
|
153
|
+
return version.trim();
|
|
154
|
+
} finally {
|
|
155
|
+
clearTimeout(timeout);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region src/utils/package-json.ts
|
|
160
|
+
function sortObjectKeys(obj) {
|
|
161
|
+
return Object.fromEntries(Object.entries(obj).sort(([a], [b]) => a.localeCompare(b)));
|
|
162
|
+
}
|
|
163
|
+
function ensureDevDependency(pkg, name, versionRange) {
|
|
164
|
+
const existingDev = pkg.devDependencies?.[name];
|
|
165
|
+
if (existingDev) return {
|
|
166
|
+
changed: false,
|
|
167
|
+
location: "devDependencies",
|
|
168
|
+
version: existingDev
|
|
169
|
+
};
|
|
170
|
+
const existingDep = pkg.dependencies?.[name];
|
|
171
|
+
if (existingDep) return {
|
|
172
|
+
changed: false,
|
|
173
|
+
location: "dependencies",
|
|
174
|
+
version: existingDep
|
|
175
|
+
};
|
|
176
|
+
if (!pkg.devDependencies || typeof pkg.devDependencies !== "object") pkg.devDependencies = {};
|
|
177
|
+
pkg.devDependencies[name] = versionRange;
|
|
178
|
+
pkg.devDependencies = sortObjectKeys(pkg.devDependencies);
|
|
179
|
+
return {
|
|
180
|
+
changed: true,
|
|
181
|
+
location: "devDependencies",
|
|
182
|
+
version: versionRange
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function getSelfVersionRange() {
|
|
186
|
+
try {
|
|
187
|
+
const moduleDirectory = path.dirname(fileURLToPath(import.meta.url));
|
|
188
|
+
const packagePath = [path.resolve(moduleDirectory, "..", "package.json"), path.resolve(moduleDirectory, "..", "..", "package.json")].find((candidate) => existsSync(candidate));
|
|
189
|
+
if (!packagePath) return "latest";
|
|
190
|
+
const pkg = JSON.parse(readFileSync(packagePath, "utf8"));
|
|
191
|
+
if (typeof pkg?.version === "string" && pkg.version.length > 0) return `^${pkg.version}`;
|
|
192
|
+
} catch {}
|
|
193
|
+
return "latest";
|
|
194
|
+
}
|
|
195
|
+
function hasDependency(pkg, name) {
|
|
196
|
+
return Boolean(pkg.devDependencies?.[name] || pkg.dependencies?.[name]);
|
|
197
|
+
}
|
|
198
|
+
async function updateProjectPackage({ projectRoot, registry }) {
|
|
199
|
+
const packagePath = path.join(projectRoot, "package.json");
|
|
200
|
+
if (!existsSync(packagePath)) {
|
|
201
|
+
console.log("No package.json found in project root.");
|
|
202
|
+
console.log("Skipping dependency injection because package.json is missing.");
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
let projectPackage;
|
|
206
|
+
try {
|
|
207
|
+
projectPackage = JSON.parse(readFileSync(packagePath, "utf8"));
|
|
208
|
+
} catch (error) {
|
|
209
|
+
console.log("Created helper configs. Could not read package.json.", error);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
let changed = false;
|
|
213
|
+
if (projectPackage.name !== "@subf/config") {
|
|
214
|
+
const result = ensureDevDependency(projectPackage, "@subf/config", getSelfVersionRange());
|
|
215
|
+
changed ||= result.changed;
|
|
216
|
+
if (result.changed) console.log(`Added @subf/config@${result.version} to devDependencies.`);
|
|
217
|
+
else console.log(`@subf/config already exists in ${result.location} (${result.version}). No dependency changes needed.`);
|
|
218
|
+
}
|
|
219
|
+
for (const name of ["oxfmt", "oxlint"]) {
|
|
220
|
+
const existing = projectPackage.devDependencies?.[name] || projectPackage.dependencies?.[name];
|
|
221
|
+
if (existing) {
|
|
222
|
+
console.log(`${name} already exists (${existing}). No dependency changes needed.`);
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
try {
|
|
226
|
+
const version = await getLatestVersion(name, registry);
|
|
227
|
+
changed ||= ensureDevDependency(projectPackage, name, `^${version}`).changed;
|
|
228
|
+
console.log(`Added ${name}@^${version} to devDependencies.`);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
console.warn(`Warning: could not resolve latest ${name} from ${registry}:`, error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (changed) try {
|
|
234
|
+
writeFileSync(packagePath, `${JSON.stringify(projectPackage, null, 2)}\n`, "utf8");
|
|
235
|
+
} catch (error) {
|
|
236
|
+
console.log("Created helper configs, but failed to write package.json.", error);
|
|
237
|
+
}
|
|
238
|
+
const missingTools = [
|
|
239
|
+
"oxfmt",
|
|
240
|
+
"oxlint",
|
|
241
|
+
"typescript"
|
|
242
|
+
].filter((name) => !hasDependency(projectPackage, name));
|
|
243
|
+
if (missingTools.length > 0) console.log(`Hint: Install ${missingTools.join(" and ")} to use the generated configs.`);
|
|
244
|
+
}
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/cli.ts
|
|
247
|
+
function parseType(argv) {
|
|
248
|
+
const index = argv.findIndex((arg) => arg === "--type" || arg.startsWith("--type="));
|
|
249
|
+
if (index < 0) return "lib";
|
|
250
|
+
const option = argv[index] ?? "";
|
|
251
|
+
const value = option.startsWith("--type=") ? option.slice(7) : argv[index + 1];
|
|
252
|
+
if (!TSCONFIG_TYPE.some((item) => item === value)) throw new Error(`Invalid type: ${value}. Must be one of ${TSCONFIG_TYPE.join(",")}.`);
|
|
253
|
+
return value;
|
|
254
|
+
}
|
|
255
|
+
async function main(argv = process.argv.slice(2)) {
|
|
256
|
+
const args = new Set(argv);
|
|
257
|
+
const force = args.has("--force") || args.has("-f");
|
|
258
|
+
const help = args.has("--help") || args.has("-h");
|
|
259
|
+
const type = parseType(argv);
|
|
260
|
+
const projectRoot = process.env.INIT_CWD || process.cwd();
|
|
261
|
+
if (help) {
|
|
262
|
+
console.log("Usage: subf [--type lib|node|web] [-f|--force]");
|
|
263
|
+
console.log("");
|
|
264
|
+
console.log("Create oxfmt.config.ts, oxlint.config.ts, and tsconfig.json.");
|
|
265
|
+
console.log("The tsconfig type defaults to lib. Existing tsconfig.json is merged automatically; use --force to overwrite existing config keys.");
|
|
266
|
+
return 0;
|
|
267
|
+
}
|
|
268
|
+
try {
|
|
269
|
+
createConfigs({
|
|
270
|
+
cwd: projectRoot,
|
|
271
|
+
force,
|
|
272
|
+
type
|
|
273
|
+
});
|
|
274
|
+
} catch (error) {
|
|
275
|
+
console.error("Failed to create config files:", error);
|
|
276
|
+
return 1;
|
|
277
|
+
}
|
|
278
|
+
await updateProjectPackage({
|
|
279
|
+
projectRoot,
|
|
280
|
+
registry: getRegistry()
|
|
281
|
+
});
|
|
282
|
+
return 0;
|
|
283
|
+
}
|
|
284
|
+
//#endregion
|
|
285
|
+
export { main, parseType };
|
package/dist/tsdown.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DepsConfig, ExportsOptions, TsdownInputOption, UserConfig } from "tsdown";
|
|
1
|
+
import { DepsConfig, DtsOptions, ExportsOptions, TsdownInputOption, UserConfig } from "tsdown";
|
|
2
2
|
//#region src/tsdown.d.ts
|
|
3
3
|
interface DepsOptions {
|
|
4
4
|
/**
|
|
@@ -27,7 +27,7 @@ interface LibOptions extends DepsOptions {
|
|
|
27
27
|
* @default true
|
|
28
28
|
*/
|
|
29
29
|
extraExports?: ExportsOptions["customExports"];
|
|
30
|
-
dts?:
|
|
30
|
+
dts?: DtsOptions & DepsOptions;
|
|
31
31
|
plugins?: UserConfig["plugins"];
|
|
32
32
|
overrides?: UserConfig;
|
|
33
33
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@subf/config",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Universal configuration for TypeScript, oxlint (+unocss, +solid), oxfmt, and tsdown.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"config",
|
|
@@ -30,7 +30,6 @@
|
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
32
|
"dist",
|
|
33
|
-
"tsconfig.*.json",
|
|
34
33
|
"bin"
|
|
35
34
|
],
|
|
36
35
|
"type": "module",
|
|
@@ -38,11 +37,7 @@
|
|
|
38
37
|
"./oxfmt": "./dist/oxfmt.js",
|
|
39
38
|
"./oxlint": "./dist/oxlint.js",
|
|
40
39
|
"./tsdown": "./dist/tsdown.js",
|
|
41
|
-
"./package.json": "./package.json"
|
|
42
|
-
"./tsconfig-base": "./tsconfig.base.json",
|
|
43
|
-
"./tsconfig-node": "./tsconfig.node.json",
|
|
44
|
-
"./tsconfig-web": "./tsconfig.web.json",
|
|
45
|
-
"./tsconfig-lib": "./tsconfig.lib.json"
|
|
40
|
+
"./package.json": "./package.json"
|
|
46
41
|
},
|
|
47
42
|
"publishConfig": {
|
|
48
43
|
"access": "public"
|
|
@@ -51,23 +46,26 @@
|
|
|
51
46
|
"build": "tsdown",
|
|
52
47
|
"dev": "tsdown --watch",
|
|
53
48
|
"typecheck": "tsc --noEmit",
|
|
49
|
+
"lint": "oxlint --fix",
|
|
50
|
+
"format": "oxfmt",
|
|
54
51
|
"release": "bumpp --all",
|
|
55
|
-
"qa": "
|
|
56
|
-
"prepublishOnly": "
|
|
52
|
+
"qa": "nub run lint && nub run format && nub run typecheck",
|
|
53
|
+
"prepublishOnly": "nub run build"
|
|
57
54
|
},
|
|
58
55
|
"dependencies": {
|
|
59
56
|
"@subf/oxlint-plugin": "^0.3.0"
|
|
60
57
|
},
|
|
61
58
|
"devDependencies": {
|
|
62
|
-
"@
|
|
63
|
-
"@types/bun": "^1.3.14",
|
|
59
|
+
"@types/node": "^24",
|
|
64
60
|
"bumpp": "^11.1.0",
|
|
65
|
-
"oxfmt": "^0.
|
|
66
|
-
"oxlint": "^1.
|
|
61
|
+
"oxfmt": "^0.65.0",
|
|
62
|
+
"oxlint": "^1.80.0",
|
|
67
63
|
"tsdown": "^0.22.14",
|
|
68
64
|
"typescript": "^7.0.2"
|
|
69
65
|
},
|
|
70
66
|
"peerDependencies": {
|
|
71
|
-
"defu": "6.1.7"
|
|
67
|
+
"defu": "6.1.7",
|
|
68
|
+
"oxfmt": "^0",
|
|
69
|
+
"oxlint": "^1"
|
|
72
70
|
}
|
|
73
71
|
}
|
package/tsconfig.base.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "esnext",
|
|
4
|
-
"module": "esnext",
|
|
5
|
-
"lib": ["ESNext"],
|
|
6
|
-
"resolveJsonModule": true,
|
|
7
|
-
"strict": true,
|
|
8
|
-
"strictNullChecks": true,
|
|
9
|
-
"noUnusedLocals": true,
|
|
10
|
-
"noImplicitOverride": true,
|
|
11
|
-
"esModuleInterop": true,
|
|
12
|
-
"isolatedModules": true,
|
|
13
|
-
"verbatimModuleSyntax": true,
|
|
14
|
-
"skipLibCheck": true,
|
|
15
|
-
"declaration": true,
|
|
16
|
-
"sourceMap": true,
|
|
17
|
-
"noEmit": true
|
|
18
|
-
}
|
|
19
|
-
}
|
package/tsconfig.lib.json
DELETED
package/tsconfig.node.json
DELETED