Skip to content

Commit 869dd8c

Browse files
committed
Better log & settingObj in function
1 parent 5371df7 commit 869dd8c

File tree

4 files changed

+163
-37
lines changed

4 files changed

+163
-37
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"dependencies": {
3232
"boxen": "^4.1.0",
3333
"colors": "^1.4.0",
34-
"fs-extra": "^8.1.0"
34+
"fs-extra": "^8.1.0",
35+
"log-update": "^4.0.0"
3536
},
3637
"devDependencies": {
3738
"@types/fs-extra": "^9.0.1",

src/index.ts

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
SettingsFileIOSParamsInterface,
2020
SettingsFileIOSInterface,
2121
SettingsFileAndroidInterface,
22+
SettingsFileInterface,
2223
} from "./types";
2324

2425
// Builds for android :
@@ -34,7 +35,7 @@ function buildAndroid(
3435
const OS = osDetection();
3536

3637
if (!newValues) {
37-
console.log(logLine);
38+
logLine();
3839
return resolve();
3940
}
4041

@@ -49,8 +50,11 @@ function buildAndroid(
4950
);
5051
}
5152

52-
console.log(logLine);
53-
beautyLog("BUILDING " + newValues.buildName + "...", "info");
53+
logLine();
54+
beautyLog("building " + newValues.buildName, "info", {
55+
boldedTxt: newValues.buildName,
56+
loadingLog: true,
57+
});
5458

