/
grab-project.js
96 lines (85 loc) · 2.49 KB
/
grab-project.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
const path = require('path');
const createOptions = require('./create-options');
const gitClone = require('./git-clone');
const spawn = require('./spawn');
async function grabProject(context) {
if (context.module.useGitClone) {
return gitClone(context);
}
return new Promise((resolve, reject) => {
const packageManager =
context.options.yarn || context.module.useYarn ? 'yarn' : 'npm';
let packageName = context.module.raw;
if (context.module.type === 'directory') {
context.module.raw = context.module.name = path.basename(packageName);
packageName = path.resolve(process.cwd(), packageName);
}
let bailed = false;
context.emit(
'data',
'info',
`${context.module.name} ${packageManager}:`,
`Downloading project: ${packageName}`
);
let options = createOptions(context.path, context);
options.stdio = ['ignore', 'pipe', 'pipe'];
const proc = spawn('npm', ['pack', packageName], options);
// Default timeout to 10 minutes if not provided
const timeout = setTimeout(
cleanup,
context.options.timeout || 1000 * 60 * 10
);
function cleanup() {
clearTimeout(timeout);
bailed = true;
context.emit(
'data',
'error',
`${context.module.name} npm:`,
'Download Timed Out'
);
proc.kill();
reject(new Error('Download Timed Out'));
}
let filename = '';
proc.stdout.setEncoding('utf8');
proc.stdout.on('data', (chunk) => {
filename += chunk;
});
let stderr = '';
proc.stderr.setEncoding('utf8');
proc.stderr.on('data', (chunk) => {
stderr += chunk;
});
proc.on('error', (err) => {
bailed = true;
clearTimeout(timeout);
reject(err);
});
proc.on('close', (code) => {
if (bailed) return;
clearTimeout(timeout);
if (code > 0) {
return reject(new Error(`Failure getting project from npm\n${stderr}`));
}
filename = filename.trim();
if (context.module.type === 'directory') {
filename = filename.trim().split('\n');
filename = filename.pop();
}
if (filename === '') {
return reject(new Error(`No project downloaded\n${stderr}`));
}
context.emit(
'data',
'info',
`${context.module.name} npm:`,
`Project downloaded ${filename}`
);
context.unpack = path.join(context.path, filename);
resolve();
});
});
}
module.exports = grabProject;