Skip to content

Commit e562267

Browse files
authored
feat: replace chalk/ansi-colors/glob with Node.js built-ins (#5853)
1 parent 236be5c commit e562267

30 files changed

+1041
-1048
lines changed

Gruntfile.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const childProcess = require("child_process");
22
const EOL = require("os").EOL;
33
const path = require("path");
44
const now = new Date().toISOString();
5-
const latestVersion = require('latest-version').default;
5+
const manifest = require('pacote').manifest;
66

77

88
const ENVIRONMENTS = {
@@ -260,28 +260,26 @@ function registerTestingDependenciesTasks(grunt) {
260260
return done(false);
261261
}
262262

263-
264-
// Kick off all version resolutions in parallel
265-
const versionPromises = testDependencies.map(dep => {
266-
if (dep.version) {
267-
dependenciesVersions[dep.name] = dep.version;
268-
return Promise.resolve();
269-
}
270-
return latestVersion(dep.name).then(v => {
271-
dependenciesVersions[dep.name] = v;
272-
});
273-
});
274-
275-
Promise.all(versionPromises)
276-
.then(() => {
277-
grunt.file.write(generatedVersionFilePath, JSON.stringify(dependenciesVersions, null, 2));
263+
(async () => {
264+
try {
265+
for (const dep of testDependencies) {
266+
if (dep.version) {
267+
dependenciesVersions[dep.name] = dep.version;
268+
} else {
269+
dependenciesVersions[dep.name] = await latestVersion(dep.name);
270+
}
271+
}
272+
grunt.file.write(
273+
generatedVersionFilePath,
274+
JSON.stringify(dependenciesVersions, null, 2)
275+
);
278276
grunt.log.writeln("Wrote", generatedVersionFilePath);
279277
done();
280-
})
281-
.catch(err => {
278+
} catch (err) {
282279
grunt.log.error(err);
283280
done(false);
284-
});
281+
}
282+
})();
285283
});
286284

287285
grunt.registerTask("verify_unit_testing_dependencies", function () {
@@ -291,3 +289,9 @@ function registerTestingDependenciesTasks(grunt) {
291289
});
292290
}
293291

292+
async function latestVersion(name) {
293+
// only fetches the package.json for the latest dist-tag
294+
const { version } = await manifest(name.toLowerCase(), { fullMetadata: false });
295+
return version;
296+
}
297+

