This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
dependencies.service.js
105 lines (91 loc) · 2.58 KB
/
dependencies.service.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
97
98
99
100
101
102
103
104
105
// @flow
import { eventChannel } from 'redux-saga';
import { PACKAGE_MANAGER_CMD } from './platform.service';
import { processLogger } from './process-logger.service';
import * as childProcess from 'child_process';
import type { QueuedDependency } from '../types';
const spawnProcess = (
cmd: string,
cmdArgs: string[],
projectPath: string
): Promise<string> =>
new Promise((resolve, reject) => {
const output = {
stdout: '',
stderr: '',
};
const child = childProcess.spawn(cmd, cmdArgs, {
cwd: projectPath,
});
child.stdout.on('data', data => (output.stdout += data.toString()));
child.stderr.on('data', data => (output.stderr += data.toString()));
child.on(
'exit',
code => (code ? reject(output.stderr) : resolve(output.stdout))
);
processLogger(child, 'DEPENDENCY');
});
export const spawnProcessChannel = (
cmd: string,
cmdArgs: string[],
projectPath: string
) => {
const output = {
stdout: '',
stderr: '',
};
const child = childProcess.spawn(cmd, cmdArgs, {
cwd: projectPath,
});
return eventChannel(emitter => {
processLogger(child, 'DEPENDENCY');
child.stdout.on('data', data => {
output.stdout += data.toString();
emitter({ data: data.toString() });
});
child.stderr.on('data', data => {
output.stderr += data.toString();
emitter({ error: data.toString() });
});
child.on('exit', code => {
// emit exit code & complete data/err --> not used yet but maybe useful later
emitter({
exit: code,
data: {
data: output.stdout,
error: output.stderr,
},
});
});
// The subscriber must return an unsubscribe function
return () => {};
});
};
export const getDependencyInstallationCommand = (
dependencies: Array<QueuedDependency>
): Array<string> => {
const versionedDependencies = dependencies.map(
({ name, version }) => name + (version ? `@${version}` : '')
);
return ['add', ...versionedDependencies, '-SE'];
};
export const installDependencies = (
projectPath: string,
dependencies: Array<QueuedDependency>
) =>
spawnProcess(
PACKAGE_MANAGER_CMD,
getDependencyInstallationCommand(dependencies),
projectPath
);
export const uninstallDependencies = (
projectPath: string,
dependencies: Array<QueuedDependency>
) =>
spawnProcess(
PACKAGE_MANAGER_CMD,
['remove', ...dependencies.map(({ name }) => name)],
projectPath
);
export const reinstallDependencies = (projectPath: string) =>
spawnProcessChannel(PACKAGE_MANAGER_CMD, ['install'], projectPath);