chore(tools): add package selector to dev command (#9328)

This commit is contained in:
liuyi
2024-12-26 20:34:17 +08:00
committed by GitHub
parent 4ae8c2f4ef
commit 0af46c966d
18 changed files with 224 additions and 232 deletions
+54 -3
View File
@@ -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 };