Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add config command #43

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"ignore": [
"@examples/hackernews",
"@examples/normal",
"@examples/normal",
"@examples/shadcn-cli",
"@examples/tailwindcss",
"@examples/with-antd4"
]
}
5 changes: 5 additions & 0 deletions .changeset/rotten-monkeys-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@umijs/tnf': patch
---

feat: add config command
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ $ pnpm preview
- `tnf generate/g <type> <name>`: Generate a new page (or component and other types in the future).
- `tnf preview`: Preview the product after building the project.
- `tnf sync --mode=<mode>`: Sync the project to the temporary directory.
- `tnf config [ list | get | set | remove ] [name] [value]`: Quickly view and modify configurations through the command line.

## API

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"get-port-please": "^3.1.2",
"http-proxy-middleware": "^3.0.3",
"json5": "^2.2.3",
"magicast": "^0.3.5",
"pathe": "^1.1.2",
"picocolors": "^1.1.1",
"random-color": "^1.0.1",
Expand Down
20 changes: 14 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ async function run(cwd: string) {
case 'sync':
const { sync } = await import('./sync/sync.js');
return sync({ context });
case 'config':
const { config } = await import('./config/config.js');
return config({ context });
default:
throw new Error(`Unknown command: ${cmd}`);
}
Expand Down
109 changes: 109 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import {
loadConfig as loadC12Config,
watchConfig as watchC12Config,
} from 'c12';
import { updateConfig } from 'c12/update';
import pc from 'picocolors';
import { CONFIG_FILE } from '../constants';
import type { Context } from '../types';
import type { Config } from './types';
import { ConfigSchema } from './types';

Expand Down Expand Up @@ -48,3 +51,109 @@ function createLoadConfigOpts({ cwd, defaults, overrides }: ConfigOpts) {
overrides,
};
}

export function list(config: Config, name?: string) {
const getValue = (value: any) => {
if (typeof value !== 'function') {
return value;
}
return pc.yellow('The value data type does not support the view');
};
const print = (key: string) => {
console.log(
` - ${pc.blue(`[key: ${key}]`)}`,
getValue(config[key as keyof Config]),
);
console.log();
};
console.log();
console.log(` Configs:`);
console.log();
if (name) {
if (
!config[name as keyof Config] &&
config[name as keyof Config] !== false
) {
// current key not existed
throw new Error(`key '${name}' not found`);
}
print(name as string);
} else {
// list all
Object.keys(config).forEach((key) => {
print(key);
});
}

console.log();
}

export async function setConfig({
cwd,
name,
value,
type = 'set',
}: {
cwd: string;
name: string | number | undefined;
value: any;
type?: string | 'set' | 'remove';
}) {
const configOpts = createLoadConfigOpts({ cwd });
await updateConfig({
...configOpts,
onUpdate: (_config) => {
if (!name) {
console.log(pc.yellow(`key '${name}' not found`));
return;
}
const _value = _config[name];
if (type === 'remove') {
if (_config.hasOwnProperty(name)) {
delete _config[name];
}
} else {
if (value) {
let safeValue: string | number | boolean | undefined = value;
if (safeValue === 'true') {
safeValue = true;
} else if (safeValue === 'false') {
safeValue = false;
}
_config[name] = safeValue as string | number | undefined;
}
}
const result = ConfigSchema.safeParse(_config);
if (!result.success) {
console.log(
pc.yellow(`Invalid configuration: ${result.error.message}`),
);
// Verification failed, recall modification
_config[name] = _value;
} else {
console.log(
pc.green(`${type} config:${name} on ${configOpts.configFile}`),
);
}
},
});
}
export async function config({ context }: { context: Context }) {
const { _ } = context.argv;
const [, command, name, value] = _;

switch (command) {
case 'list':
list(context.config);
break;
case 'get':
list(context.config, `${name}`);
break;
case 'set':
case 'remove':
setConfig({ cwd: context.cwd, name, value, type: command });
break;
default:
throw new Error(`Unsupported sub command ${command} for tnf config.`);
}
}
Loading