forked from serverless/serverless-python-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipenv.js
60 lines (55 loc) · 1.45 KB
/
pipenv.js
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
const fse = require('fs-extra');
const path = require('path');
const { spawnSync } = require('child_process');
const { EOL } = require('os');
/**
* pipenv install
*/
function pipfileToRequirements() {
if (
!this.options.usePipenv ||
!fse.existsSync(path.join(this.servicePath, 'Pipfile'))
) {
return;
}
this.serverless.cli.log('Generating requirements.txt from Pipfile...');
const res = spawnSync(
'pipenv',
['lock', '--requirements', '--keep-outdated'],
{
cwd: this.servicePath
}
);
if (res.error) {
if (res.error.code === 'ENOENT') {
throw new Error(
`pipenv not found! Install it with 'pip install pipenv'.`
);
}
throw new Error(res.error);
}
if (res.status !== 0) {
throw new Error(res.stderr);
}
fse.ensureDirSync(path.join(this.servicePath, '.serverless'));
fse.writeFileSync(
path.join(this.servicePath, '.serverless/requirements.txt'),
removeEditableFlagFromRequirementsString(res.stdout)
);
}
/**
*
* @param requirementBuffer
* @returns Buffer with editable flags remove
*/
function removeEditableFlagFromRequirementsString(requirementBuffer) {
const flagStr = '-e ';
const lines = requirementBuffer.toString('utf8').split(EOL);
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith(flagStr)) {
lines[i] = lines[i].substring(flagStr.length);
}
}
return Buffer.from(lines.join(EOL));
}
module.exports = { pipfileToRequirements };