-
Notifications
You must be signed in to change notification settings - Fork 4k
/
packaging.ts
75 lines (66 loc) · 2.22 KB
/
packaging.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import * as fs from 'fs';
import * as path from 'path';
export enum DependenciesFile {
PIP = 'requirements.txt',
POETRY = 'poetry.lock',
PIPENV = 'Pipfile.lock',
NONE = ''
}
export interface PackagingProps {
/**
* Dependency file for the type of packaging.
*/
readonly dependenciesFile: DependenciesFile;
/**
* Command to export the dependencies into a pip-compatible `requirements.txt` format.
*
* @default - No dependencies are exported.
*/
readonly exportCommand?: string;
}
export class Packaging {
/**
* Standard packaging with `pip`.
*/
public static readonly PIP = new Packaging({
dependenciesFile: DependenciesFile.PIP,
});
/**
* Packaging with `pipenv`.
*/
public static readonly PIPENV = new Packaging({
dependenciesFile: DependenciesFile.PIPENV,
// By default, pipenv creates a virtualenv in `/.local`, so we force it to create one in the package directory.
// At the end, we remove the virtualenv to avoid creating a duplicate copy in the Lambda package.
exportCommand: `PIPENV_VENV_IN_PROJECT=1 pipenv lock -r > ${DependenciesFile.PIP} && rm -rf .venv`,
});
/**
* Packaging with `poetry`.
*/
public static readonly POETRY = new Packaging({
dependenciesFile: DependenciesFile.POETRY,
// Export dependencies with credentials avaiable in the bundling image.
exportCommand: `poetry export --with-credentials --format ${DependenciesFile.PIP} --output ${DependenciesFile.PIP}`,
});
/**
* No dependencies or packaging.
*/
public static readonly NONE = new Packaging({ dependenciesFile: DependenciesFile.NONE });
public static fromEntry(entry: string): Packaging {
if (fs.existsSync(path.join(entry, DependenciesFile.PIPENV))) {
return Packaging.PIPENV;
} if (fs.existsSync(path.join(entry, DependenciesFile.POETRY))) {
return Packaging.POETRY;
} else if (fs.existsSync(path.join(entry, DependenciesFile.PIP))) {
return Packaging.PIP;
} else {
return Packaging.NONE;
}
}
public readonly dependenciesFile: string;
public readonly exportCommand?: string;
constructor(props: PackagingProps) {
this.dependenciesFile = props.dependenciesFile;
this.exportCommand = props.exportCommand;
}
}