diff --git a/scripts/cli.ts b/scripts/cli.ts index 3d03e8c..541bf08 100644 --- a/scripts/cli.ts +++ b/scripts/cli.ts @@ -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'); @@ -20,6 +21,7 @@ useResetWorkspacePackages(cli); useCreateSimplePackage(cli); useCopyPackage(cli); useCachePackage(cli); +useRenameWorkspacePackage(cli); cli.help(); cli.parse(); diff --git a/scripts/init-workspace.ts b/scripts/init-workspace.ts index 739fc17..dc27f4f 100644 --- a/scripts/init-workspace.ts +++ b/scripts/init-workspace.ts @@ -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) { @@ -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); diff --git a/scripts/rename-package.ts b/scripts/rename-package.ts new file mode 100644 index 0000000..2e77db9 --- /dev/null +++ b/scripts/rename-package.ts @@ -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 = []; + + 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); + } + }); +} diff --git a/scripts/utils.ts b/scripts/utils.ts index 164bc49..f09bf94 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -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() { @@ -74,6 +76,10 @@ export class Constants { } } +type Tmp = { + Name: string[]; +}; + export class CLIUtils { public static get existPackages() { return fs.readdirSync( @@ -113,6 +119,41 @@ export class CLIUtils { return null; } + public static async createPackageMultiSelector( + name: T, + message: string, + color = true + ): Promise { + const existPackages = CLIUtils.existPackages; + + const res = await enquirer.prompt>({ + 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>( filePath: string, options?: {