-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a first inventory snapshot feature (for CI/CD)
- Loading branch information
Showing
4 changed files
with
195 additions
and
3 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
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,141 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import dotenv from 'dotenv'; | ||
import recursive from 'recursive-readdir'; | ||
import { isEqual, last, reject } from 'lodash'; | ||
import { | ||
assertUniqueness, | ||
getMetaSectionsFromTitle, | ||
getSongInSectionTuples, | ||
getTitleWithoutMeta, | ||
getTodayAsDateString, | ||
logFileWithLinkInConsole, | ||
logProcessingFile, | ||
} from './core'; | ||
import assert from 'node:assert'; | ||
import { | ||
SongInventoryChange, | ||
SongMeta, | ||
SongsInventory, | ||
SongsInventoryEntry, | ||
} from './types'; | ||
import { TXT_EXTENSION } from './constants'; | ||
|
||
dotenv.config(); | ||
|
||
const inventoryChangelogFilename = path.join( | ||
__dirname, | ||
'..', | ||
'inventoryChangelog.json', | ||
); | ||
|
||
const snapshotInventory = async (dir: string) => { | ||
console.log(`"Reprocessing file names from ${dir} directory.."`); | ||
|
||
const currentInventory = JSON.parse( | ||
fs.readFileSync(inventoryChangelogFilename).toString(), | ||
) as SongsInventory; | ||
|
||
const { songs } = currentInventory; | ||
|
||
const songsAsHashMap = songs.reduce( | ||
(accumulator, entry) => ({ | ||
...accumulator, | ||
[entry.id]: entry, | ||
}), | ||
{} as { | ||
[id: string]: SongsInventoryEntry; | ||
}, | ||
); | ||
|
||
const songsMeta = (await recursive(dir)) | ||
.filter((filePath) => path.extname(filePath) === TXT_EXTENSION) | ||
.map((filePath) => { | ||
const filename = path.basename(filePath); | ||
const fileContent = fs.readFileSync(filePath).toString(); | ||
|
||
logProcessingFile(filename, 'inventory generation'); | ||
logFileWithLinkInConsole(filePath); | ||
|
||
const rawTitle = getSongInSectionTuples(fileContent)[1]; | ||
|
||
assert.ok( | ||
rawTitle.includes(SongMeta.CONTENT_HASH), | ||
`The ${SongMeta.CONTENT_HASH} should be defined.`, | ||
); | ||
|
||
return { | ||
filename, | ||
metaSections: getMetaSectionsFromTitle(rawTitle), | ||
titleWithoutMetaSections: getTitleWithoutMeta(rawTitle), | ||
}; | ||
}); | ||
|
||
assertUniqueness( | ||
songsMeta.map(({ metaSections }) => metaSections[SongMeta.ID]), | ||
); | ||
|
||
const dateAsString = getTodayAsDateString(); | ||
const updatedSongsInventory = songsMeta.map(({ filename, metaSections }) => { | ||
const id = metaSections[SongMeta.ID]; | ||
const contentHash = metaSections[SongMeta.CONTENT_HASH]; | ||
const currentInventory = songsAsHashMap[id]; | ||
|
||
if (!currentInventory) { | ||
return { | ||
id, | ||
filename, | ||
changes: [ | ||
{ | ||
date: dateAsString, | ||
contentHash, | ||
}, | ||
], | ||
} as SongsInventoryEntry; | ||
} | ||
|
||
const { changes } = currentInventory; | ||
const { | ||
filename: lastChangeFilename, | ||
contentHash: lastChangedContentHash, | ||
date: lastChangedDate, | ||
} = last(changes) as SongInventoryChange; | ||
|
||
if (isEqual(lastChangedContentHash, contentHash)) { | ||
return currentInventory; | ||
} | ||
|
||
const isFilenameChanged = !isEqual(lastChangeFilename, filename); | ||
return { | ||
id, | ||
filename, | ||
changes: [ | ||
...(isEqual(lastChangedDate, dateAsString) | ||
? reject(changes, { date: lastChangedDate }) | ||
: changes), | ||
{ | ||
date: dateAsString, | ||
contentHash, | ||
...(isFilenameChanged ? { filename, isFilenameChanged } : {}), | ||
}, | ||
], | ||
} as SongsInventoryEntry; | ||
}); | ||
|
||
assertUniqueness(Object.keys(updatedSongsInventory).map((id) => id)); | ||
|
||
const nextSongs = updatedSongsInventory.sort((a, b) => | ||
a.filename.localeCompare(b.filename), | ||
); | ||
fs.writeFileSync( | ||
inventoryChangelogFilename, | ||
JSON.stringify( | ||
isEqual(songs, nextSongs) | ||
? currentInventory | ||
: { | ||
updatedOn: dateAsString, | ||
songs: nextSongs, | ||
}, | ||
), | ||
); | ||
}; |
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