5559
// Set variable :
5660
try {
@@ -75,7 +79,9 @@ function buildAndroid(
7579
}
7680

7781
if (stdout.includes("BUILD SUCCESSFUL")) {
78-
beautyLog(newValues.buildName + " FINISHED", "success");
82+
beautyLog(newValues.buildName + " finished", "success", {
83+
boldedTxt: newValues.buildName,
84+
});
7985

8086
const newPath = path.join(
8187
".",
@@ -123,7 +129,7 @@ function buildIOS(
123129
const iosBuildPath = `/ios/${ARCHIVE_NAME}.xcarchive`;
124130
const { value: newValues, done } = iosValueGen.next();
125131
if (!newValues) {
126-
console.log(logLine);
132+
logLine();
127133
return resolve();
128134
}
129135

@@ -138,8 +144,11 @@ function buildIOS(
138144
);
139145
}
140146

141-
console.log(logLine);
142-
beautyLog("BUILDING " + newValues.buildName + "...", "info");
147+
logLine();
148+
beautyLog("building " + newValues.buildName, "info", {
149+
boldedTxt: newValues.buildName,
150+
loadingLog: true,
151+
});
143152

144153
// Set variable :
145154
try {
@@ -161,7 +170,9 @@ function buildIOS(
161170
}
162171

163172
if (stdout.includes("ARCHIVE SUCCEEDED")) {
164-
beautyLog(newValues.buildName + " FINISHED", "success");
173+
beautyLog(newValues.buildName + " finished", "success", {
174+
boldedTxt: newValues.buildName,
175+
});
165176
const newPath = path.join(
166177
".",
167178
`/builds/ios/${newValues.buildName}.xcarchive`
@@ -191,7 +202,7 @@ function buildIOS(
191202
// Main :
192203
export default function main(
193204
platform: PlatformInterface,
194-
settingFilePath: string
205+
settingFilePath: string | SettingsFileInterface
195206
): Promise<any> {
196207
packageInfoLog();
197208

@@ -200,11 +211,15 @@ export default function main(
200211
beautyErrorLog("invalid setting file address!");
201212
reject(new Error("invalid setting file address!"));
202213
}
203-
const parsedSettingFile = initializeSettingFile(
204-
platform,
205-
settingFilePath,
206-
reject
207-
)!;
214+
215+
let parsedSettingFile: SettingsFileInterface;
216+
if (typeof settingFilePath === "string")
217+
parsedSettingFile = initializeSettingFile(
218+
platform,
219+
settingFilePath,
220+
reject
221+
)!;
222+
else parsedSettingFile = settingFilePath;
208223

209224
const [androidValueGen, iosValueGen] = settingFileParameters(
210225
platform,
@@ -214,7 +229,9 @@ export default function main(
214229

215230
switch (platform) {
216231
case "android":
217-
fs.mkdirSync(buildPathResolver("android"), { recursive: true });
232+
fs.mkdirSync(buildPathResolver("android"), {
233+
recursive: true,
234+
});
218235
buildAndroid(
219236
androidValueGen!,
220237
buildObjectResolver(parsedSettingFile, "android"),
@@ -227,7 +244,9 @@ export default function main(
227244
beautyErrorLog("ios need mac operating system!");
228245
return reject(new Error("ios need mac operating system!"));
229246
}
230-
fs.mkdirSync(buildPathResolver("ios"), { recursive: true });
247+
fs.mkdirSync(buildPathResolver("ios"), {
248+
recursive: true,
249+
});
231250
buildIOS(
232251
iosValueGen!,
233252
buildObjectResolver(
@@ -239,12 +258,16 @@ export default function main(
239258
);
240259
break;
241260
case "both":
242-
fs.mkdirSync(buildPathResolver("android"), { recursive: true });
261+
fs.mkdirSync(buildPathResolver("android"), {
262+
recursive: true,
263+
});
243264
buildAndroid(
244265
androidValueGen!,
245266
buildObjectResolver(parsedSettingFile, "android"),
246267
() => {
247-
fs.mkdirSync(buildPathResolver("ios"), { recursive: true });
268+
fs.mkdirSync(buildPathResolver("ios"), {
269+
recursive: true,
270+
});
248271
buildIOS(
249272
iosValueGen!,
250273
buildObjectResolver(

src/logs.ts

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
import colors from "colors";
22
import boxen from "boxen";
3+
import logUpdate from "log-update";
34
import {
45
name as packageName,
56
version as packageVersion,
67
description as packageDescription,
78
} from "../package.json";
89

9-
export const logLine = colors.gray("\n---------------------------------");
10+
let loadingLogInterval: NodeJS.Timeout;
11+
let loadingLogTxt: string | boolean;
12+
13+
function loadingLog(txt: string): void {
14+
loadingLogTxt = txt;
15+
const frames = ["", ".", "..", "..."];
16+
let i = 0;
17+
18+
loadingLogInterval = setInterval(() => {
19+
const frame = frames[(i = ++i % frames.length)];
20+
logUpdate(txt + `${frame}`);
21+
}, 1000);
22+
}
23+
24+
function clearLoadingLog(): void {
25+
clearInterval(loadingLogInterval);
26+
logUpdate.clear();
27+
logUpdate.done();
28+
}
29+
30+
export const logLine = (): void =>
31+
log(colors.gray("\n---------------------------------"));
1032

1133
// Package info log :
1234
export function packageInfoLog(): void {
13-
console.log(
35+
log(
1436
"\n" +
1537
boxen(
1638
packageName + " v" + packageVersion + "\n\n " + packageDescription,
@@ -19,23 +41,41 @@ export function packageInfoLog(): void {
1941
);
2042
}
2143

22-
export function beautyLog(message: string, type = "warn"): void {
44+
export interface BeautyLogOptionsInterface {
45+
boldedTxt?: string;
46+
loadingLog?: boolean;
47+
}
48+
49+
export function beautyLog(
50+
message: string,
51+
type = "warn",
52+
options = {
53+
boldedTxt: "",
54+
loadingLog: false,
55+
} as BeautyLogOptionsInterface
56+
): void {
57+
message = message.replace(
58+
options.boldedTxt!,
59+
colors.bold(options.boldedTxt!)
60+
);
61+
let logTxt = "";
2362
switch (type) {
2463
case "warn":
25-
console.log("\n " + colors.bold(colors.yellow("warn")) + " " + message);
64+
logTxt = "\n " + colors.bold(colors.yellow("warn")) + " " + message;
2665
break;
2766
case "info":
28-
console.log("\n " + colors.bold(colors.cyan("info")) + " " + message);
67+
logTxt = "\n " + colors.bold(colors.cyan("info")) + " " + message;
2968
break;
3069
case "success":
31-
console.log(
32-
"\n " + colors.bold(colors.green("success")) + " " + message
33-
);
70+
logTxt = "\n " + colors.bold(colors.green("success")) + " " + message;
3471
break;
3572
default:
36-
console.log("\n " + message);
73+
logTxt = "\n " + message;
3774
break;
3875
}
76+
77+
if (options.loadingLog) loadingLog(logTxt);
78+
else log(logTxt);
3979
}
4080

4181
// read error better :
@@ -46,11 +86,23 @@ export function beautyErrorLog(err: string | Error): void {
4686
} else {
4787
errMessage = err;
4888
}
49-
console.log(
89+
log(
5090
"\n " +
5191
colors.bold(colors.red("error")) +
5292
" " +
5393
errMessage.replace(/\n/g, "\n ") +
5494
"\n"
5595
);
5696
}
97+
98+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
99+
export function log(param?: any): void {
100+
clearLoadingLog();
101+
102+
if (loadingLogTxt) {
103+
console.log(loadingLogTxt);
104+
loadingLogTxt = false;
105+
}
106+
107+
console.log(param);
108+
}

0 commit comments

Comments
 (0)