Skip to content

Commit c1f4938

Browse files
chore: reduce dep (#125)
* chore: reduce dep * fix test * fix: fmt
1 parent e427c54 commit c1f4938

File tree

8 files changed

+52
-50
lines changed

8 files changed

+52
-50
lines changed

package-lock.json

Lines changed: 11 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@
3939
],
4040
"devDependencies": {
4141
"@types/mocha": "^10.0.6",
42-
"@types/node": "^20.11.18",
42+
"@types/node": "^22.9.0",
4343
"mocha": "^10.3.0",
4444
"rimraf": "^5.0.5",
4545
"ts-node": "^10.9.2",
4646
"typescript": "^5.3.3"
4747
},
4848
"dependencies": {
49-
"kolorist": "^1.8.0",
5049
"node-stream-zip": "^1.15.0",
5150
"semiver": "^1.1.0"
5251
}

src/bin.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env node
22
// Copyright 2024 the JSR authors. MIT license.
3-
import * as kl from "kolorist";
43
import * as fs from "node:fs";
54
import * as path from "node:path";
65
import { parseArgs } from "node:util";
@@ -18,6 +17,7 @@ import {
1817
JsrPackageNameError,
1918
prettyTime,
2019
setDebug,
20+
styleText,
2121
} from "./utils";
2222
import { PkgManagerName } from "./pkg_manager";
2323

@@ -31,7 +31,7 @@ function prettyPrintRow(rows: [string, string][]) {
3131
}
3232

3333
return rows
34-
.map((row) => ` ${kl.green(row[0].padStart(max))} ${row[1]}`)
34+
.map((row) => ` ${styleText("green", row[0].padEnd(max))} ${row[1]}`)
3535
.join("\n");
3636
}
3737

