- Bump Node.js to version 18. This change is necessary as Node.js v16 will reach end-of-life on 2023-09-11. It also ensure compatibility with dependencies requiring minimum of Node.js v18, such as `vite`, `@vitejs`plugin-legacy` and `icon-gen`. - Bump `setup-node` action to v4. - Recommend using the `nvm` tool for managing Node.js versions in the documentation. - Update documentation to point to code reference for required Node.js version. This removes duplication of information, and keeps the code as single source of truth for required Node.js version. - Refactor code to adopt the `node:` protocol for Node API imports as per Node.js 18 standards. This change addresses ambiguities and aligns with Node.js best practices (nodejs/node#38343). Currently, there is no ESLint rule to enforce this protocol, as noted in import-js/eslint-plugin-import#2717. - Replace `cross-fetch` dependency with the native Node.js fetch API introduced in Node.js 18. Adjust type casting for async iterable read streams to align with the latest Node.js APIs, based on discussions in DefinitelyTyped/DefinitelyTyped#65542.
92 lines
2.9 KiB
TypeScript
92 lines
2.9 KiB
TypeScript
import { unlink, readFile } from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { log, die, LogLevel } from '../utils/log';
|
|
import { exists } from '../utils/io';
|
|
import { SupportedPlatform, CURRENT_PLATFORM } from '../utils/platform';
|
|
import { getAppName } from '../utils/npm';
|
|
|
|
export async function clearAppLogFiles(
|
|
projectDir: string,
|
|
): Promise<void> {
|
|
if (!projectDir) { throw new Error('missing project directory'); }
|
|
const logPath = await determineLogPath(projectDir);
|
|
if (!logPath || !await exists(logPath)) {
|
|
log(`Skipping clearing logs, log file does not exist: ${logPath}.`);
|
|
return;
|
|
}
|
|
try {
|
|
await unlink(logPath);
|
|
log(`Successfully cleared the log file at: ${logPath}.`);
|
|
} catch (error) {
|
|
die(`Failed to clear the log file at: ${logPath}. Reason: ${error}`);
|
|
}
|
|
}
|
|
|
|
export async function readAppLogFile(
|
|
projectDir: string,
|
|
): Promise<AppLogFileResult> {
|
|
if (!projectDir) { throw new Error('missing project directory'); }
|
|
const logPath = await determineLogPath(projectDir);
|
|
if (!logPath || !await exists(logPath)) {
|
|
log(`No log file at: ${logPath}`, LogLevel.Warn);
|
|
return {
|
|
logFilePath: logPath,
|
|
};
|
|
}
|
|
const logContent = await readLogFile(logPath);
|
|
return {
|
|
logFileContent: logContent,
|
|
logFilePath: logPath,
|
|
};
|
|
}
|
|
|
|
interface AppLogFileResult {
|
|
readonly logFilePath: string;
|
|
readonly logFileContent?: string;
|
|
}
|
|
|
|
async function determineLogPath(
|
|
projectDir: string,
|
|
): Promise<string> {
|
|
if (!projectDir) { throw new Error('missing project directory'); }
|
|
const logFileName = 'main.log';
|
|
const appName = await getAppName(projectDir);
|
|
if (!appName) {
|
|
return die('App name not found.');
|
|
}
|
|
const logFilePaths: {
|
|
readonly [K in SupportedPlatform]: () => string;
|
|
} = {
|
|
[SupportedPlatform.macOS]: () => {
|
|
if (!process.env.HOME) {
|
|
throw new Error('HOME environment variable is not defined');
|
|
}
|
|
return join(process.env.HOME, 'Library', 'Logs', appName, logFileName);
|
|
},
|
|
[SupportedPlatform.Linux]: () => {
|
|
if (!process.env.HOME) {
|
|
throw new Error('HOME environment variable is not defined');
|
|
}
|
|
return join(process.env.HOME, '.config', appName, 'logs', logFileName);
|
|
},
|
|
[SupportedPlatform.Windows]: () => {
|
|
if (!process.env.USERPROFILE) {
|
|
throw new Error('USERPROFILE environment variable is not defined');
|
|
}
|
|
return join(process.env.USERPROFILE, 'AppData', 'Roaming', appName, 'logs', logFileName);
|
|
},
|
|
};
|
|
const logFilePath = logFilePaths[CURRENT_PLATFORM]?.();
|
|
if (!logFilePath) {
|
|
log(`Cannot determine log path, unsupported OS: ${SupportedPlatform[CURRENT_PLATFORM]}`, LogLevel.Warn);
|
|
}
|
|
return logFilePath;
|
|
}
|
|
|
|
async function readLogFile(
|
|
logFilePath: string,
|
|
): Promise<string | undefined> {
|
|
const content = await readFile(logFilePath, 'utf-8');
|
|
return content?.trim().length > 0 ? content : undefined;
|
|
}
|