forked from serverless/serverless-python-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipenv.js
More file actions
65 lines (58 loc) 路 1.45 KB
/
Copy pathpipenv.js
File metadata and controls
65 lines (58 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
57
58
59
60
61
62
63
64
65
const fse = require('fs-extra');
const path = require('path');
const { spawnSync } = require('child_process');
const { EOL } = require('os');
/**
* pipenv install
*/
function pipfileToRequirements(
modulePath,
outputRequirements,
serverless,
options
) {
const pipenvPath = path.join(modulePath, 'Pipfile');
// Stop if Pipfile file does not exist
if (!options.usePipenv || !fse.existsSync(pipenvPath)) {
return;
}
serverless.cli.log('Generating requirements.txt from Pipfile...');
const res = spawnSync(
'pipenv',
['lock', '--requirements', '--keep-outdated'],
{
cwd: modulePath
}
);
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.writeFileSync(
outputRequirements,
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 };