Skip to content

Commit

Permalink
refactor(insert-emit-option): migrate to citty and consola (#61)
Browse files Browse the repository at this point in the history
* chore(deps): add citty and consola

* refactor: migrate to citty

* refactor(insert-emit-option): migrate to consola
  • Loading branch information
wattanx authored Aug 22, 2024
1 parent 071c5a2 commit b1ab7be
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 75 deletions.
5 changes: 5 additions & 0 deletions packages/insert-emits-option/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
entries: ["./src/cli.ts"],
});
17 changes: 9 additions & 8 deletions packages/insert-emits-option/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
"name": "@wattanx/insert-emits-option",
"version": "0.0.17",
"description": "Insert emits option into defineComponent.",
"main": "index.js",
"bin": {
"insert-emits-option": "dist/cli.js"
"insert-emits-option": "dist/cli.mjs"
},
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"test:cli": "node dist/cli.js",
"build": "unbuild",
"build:stub": "unbuild --stub",
"test:cli": "node dist/cli.mjs",
"test": "vitest run",
"test:watch": "vitest"
},
Expand All @@ -31,15 +31,16 @@
"devDependencies": {
"@types/cli-progress": "^3.11.0",
"@types/node": "^18.11.9",
"@types/yargs": "^17.0.13",
"typescript": "^4.9.3",
"unbuild": "^2.0.0",
"vitest": "1.5.3"
},
"dependencies": {
"@vue/compiler-sfc": "^3.2.45",
"citty": "^0.1.6",
"cli-progress": "^3.11.2",
"colorette": "^2.0.19",
"ts-morph": "^16.0.0",
"yargs": "^17.6.2"
"consola": "^3.2.3",
"globby": "^14.0.2",
"ts-morph": "^16.0.0"
}
}
9 changes: 3 additions & 6 deletions packages/insert-emits-option/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/usr/bin/env node

import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { runMain } from "citty";
import main from "./main";

yargs(hideBin(process.argv))
.commandDir("commands")
.strict()
.alias({ h: "help", v: "version" }).argv;
runMain(main);
29 changes: 0 additions & 29 deletions packages/insert-emits-option/src/commands/index.ts

This file was deleted.

5 changes: 2 additions & 3 deletions packages/insert-emits-option/src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { parse } from "@vue/compiler-sfc";
import { green } from "colorette";
import { readFile, writeFile } from "fs/promises";
import { readFile, writeFile } from "node:fs/promises";
import { Project } from "ts-morph";
import { generateProgressBar } from "../lib/generateProgressBar";
import { insertEmitsOption } from "../lib/insertEmitsOption";
Expand All @@ -26,7 +25,7 @@ export const handleCommand = async (

const targetFiles = allFiles.filter((file) => file.script !== "");

const progressBar = generateProgressBar(green);
const progressBar = generateProgressBar();
progressBar.start(targetFiles.length, 0);

const project = new Project({ tsConfigFilePath: tsconfigPath });
Expand Down
7 changes: 5 additions & 2 deletions packages/insert-emits-option/src/lib/generateProgressBar.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import cliProgress from "cli-progress";
import { blue, Color } from "colorette";
import type { ColorFunction } from "consola/utils";
import { getColor } from "consola/utils";

const defaultColorFunc = getColor("green");

export const generateProgressBar = (
colorFunc: Color = blue
colorFunc: ColorFunction = defaultColorFunc
): cliProgress.SingleBar =>
new cliProgress.SingleBar(
{
Expand Down
33 changes: 33 additions & 0 deletions packages/insert-emits-option/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { handleCommand } from "./handlers";
import { colorize } from "consola/utils";
import { defineCommand } from "citty";
import pkgJson from "../package.json";
import { globby } from "globby";

export default defineCommand({
meta: {
name: pkgJson.name,
version: pkgJson.version,
description: pkgJson.description,
},
args: {
targetFilePaths: {
type: "positional",
required: true,
description:
"Path to the target vue file, which can be set with the glob pattern. eg: 'src/**/*.vue'",
},
tsconfigPath: {
type: "string",
required: false,
alias: "t",
},
},
async run({ args }) {
const { targetFilePaths } = args;
const files = await globby(targetFilePaths);
const { insertedCount } = await handleCommand(files, args.tsconfigPath);
console.log("\nCompleted 🎉");
console.log(`${colorize("green", insertedCount)} files changed.`);
},
});
2 changes: 1 addition & 1 deletion packages/insert-emits-option/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "resolveJsonModule": true, /* Enable importing .json files. */
"resolveJsonModule": true,
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Expand Down
48 changes: 22 additions & 26 deletions pnpm-lock.yaml

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

0 comments on commit b1ab7be

Please sign in to comment.