lib/bootstrap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,8 @@ injector.require(
414414
injector.requirePublic("cleanupService", "./services/cleanup-service");
415415

416416
injector.require(
417-
"webpackCompilerService",
418-
"./services/webpack/webpack-compiler-service",
417+
"bundlerCompilerService",
418+
"./services/bundler/bundler-compiler-service",
419419
);
420420

421421
injector.require(

lib/color.ts

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,92 @@
1-
// using chalk as some of our other dependencies are already using it...
2-
// exporting from here so we can easily refactor to a different color library if needed
3-
import * as ansi from "ansi-colors";
4-
import * as chalk from "chalk";
1+
import { styleText, stripVTControlCharacters } from "node:util";
52

6-
export type Color = typeof chalk.Color;
3+
// Define color types based on util.inspect.colors
4+
export type StyleFormat =
5+
| "reset"
6+
| "bold"
7+
| "dim"
8+
| "italic"
9+
| "underline"
10+
| "blink"
11+
| "inverse"
12+
| "hidden"
13+
| "strikethrough"
14+
| "doubleunderline"
15+
| "black"
16+
| "red"
17+
| "green"
18+
| "yellow"
19+
| "blue"
20+
| "magenta"
21+
| "cyan"
22+
| "white"
23+
| "gray"
24+
| "redBright"
25+
| "greenBright"
26+
| "yellowBright"
27+
| "blueBright"
28+
| "magentaBright"
29+
| "cyanBright"
30+
| "whiteBright"
31+
| "bgBlack"
32+
| "bgRed"
33+
| "bgGreen"
34+
| "bgYellow"
35+
| "bgBlue"
36+
| "bgMagenta"
37+
| "bgCyan"
38+
| "bgWhite"
39+
| "bgGray"
40+
| "bgRedBright"
41+
| "bgGreenBright"
42+
| "bgYellowBright"
43+
| "bgBlueBright"
44+
| "bgMagentaBright"
45+
| "bgCyanBright"
46+
| "bgWhiteBright";
747

8-
export function stripColors(formatStr: string) {
9-
return ansi.stripColor(formatStr);
10-
}
48+
export type Color = StyleFormat;
1149

12-
export const color = chalk;
50+
// Create a chalk-like API using the Node.js util.styleText function
51+
export const color = {
52+
reset: (text: string) => styleText("reset", text),
53+
bold: (text: string) => styleText("bold", text),
54+
dim: (text: string) => styleText("dim", text),
55+
italic: (text: string) => styleText("italic", text),
56+
underline: (text: string) => styleText("underline", text),
57+
inverse: (text: string) => styleText("inverse", text),
58+
hidden: (text: string) => styleText("hidden", text),
59+
strikethrough: (text: string) => styleText("strikethrough", text),
60+
61+
// Text colors
62+
black: (text: string) => styleText("black", text),
63+
red: (text: string) => styleText("red", text),
64+
blue: (text: string) => styleText("blue", text),
65+
magenta: (text: string) => styleText("magenta", text),
66+
cyan: (text: string) => styleText("cyan", text),
67+
white: (text: string) => styleText("white", text),
68+
gray: (text: string) => styleText("gray", text),
69+
yellow: (text: string) => styleText("yellow", text),
70+
green: (text: string) => styleText("green", text),
71+
grey: (text: string) => styleText("grey", text),
72+
73+
// Background colors
74+
bgBlack: (text: string) => styleText("bgBlack", text),
75+
bgBlackBright: (text: string) => styleText("bgBlackBright", text),
76+
bgRed: (text: string) => styleText("bgRed", text),
77+
bgGreen: (text: string) => styleText("bgGreen", text),
78+
bgYellow: (text: string) => styleText("bgYellow", text),
79+
bgBlue: (text: string) => styleText("bgBlue", text),
80+
bgMagenta: (text: string) => styleText("bgMagenta", text),
81+
bgCyan: (text: string) => styleText("bgCyan", text),
82+
bgWhite: (text: string) => styleText("bgWhite", text),
83+
cyanBright: (text: string) => styleText("cyanBright", text),
84+
whiteBright: (text: string) => styleText("whiteBright", text),
85+
greenBright: (text: string) => styleText("greenBright", text),
86+
yellowBright: (text: string) => styleText("yellowBright", text),
87+
redBright: (text: string) => styleText("redBright", text),
88+
89+
styleText,
90+
};
91+
92+
export const stripColors = (text: string) => stripVTControlCharacters(text);

lib/commands/clean.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ export class CleanCommand implements ICommand {
267267
spinner.warn(
268268
`This will run "${color.yellow(
269269
`ns clean`,
270-
)}" in all the selected projects and ${color.red.bold(
270+
)}" in all the selected projects and ${color.styleText(
271+
["red", "bold"],
271272
"delete files from your system",
272273
)}!`,
273274
);

lib/commands/embedding/embed.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ export class EmbedCommand extends PrepareCommand implements ICommand {
5656
if (!this.$fs.exists(resolvedHostProjectPath)) {
5757
this.$logger.error(
5858
`The host project path ${color.yellow(
59-
hostProjectPath
60-
)} (resolved to: ${color.yellow.dim(
59+
hostProjectPath,
60+
)} (resolved to: ${color.styleText(
61+
["yellow", "dim"],
6162
resolvedHostProjectPath
6263
)}) does not exist.`
6364
);

lib/commands/post-install.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ export class PostInstallCliCommand implements ICommand {
5555
public async postCommandAction(args: string[]): Promise<void> {
5656
this.$logger.info("");
5757
this.$logger.info(
58-
color.green.bold("You have successfully installed the NativeScript CLI!")
58+
color.styleText(
59+
["green", "bold"],
60+
"You have successfully installed the NativeScript CLI!",
61+
),
5962
);
6063
this.$logger.info("");
6164
this.$logger.info("Your next step is to create a new project:");
62-
this.$logger.info(color.green.bold("ns create"));
65+
this.$logger.info(color.styleText(["green", "bold"], "ns create"));
6366

6467
this.$logger.info("");
6568
this.$logger.printMarkdown(

lib/commands/typings.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { glob } from "glob";
1+
import { glob } from "node:fs/promises";
22
import { homedir } from "os";
33
import * as path from "path";
44
import { PromptObject } from "prompts";
@@ -67,27 +67,26 @@ export class TypingsCommand implements ICommand {
6767

6868
const pattern = `${target.replaceAll(":", "/")}/**/*.{jar,aar}`;
6969

70-
const res = await glob(pattern, {
70+
const items = [];
71+
for await (const item of glob(pattern, {
7172
cwd: gradleFiles,
72-
});
73-
74-
if (!res || res.length === 0) {
75-
this.$logger.warn("No files found");
76-
return [];
77-
}
78-
79-
const items = res.map((item) => {
73+
})) {
8074
const [group, artifact, version, sha1, file] = item.split(path.sep);
81-
return {
75+
items.push({
8276
id: sha1 + version,
8377
group,
8478
artifact,
8579
version,
8680
sha1,
8781
file,
8882
path: path.resolve(gradleFiles, item),
89-
};
90-
});
83+
});
84+
}
85+
86+
if (items.length === 0) {
87+
this.$logger.warn("No files found");
88+
return [];
89+
}
9190

9291
this.$logger.clearScreen();
9392

@@ -108,9 +107,10 @@ export class TypingsCommand implements ICommand {
108107
.map((item) => {
109108
return {
110109
title: `${color.white(item.group)}:${color.greenBright(
111-
item.artifact
112-
)}:${color.yellow(item.version)} - ${color.cyanBright.bold(
113-
item.file
110+
item.artifact,
111+
)}:${color.yellow(item.version)} - ${color.styleText(
112+
["cyanBright", "bold"],
113+
item.file,
114114
)}`,
115115
value: item.id,
116116
};

lib/common/header.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ export function printHeader() {
1010

1111
const header = [
1212
color.dim("│ "),
13-
color.cyanBright.bold("{N} NativeScript "),
14-
color.whiteBright.bold("CLI"),
13+
color.styleText(["cyanBright", "bold"], "{N} NativeScript "),
14+
color.styleText(["whiteBright", "bold"], "CLI"),
1515
color.dim(` [v${version}] `),
1616
// color.dim(" │"),
1717
].join("");
1818
const tagLine = [
1919
color.dim("│ "),
2020
color.dim(" → "),
21-
color.whiteBright.bold("Empower JavaScript with native APIs "),
21+
color.styleText(
22+
["whiteBright", "bold"],
23+
"Empower JavaScript with native APIs ",
24+
),
2225
// color.dim(" │"),
2326
].join("");
2427

lib/common/logger/layouts/cli-layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function layout(config: any) {
2222
}
2323

2424
if (logEvent.level.isEqualTo(LoggerLevel.ERROR)) {
25-
return color.red.bold(msg);
25+
return color.styleText(["red", "bold"], msg);
2626
}
2727

2828
if (logEvent.level.isEqualTo(LoggerLevel.WARN)) {

lib/common/logger/logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ export class Logger implements ILogger {
136136
const opts = {
137137
unescape: true,
138138
link: color.red,
139-
strong: color.green.bold,
140-
firstHeading: color.blue.bold,
139+
strong: (str: string) => color.styleText(["green", "bold"], str),
140+
firstHeading: (str: string) => color.styleText(["blue", "bold"], str),
141141
tableOptions: {
142142
chars: { mid: "", "left-mid": "", "mid-mid": "", "right-mid": "" },
143143
style: {

0 commit comments

Comments
 (0)