-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathexecutor.ts
116 lines (105 loc) · 3.35 KB
/
executor.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
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
106
107
108
109
110
111
112
113
114
115
116
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { BaseException } from '@angular-devkit/core';
import { SpawnOptions, spawn } from 'child_process';
import * as path from 'path';
import { Observable } from 'rxjs';
import { TaskExecutor } from '../../src';
import { NodePackageTaskFactoryOptions, NodePackageTaskOptions } from './options';
type PackageManagerProfile = {
quietArgument?: string;
commands: {
installAll?: string;
installPackage: string;
},
};
const packageManagers: { [name: string]: PackageManagerProfile } = {
'npm': {
quietArgument: '--quiet',
commands: {
installAll: 'install',
installPackage: 'install',
},
},
'cnpm': {
commands: {
installAll: 'install',
installPackage: 'install',
},
},
'yarn': {
quietArgument: '--silent',
commands: {
installPackage: 'add',
},
},
'pnpm': {
quietArgument: '--silent',
commands: {
installAll: 'install',
installPackage: 'install',
},
},
};
export class UnknownPackageManagerException extends BaseException {
constructor(name: string) {
super(`Unknown package manager "${name}".`);
}
}
export default function(
factoryOptions: NodePackageTaskFactoryOptions = {},
): TaskExecutor<NodePackageTaskOptions> {
const packageManagerName = factoryOptions.packageManager || 'npm';
const packageManagerProfile = packageManagers[packageManagerName];
if (!packageManagerProfile) {
throw new UnknownPackageManagerException(packageManagerName);
}
const rootDirectory = factoryOptions.rootDirectory || process.cwd();
return (options: NodePackageTaskOptions) => {
let taskPackageManagerProfile = packageManagerProfile;
let taskPackageManagerName = packageManagerName;
if (factoryOptions.allowPackageManagerOverride && options.packageManager) {
taskPackageManagerProfile = packageManagers[options.packageManager];
if (!taskPackageManagerProfile) {
throw new UnknownPackageManagerException(options.packageManager);
}
taskPackageManagerName = options.packageManager;
}
const outputStream = process.stdout;
const errorStream = process.stderr;
const spawnOptions: SpawnOptions = {
stdio: [ process.stdin, outputStream, errorStream ],
shell: true,
cwd: path.join(rootDirectory, options.workingDirectory || ''),
};
const args: string[] = [];
if (options.packageName) {
if (options.command === 'install') {
args.push(packageManagerProfile.commands.installPackage);
}
args.push(options.packageName);
} else if (options.command === 'install' && packageManagerProfile.commands.installAll) {
args.push(packageManagerProfile.commands.installAll);
}
if (options.quiet && taskPackageManagerProfile.quietArgument) {
args.push(taskPackageManagerProfile.quietArgument);
}
return new Observable(obs => {
spawn(taskPackageManagerName, args, spawnOptions)
.on('close', (code: number) => {
if (code === 0) {
obs.next();
obs.complete();
} else {
const message = 'Package install failed, see above.';
obs.error(new Error(message));
}
});
});
};
}