Skip to content

Commit b96e4b3

Browse files
authored
NEW @W-17701084@ CLI update and logfile adjustment (#1746)
1 parent c88ea0f commit b96e4b3

File tree

5 files changed

+260
-56
lines changed

5 files changed

+260
-56
lines changed

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
"bugs": "https://github.com/forcedotcom/sfdx-scanner/issues",
77
"dependencies": {
88
"@oclif/core": "3.27.0",
9-
"@salesforce/code-analyzer-core": "0.22.0",
10-
"@salesforce/code-analyzer-engine-api": "0.17.0",
11-
"@salesforce/code-analyzer-eslint-engine": "0.19.0",
12-
"@salesforce/code-analyzer-flowtest-engine": "0.17.0",
13-
"@salesforce/code-analyzer-pmd-engine": "0.19.0",
14-
"@salesforce/code-analyzer-regex-engine": "0.17.0",
15-
"@salesforce/code-analyzer-retirejs-engine": "0.17.0",
9+
"@salesforce/code-analyzer-core": "0.23.0",
10+
"@salesforce/code-analyzer-engine-api": "0.18.0",
11+
"@salesforce/code-analyzer-eslint-engine": "0.20.0",
12+
"@salesforce/code-analyzer-flowtest-engine": "0.18.0",
13+
"@salesforce/code-analyzer-pmd-engine": "0.20.0",
14+
"@salesforce/code-analyzer-regex-engine": "0.18.0",
15+
"@salesforce/code-analyzer-retirejs-engine": "0.18.0",
1616
"@salesforce/core": "6.7.6",
1717
"@salesforce/sf-plugins-core": "5.0.13",
1818
"@salesforce/ts-types": "^2.0.12",

src/lib/utils/DateTimeUtils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export interface Clock {
2+
now(): Date;
3+
}
4+
5+
export class RealClock implements Clock {
6+
public now(): Date {
7+
return new Date();
8+
}
9+
}
10+
11+
export function formatToDateTimeString(dateTime: Date): string {
12+
const year: number = dateTime.getFullYear();
13+
const month: string = String(dateTime.getMonth() + 1).padStart(2, '0'); // Months are 0-indexed
14+
const day: string = String(dateTime.getDate()).padStart(2, '0');
15+
const hours: string = String(dateTime.getHours()).padStart(2, '0');
16+
const minutes: string = String(dateTime.getMinutes()).padStart(2, '0');
17+
const seconds: string = String(dateTime.getSeconds()).padStart(2, '0');
18+
const milliseconds: string = String(dateTime.getMilliseconds()).padStart(3, '0');
19+
return `${year}_${month}_${day}_${hours}_${minutes}_${seconds}_${milliseconds}`;
20+
}

src/lib/writers/LogWriter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from 'node:path';
22
import * as fs from 'node:fs/promises';
33
import {CodeAnalyzerConfig} from '@salesforce/code-analyzer-core';
4+
import {Clock, RealClock, formatToDateTimeString} from '../utils/DateTimeUtils';
45

56
export interface LogWriter {
67
writeToLog(message: string): void;
@@ -29,11 +30,11 @@ export class LogFileWriter implements LogWriter {
2930
this.writeStream.end();
3031
}
3132

32-
public static async fromConfig(config: CodeAnalyzerConfig): Promise<LogFileWriter> {
33+
public static async fromConfig(config: CodeAnalyzerConfig, clock: Clock = new RealClock()): Promise<LogFileWriter> {
3334
const logFolder = config.getLogFolder();
3435
// Use the current timestamp to make sure each transaction has a unique logfile. If we want to reuse logfiles,
3536
// or just have one running logfile, we can change this.
36-
const logFile = path.join(logFolder, `sfca-${Date.now()}.log`);
37+
const logFile = path.join(logFolder, `sfca-${formatToDateTimeString(clock.now())}.log`);
3738
// 'w' flag causes the file to be created if it doesn't already exist.
3839
const fh = await fs.open(logFile, 'w');
3940
return new LogFileWriter(fh.createWriteStream({}), logFile);

test/lib/writers/LogWriter.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as fs from 'node:fs/promises';
33
import * as tmp from 'tmp';
44
import {CodeAnalyzerConfig} from '@salesforce/code-analyzer-core';
55
import {LogFileWriter} from '../../../src/lib/writers/LogWriter';
6+
import {Clock} from '../../../src/lib/utils/DateTimeUtils';
67

78
describe('LogWriter implementations', () => {
89

@@ -30,10 +31,11 @@ describe('LogWriter implementations', () => {
3031
jest.restoreAllMocks();
3132
})
3233

33-
it('Writes to file specified by config', async () => {
34+
it('Writes properly-named file to config-specified folder', async () => {
3435
// ==== TEST SETUP ====
3536
const config = CodeAnalyzerConfig.withDefaults();
36-
const logWriter = await LogFileWriter.fromConfig(config);
37+
const fixedDate: Date = new Date(2025, 1, 20, 14, 30, 18, 14);
38+
const logWriter = await LogFileWriter.fromConfig(config, new FixedClock(fixedDate));
3739

3840
// ==== TESTED BEHAVIOR ====
3941
logWriter.writeToLog('beep');
@@ -44,10 +46,23 @@ describe('LogWriter implementations', () => {
4446
const logFolderContents = await fs.readdir(tmpLogFolder);
4547
expect(logFolderContents).toHaveLength(1);
4648
const logFilePath = path.join(tmpLogFolder, logFolderContents[0]);
49+
expect(path.basename(logFilePath)).toEqual('sfca-2025_02_20_14_30_18_014.log');
4750
const logFileContents = await fs.readFile(logFilePath, {encoding: 'utf-8'});
4851
expect(logFileContents).toContain('beep');
4952
expect(logFileContents).toContain('boop');
5053
expect(logFileContents).toContain('bop');
5154
});
5255
});
5356
});
57+
58+
class FixedClock implements Clock {
59+
private readonly fixedDate: Date;
60+
61+
public constructor(fixedDate: Date) {
62+
this.fixedDate = fixedDate;
63+
}
64+
65+
public now(): Date {
66+
return this.fixedDate;
67+
}
68+
}

0 commit comments

Comments
 (0)