|
1 | 1 | const fs = require('fs').promises; |
2 | 2 | const path = require('path'); |
3 | 3 |
|
4 | | -const SNAPSHOTS_DIR = '.licensepulse/snapshots'; |
| 4 | +function getSnapshotsDir() { |
| 5 | + return process.env.LICENSEPULSE_SNAPSHOTS_DIR || '.licensepulse/snapshots'; |
| 6 | +} |
5 | 7 |
|
6 | 8 | function getSnapshotFilename(repoId) { |
7 | 9 | return repoId.replace('/', '-') + '.json'; |
8 | 10 | } |
9 | 11 |
|
10 | 12 | function getSnapshotPath(repoId) { |
11 | | - return path.join(SNAPSHOTS_DIR, getSnapshotFilename(repoId)); |
| 13 | + return path.join(getSnapshotsDir(), getSnapshotFilename(repoId)); |
12 | 14 | } |
13 | 15 |
|
14 | 16 | async function ensureSnapshotsDir() { |
15 | 17 | try { |
16 | 18 | const path = require('path'); |
17 | | - const baseDir = path.dirname(SNAPSHOTS_DIR); |
| 19 | + const dir = getSnapshotsDir(); |
| 20 | + const baseDir = path.dirname(dir); |
18 | 21 | await fs.mkdir(baseDir, { recursive: true }); |
19 | | - await fs.mkdir(SNAPSHOTS_DIR, { recursive: true }); |
| 22 | + await fs.mkdir(dir, { recursive: true }); |
20 | 23 | } catch (error) { |
21 | 24 | // Ignore errors if directory already exists |
22 | 25 | } |
@@ -57,12 +60,13 @@ async function saveSnapshot(repoId, licenseData) { |
57 | 60 | async function getAllSnapshots() { |
58 | 61 | try { |
59 | 62 | await ensureSnapshotsDir(); |
60 | | - const files = await fs.readdir(SNAPSHOTS_DIR); |
| 63 | + const dir = getSnapshotsDir(); |
| 64 | + const files = await fs.readdir(dir); |
61 | 65 |
|
62 | 66 | const snapshots = []; |
63 | 67 | for (const file of files) { |
64 | 68 | if (file.endsWith('.json')) { |
65 | | - const filePath = path.join(SNAPSHOTS_DIR, file); |
| 69 | + const filePath = path.join(dir, file); |
66 | 70 | const data = await fs.readFile(filePath, 'utf8'); |
67 | 71 | snapshots.push(JSON.parse(data)); |
68 | 72 | } |
|
0 commit comments