-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from meza/lockfile
chore: added lockfile #17
- Loading branch information
Showing
4 changed files
with
70 additions
and
11 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
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,41 @@ | ||
import { afterAll, describe, it, vi, expect } from 'vitest'; | ||
import fs from 'fs/promises'; | ||
import { newNumber } from './numbering'; | ||
|
||
type ReaddirMock = () => Promise<String[]> | ||
|
||
vi.mock('fs/promises'); | ||
vi.mock('./config', () => ({ | ||
getDir: vi.fn().mockResolvedValue('/') | ||
})); | ||
|
||
describe('The numbering logic', () => { | ||
|
||
afterAll(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
it('can read from the sequence file', async () => { | ||
const random = Math.floor(Math.random() * 100); | ||
vi.mocked(fs.readFile).mockResolvedValueOnce(random.toString()); | ||
const num = await newNumber(); | ||
expect(num).toEqual(random + 1); | ||
}); | ||
|
||
it('returns 1 if there are no files', async () => { | ||
vi.mocked(fs.readFile).mockRejectedValueOnce('no sequence file'); | ||
vi.mocked(fs.readdir).mockResolvedValueOnce([]); | ||
const num = await newNumber(); | ||
expect(num).toEqual(1); | ||
}); | ||
|
||
it('processes existing files if there is no lockfile', async () => { | ||
const fakeFiles: string[] = [ | ||
'0001-first-file.md', | ||
'0002-first-file.md' | ||
]; | ||
vi.mocked(fs.readFile).mockRejectedValueOnce('no sequence file'); | ||
vi.mocked(fs.readdir as unknown as ReaddirMock).mockResolvedValueOnce(fakeFiles); | ||
const num = await newNumber(); | ||
expect(num).toEqual(3); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,17 +1,26 @@ | ||
import fs from 'fs/promises'; | ||
import { getDir } from './config'; | ||
import * as path from 'path'; | ||
|
||
export const newNumber = async () => { | ||
const filePattern = /^0*(\d+)-.*$/; | ||
const files = await fs.readdir(await getDir()); | ||
const numbers = files.map(f => { | ||
const adrFile = f.match(filePattern); | ||
if (adrFile) { | ||
return parseInt(adrFile[1], 10); | ||
} | ||
return 0; | ||
}); | ||
try { | ||
const lockfile = await fs.readFile(path.resolve(await getDir(), '.adr-sequence.lock')); | ||
return parseInt(lockfile.toString().trim(), 10) + 1; | ||
} catch (e) { | ||
// This is for backward compatibility. If someone upgrades from an older tool without a lockfile, | ||
// we create one. | ||
const filePattern = /^0*(\d+)-.*$/; | ||
const files = await fs.readdir(await getDir()); | ||
const numbers = files.map(f => { | ||
const adrFile = f.match(filePattern); | ||
if (adrFile) { | ||
return parseInt(adrFile[1], 10); | ||
} | ||
return 0; | ||
}); | ||
|
||
const largestNumber = numbers.reduce((a, b) => Math.max(a, b), 0); | ||
return largestNumber + 1; | ||
} | ||
|
||
const largestNumber = numbers.reduce((a, b) => Math.max(a, b), 0); | ||
return largestNumber + 1; | ||
}; |
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