-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: normalize filepath and ensure path exists for stats writing (#750)
* fix: normalize filepath for stats writing * refactor: move error handling to bundle command itself * test: setupStatsWriter tests * fix: ensure path exists * chore: add changeset
- Loading branch information
Showing
5 changed files
with
113 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@callstack/repack': minor | ||
--- | ||
|
||
Normalize filepath & ensure path exists when writing stats to a file |
61 changes: 61 additions & 0 deletions
61
packages/repack/src/commands/common/__tests__/setupStatsWriter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import path from 'node:path'; | ||
import { fs } from 'memfs'; | ||
import { normalizeStatsOptions, writeStats } from '../setupStatsWriter'; | ||
import { Logger } from '../../../types'; | ||
|
||
jest.mock('node:fs', () => jest.requireActual('memfs').fs); | ||
|
||
describe('setupStatsWriter', () => { | ||
const logger = { info: jest.fn() } as unknown as Logger; | ||
|
||
describe('normalizeStatsOptions', () => { | ||
it('should return options with preset if preset is provided', () => { | ||
const options = {}; | ||
const preset = 'detailed'; | ||
const result = normalizeStatsOptions(options, preset); | ||
expect(result).toHaveProperty('preset', 'detailed'); | ||
}); | ||
|
||
it('should return options with preset "normal" if options is true', () => { | ||
const options = true; | ||
const result = normalizeStatsOptions(options); | ||
expect(result).toHaveProperty('preset', 'normal'); | ||
}); | ||
|
||
it('should return options with preset "none" if options is false', () => { | ||
const options = false; | ||
const result = normalizeStatsOptions(options); | ||
expect(result).toHaveProperty('preset', 'none'); | ||
}); | ||
|
||
it('should return options as is if no preset is provided', () => { | ||
const options = { custom: 'value' }; | ||
const result = normalizeStatsOptions(options); | ||
expect(result).toEqual(options); | ||
}); | ||
}); | ||
|
||
describe('writeStats', () => { | ||
it('should write stats to the specified file', async () => { | ||
const stats = { key: 'value' }; | ||
const filepath = 'stats.json'; | ||
|
||
await writeStats(stats, { filepath, logger, rootDir: '/' }); | ||
|
||
const absoluteFilepath = path.resolve('/', filepath); | ||
const fileContent = fs.readFileSync(absoluteFilepath, 'utf-8') as string; | ||
expect(JSON.parse(fileContent)).toEqual(stats); | ||
}); | ||
|
||
it('should ensure path exists', async () => { | ||
const stats = { key: 'value' }; | ||
const filepath = './my/custom/dir/stats.json'; | ||
|
||
await writeStats(stats, { filepath, logger, rootDir: '/' }); | ||
|
||
const absoluteFilepath = path.resolve('/', filepath); | ||
const fileContent = fs.readFileSync(absoluteFilepath, 'utf-8') as string; | ||
expect(JSON.parse(fileContent)).toEqual(stats); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters