Skip to content

Commit

Permalink
feat: cli for renaming packages
Browse files Browse the repository at this point in the history
  • Loading branch information
linbudu599 committed Mar 15, 2022
1 parent af73567 commit 1f29b81
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 23 deletions.
2 changes: 2 additions & 0 deletions scripts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useResetWorkspacePackages from './reset-workspace';
import useCreateSimplePackage from './create-package';
import useCopyPackage from './copy-package';
import useCachePackage from './cache-package';
import useRenameWorkspacePackage from './rename-package';

const cli = cac('LinbuduLab-Starter');

Expand All @@ -20,6 +21,7 @@ useResetWorkspacePackages(cli);
useCreateSimplePackage(cli);
useCopyPackage(cli);
useCachePackage(cli);
useRenameWorkspacePackage(cli);

cli.help();
cli.parse();
30 changes: 7 additions & 23 deletions scripts/init-workspace.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { CAC } from 'cac';
import fs from 'fs-extra';
import enquirer from 'enquirer';
import chalk from 'chalk';
import { CLIUtils, Constants } from './utils';

export default function useInitWorkspaceAfterInstall(cli: CAC) {
Expand All @@ -12,29 +10,15 @@ export default function useInitWorkspaceAfterInstall(cli: CAC) {
.action(async () => {
const existPackages = CLIUtils.existPackages;

const { preserveStarter } = await enquirer.prompt<{
preserveStarter: string[];
}>({
type: 'multiselect',
choices: existPackages.map((p) => {
const color = CLIUtils.findInfoFromKeywords(p)?.color ?? null;
const chosedStarters = await CLIUtils.createPackageMultiSelector(
'chosedStarters',
'Pick starters to initialize workspace',
true
);

return {
name: p,
value: p,
message: color ? chalk.hex(color)(p) : p,
};
}),
muliple: true,
sort: true,
scroll: true,
name: 'preserveStarter',
message: 'Choose starters you want to use for this time!',
});

const excluded = preserveStarter.includes(Constants.noneIdentifier)
const excluded = chosedStarters.includes(Constants.noneIdentifier)
? existPackages
: existPackages.filter((p) => !preserveStarter.includes(p));
: existPackages.filter((p) => !chosedStarters.includes(p));

for (const project of excluded) {
const projectSrcPath = CLIUtils.resolvePackageDir(project);
Expand Down
57 changes: 57 additions & 0 deletions scripts/rename-package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { CAC } from 'cac';
import fs from 'fs-extra';
import enquirer from 'enquirer';
import chalk from 'chalk';
import { CLIUtils } from './utils';
import consola from 'consola';
import path from 'path';

// raw, renamed
type Raw2RenamedTuple = [string, string];

export default function useRenameWorkspacePackage(cli: CAC) {
cli
.command('rename', 'pick starters and rename them', {
allowUnknownOptions: true,
})
.action(async () => {
const chosedStarters = await CLIUtils.createPackageMultiSelector(
'chosedStarters',
'Pick starters to rename them',
true
);

const tasks: Array<Raw2RenamedTuple> = [];

for (const pkg of chosedStarters) {
const { renamed } = await enquirer.prompt<{ renamed: string }>({
type: 'input',
name: 'renamed',
message: `Rename package ${chalk.bold.green(pkg)}`,
});

tasks.push([pkg, renamed]);
}

if (!tasks.length) {
consola.error('No copy tasks available.');
}

for (const [raw, renamed] of tasks) {
const rawDir = CLIUtils.resolvePackageDir(raw);
const dest = CLIUtils.resolvePackageDir(renamed);

console.info(
`Rename template ${chalk.bold.yellow(raw)} to ${chalk.bold.green(
renamed
)}`
);

fs.renameSync(rawDir, dest);

const destPkgJsonPath = path.join(dest, 'package.json');

CLIUtils.modifyPackageJSON(destPkgJsonPath, 'name', renamed);
}
});
}
41 changes: 41 additions & 0 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import execa from 'execa';
import preferredPM from 'preferred-pm';
import { PackageJson } from 'type-fest';
import _ from 'lodash';
import enquirer from 'enquirer';
import chalk from 'chalk';

export class Constants {
public static get demoOnlyPackages() {
Expand Down Expand Up @@ -74,6 +76,10 @@ export class Constants {
}
}

type Tmp<Name> = {
Name: string[];
};

export class CLIUtils {
public static get existPackages() {
return fs.readdirSync(
Expand Down Expand Up @@ -113,6 +119,41 @@ export class CLIUtils {
return null;
}

public static async createPackageMultiSelector<T extends string>(
name: T,
message: string,
color = true
): Promise<string[]> {
const existPackages = CLIUtils.existPackages;

const res = await enquirer.prompt<Record<T, string[]>>({
type: 'multiselect',
choices: color
? existPackages.map((p) => {
return {
name: p,
value: p,
message: color
? chalk.hex(
(
CLIUtils.findInfoFromKeywords(p) ??
Constants.starterInfoMap['other']
).color
)(p)
: p,
};
})
: existPackages,
muliple: true,
sort: true,
scroll: true,
name,
message,
});

return res[name];
}

public static readJsonSync<TParsedContent = Record<string, unknown>>(
filePath: string,
options?: {
Expand Down

0 comments on commit 1f29b81

Please sign in to comment.