mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-05 03:25:10 +08:00
chore(tools): add package selector to dev command (#9328)
This commit is contained in:
@@ -2,6 +2,7 @@ import { AliasToPackage } from '@affine-tools/utils/distribution';
|
||||
import { Logger } from '@affine-tools/utils/logger';
|
||||
import { type PackageName, Workspace } from '@affine-tools/utils/workspace';
|
||||
import { Command as BaseCommand, Option } from 'clipanion';
|
||||
import inquirer from 'inquirer';
|
||||
import * as t from 'typanion';
|
||||
|
||||
import type { CliContext } from './context';
|
||||
@@ -32,10 +33,14 @@ export abstract class PackageCommand extends Command {
|
||||
});
|
||||
|
||||
get package(): PackageName {
|
||||
return (
|
||||
const name =
|
||||
AliasToPackage.get(this.packageNameOrAlias as any) ??
|
||||
(this.packageNameOrAlias as PackageName)
|
||||
);
|
||||
(this.packageNameOrAlias as PackageName);
|
||||
|
||||
// check
|
||||
this.workspace.getPackage(name);
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
protected _deps = Option.Boolean('--deps', false, {
|
||||
@@ -76,4 +81,50 @@ export abstract class PackagesCommand extends Command {
|
||||
});
|
||||
}
|
||||
|
||||
export abstract class PackageSelectorCommand extends Command {
|
||||
protected availablePackages = Workspace.PackageNames;
|
||||
|
||||
protected availablePackageNameArgs = (
|
||||
Workspace.PackageNames as string[]
|
||||
).concat(Array.from(AliasToPackage.keys()));
|
||||
|
||||
protected packageNameValidator = t.isOneOf(
|
||||
this.availablePackageNameArgs.map(k => t.isLiteral(k))
|
||||
);
|
||||
|
||||
protected packageNameOrAlias = Option.String('--package,-p', {
|
||||
validator: this.packageNameValidator,
|
||||
description: 'The package name or alias to be run with',
|
||||
});
|
||||
|
||||
async getPackage(): Promise<PackageName> {
|
||||
let name = this.packageNameOrAlias
|
||||
? (AliasToPackage.get(this.packageNameOrAlias as any) ??
|
||||
this.packageNameOrAlias)
|
||||
: undefined;
|
||||
|
||||
if (!name) {
|
||||
const answer = await inquirer.prompt([
|
||||
{
|
||||
type: 'list',
|
||||
name: 'package',
|
||||
message: 'Which package do you want to dev?',
|
||||
choices: this.availablePackages.map(name => ({
|
||||
name,
|
||||
value: name,
|
||||
})),
|
||||
default: '@affine/web',
|
||||
},
|
||||
]);
|
||||
|
||||
name = answer.package as PackageName;
|
||||
}
|
||||
|
||||
// check
|
||||
this.workspace.getPackage(name as PackageName);
|
||||
|
||||
return name as PackageName;
|
||||
}
|
||||
}
|
||||
|
||||
export { Option };
|
||||
|
||||
Reference in New Issue
Block a user