Repository navigation
Expand file tree
/
Copy pathpaths.ts
More file actions
56 lines (51 loc) · 1.45 KB
/
Copy pathpaths.ts
File metadata and controls
56 lines (51 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import * as constants from './constants'
import * as fs from 'mz/fs'
import * as path from 'path'
/**
* Construct the path of the SQLite database file for the given dump.
*
* @param storageRoot The path where SQLite databases are stored.
* @param id The ID of the dump.
*/
export function dbFilename(storageRoot: string, id: number): string {
return path.join(storageRoot, constants.DBS_DIR, `${id}.lsif.db`)
}
/**
* Construct the path of the raw upload file for the given identifier.
*
* @param storageRoot The path where uploads are stored.
* @param id The identifier of the upload.
*/
export function uploadFilename(storageRoot: string, id: number): string {
return path.join(storageRoot, constants.UPLOADS_DIR, `${id}.lsif.gz`)
}
/**
* Returns the identifier of the database file. Handles both of the
* following formats:
*
* - `{id}.lsif.db`
* - `{id}-{repo}-{commit}.lsif.db`
*
* @param filename The filename.
*/
export function idFromFilename(filename: string): number | undefined {
const id = parseInt(path.parse(filename).name.split('-')[0], 10)
if (!isNaN(id)) {
return id
}
return undefined
}
/**
* Ensure the directory exists.
*
* @param directoryPath The directory path.
*/
export async function ensureDirectory(directoryPath: string): Promise<void> {
try {
await fs.mkdir(directoryPath)
} catch (error) {
if (!(error && error.code === 'EEXIST')) {
throw error
}
}
}