@@ -121,7 +121,7 @@ function getPackages(positionals: string[], allowEmpty: boolean): JsrPackage[] {
121121
const packages = pkgArgs.map((p) => JsrPackage.from(p));
122122

123123
if (!allowEmpty && pkgArgs.length === 0) {
124-
console.error(kl.red(`Missing packages argument.`));
124+
console.error(styleText("red", `Missing packages argument.`));
125125
console.log();
126126
printHelp();
127127
process.exit(1);
@@ -161,7 +161,7 @@ if (args.length === 0) {
161161
} else if (cmd === "view" || cmd === "show" || cmd === "info") {
162162
const pkgName = args[1];
163163
if (pkgName === undefined) {
164-
console.log(kl.red(`Missing package name.`));
164+
console.error(styleText("red", `Missing package name.`));
165165
printHelp();
166166
process.exit(1);
167167
}
@@ -236,7 +236,7 @@ if (args.length === 0) {
236236
} else if (cmd === "run") {
237237
const script = options.positionals[1];
238238
if (!script) {
239-
console.error(kl.red(`Missing script argument.`));
239+
console.error(styleText("red", `Missing script argument.`));
240240
console.log();
241241
printHelp();
242242
process.exit(1);
@@ -270,13 +270,13 @@ async function run(fn: () => Promise<void>) {
270270
await fn();
271271
const time = Date.now() - start;
272272
console.log();
273-
console.log(`${kl.green("Completed")} in ${prettyTime(time)}`);
273+
console.log(`${styleText("green", "Completed")} in ${prettyTime(time)}`);
274274
} catch (err) {
275275
if (err instanceof JsrPackageNameError) {
276-
console.log(kl.red(err.message));
276+
console.log(styleText("red", err.message));
277277
process.exit(1);
278278
} else if (err instanceof ExecError) {
279-
console.log(kl.red(err.message));
279+
console.log(styleText("red", err.message));
280280
process.exit(err.code);
281281
}
282282

@@ -285,7 +285,7 @@ async function run(fn: () => Promise<void>) {
285285
}
286286

287287
function throwUnknownCommand(cmd: string) {
288-
console.error(kl.red(`Unknown command: ${cmd}`));
288+
console.error(styleText("red", `Unknown command: ${cmd}`));
289289
console.log();
290290
printHelp();
291291
process.exit(1);

src/commands.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2024 the JSR authors. MIT license.
22
import * as path from "node:path";
33
import * as fs from "node:fs";
4-
import * as kl from "kolorist";
4+
import { styleText } from "./utils";
55
import {
66
exec,
77
fileExists,
@@ -26,9 +26,9 @@ async function wrapWithStatus(msg: string, fn: () => Promise<void>) {
2626

2727
try {
2828
await fn();
29-
process.stdout.write(kl.green("ok") + "\n");
29+
process.stdout.write(styleText("green", "ok") + "\n");
3030
} catch (err) {
31-
process.stdout.write(kl.red("error") + "\n");
31+
process.stdout.write(styleText("red", "error") + "\n");
3232
throw err;
3333
}
3434
}
@@ -110,7 +110,7 @@ export async function install(packages: JsrPackage[], options: InstallOptions) {
110110
await setupNpmRc(root);
111111
}
112112

113-
console.log(`Installing ${kl.cyan(packages.join(", "))}...`);
113+
console.log(`Installing ${styleText("cyan", packages.join(", "))}...`);
114114
}
115115

116116
await pkgManager.install(packages, options);
@@ -121,7 +121,7 @@ export async function remove(packages: JsrPackage[], options: BaseOptions) {
121121
process.cwd(),
122122
options.pkgManagerName,
123123
);
124-
console.log(`Removing ${kl.cyan(packages.join(", "))}...`);
124+
console.log(`Removing ${styleText("cyan", packages.join(", "))}...`);
125125
await pkgManager.remove(packages);
126126
}
127127

@@ -235,17 +235,19 @@ export async function showPackageInfo(raw: string) {
235235

236236
console.log();
237237
console.log(
238-
kl.cyan(`@${pkg.scope}/${pkg.name}@${pkg.version}`) +
239-
` | latest: ${kl.magenta(meta.latest ?? "-")} | versions: ${
240-
kl.magenta(versionCount)
238+
styleText("cyan", `@${pkg.scope}/${pkg.name}@${pkg.version}`) +
239+
` | latest: ${styleText("magenta", meta.latest ?? "-")} | versions: ${
240+
styleText("magenta", versionCount.toString())
241241
}`,
242242
);
243243
console.log(npmInfo.description);
244244
console.log();
245-
console.log(`npm tarball: ${kl.cyan(versionInfo.dist.tarball)}`);
246-
console.log(`npm integrity: ${kl.cyan(versionInfo.dist.integrity)}`);
245+
console.log(`npm tarball: ${styleText("cyan", versionInfo.dist.tarball)}`);
246+
console.log(
247+
`npm integrity: ${styleText("cyan", versionInfo.dist.integrity)}`,
248+
);
247249
console.log();
248250
console.log(
249-
`published: ${kl.magenta(timeAgo(Date.now() - publishTime))}`,
251+
`published: ${styleText("magenta", timeAgo(Date.now() - publishTime))}`,
250252
);
251253
}

src/download.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import * as fs from "node:fs";
44
import * as path from "node:path";
55
import * as util from "node:util";
66
import * as stream from "node:stream";
7-
import * as kl from "kolorist";
87
import * as StreamZip from "node-stream-zip";
8+
import { styleText } from "./utils";
99

1010
const streamFinished = util.promisify(stream.finished);
1111

@@ -134,12 +134,13 @@ async function withProgressBar<T>(
134134
const percent = Math.floor((100 / options.max) * current);
135135

136136
const bar = "#".repeat((barLength / 100) * percent) + ">";
137-
const remaining = kl.blue(
137+
const remaining = styleText(
138+
"blue",
138139
"-".repeat(Math.max(barLength - bar.length, 0)),
139140
);
140-
s += ` [${kl.cyan(bar)}${remaining}] `;
141+
s += ` [${styleText("cyan", bar)}${remaining}] `;
141142
}
142-
s += kl.dim(stats);
143+
s += styleText("dim", stats);
143144

144145
if (process.stdout.isTTY) {
145146
if (logged) {

src/pkg_manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import { getLatestPackageVersion } from "./api";
33
import { InstallOptions } from "./commands";
44
import { exec, findProjectDir, JsrPackage, logDebug } from "./utils";
5-
import * as kl from "kolorist";
5+
import { styleText } from "./utils";
66
import semiver from "semiver";
77

88
async function execWithLog(cmd: string, args: string[], cwd: string) {
9-
console.log(kl.dim(`$ ${cmd} ${args.join(" ")}`));
9+
console.log(styleText("dim", `$ ${cmd} ${args.join(" ")}`));
1010
return exec(cmd, args, cwd);
1111
}
1212

src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2024 the JSR authors. MIT license.
22
import * as path from "node:path";
33
import * as fs from "node:fs";
4+
import * as util from "node:util";
45
import { PkgManagerName } from "./pkg_manager";
56
import { spawn } from "node:child_process";
67

@@ -303,3 +304,7 @@ export async function writeTextFile(
303304
} catch (_) {}
304305
await fs.promises.writeFile(file, content, "utf-8");
305306
}
307+
308+
export const styleText = typeof util.styleText === "function"
309+
? util.styleText
310+
: (_style: string[] | string, text: string) => text;

test/commands.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as path from "path";
22
import * as fs from "fs";
3-
import * as kl from "kolorist";
43
import {
54
DenoJson,
65
enableYarnBerry,
@@ -839,9 +838,9 @@ describe("show", () => {
839838
undefined,
840839
true,
841840
);
842-
const txt = kl.stripColors(output.combined);
843-
assert.ok(txt.includes("latest:"));
844-
assert.ok(txt.includes("npm tarball:"));
841+
842+
assert.ok(output.combined.includes("latest:"));
843+
assert.ok(output.combined.includes("npm tarball:"));
845844
});
846845

847846
it("can use 'view' alias", async () => {
@@ -865,8 +864,8 @@ describe("show", () => {
865864
undefined,
866865
true,
867866
);
868-
const txt = kl.stripColors(output.combined);
869-
assert.ok(txt.includes("latest: -"));
870-
assert.ok(txt.includes("npm tarball:"));
867+
868+
assert.ok(output.combined.includes("latest: -"));
869+
assert.ok(output.combined.includes("npm tarball:"));
871870
});
872871
});

0 commit comments

Comments
 (0)