0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

表題通り、package.jsonの中のscriptsに記載された内容を表示するコマンドを作ります。
意外と毎回確認するのは大変なので、便利コマンドを作っておくと楽できます!!

ソースコード

scripts/list-scripts.js
import { promises as fs } from "node:fs";
import path from "node:path";
import chalk from "chalk";

// package.jsonのパスを取得
const packageJsonPath = path.join(process.cwd(), "package.json");

// package.jsonを読み込み
fs.readFile(packageJsonPath, "utf8")
	.then((data) => {
		const packageJson = JSON.parse(data);
		const scripts = packageJson.scripts;

		if (scripts) {
			console.log(chalk.green("Scripts available in package.json:"));
			for (const [script, command] of Object.entries(scripts)) {
				console.log(chalk.blue(`- ${script}:`), chalk.yellow(command));
			}
		} else {
			console.log(chalk.red("No scripts found in package.json."));
		}
	})
	.catch((err) => {
		console.error(chalk.red("Error reading or parsing package.json:"), err);
		process.exit(1);
	});
~/develop/pokemon_react_ddd (feat/pokemon_ddd_step5)$ yarn command-list

yarn run v1.22.19
$ node scripts/list-scripts.js
Scripts available in package.json:
- dev: vite
- build: tsc -b && vite build
- lint: eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0
- preview: vite preview
- test: jest
- command-list: node scripts/list-scripts.js
✨  Done in 0.51s.

以下のようにコマンドの内容に色がつきます。
スクリーンショット 2024-07-11 21.26.30.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?