-
Notifications
You must be signed in to change notification settings - Fork 84
/
add.js
51 lines (45 loc) · 1.53 KB
/
add.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
// @flow
import Project from '../Project';
import Package from '../Package';
import * as options from '../utils/options';
import * as logger from '../utils/logger';
import addDependenciesToPackage from '../utils/addDependenciesToPackages';
import type { Dependency, configDependencyType } from '../types';
import { DEPENDENCY_TYPE_FLAGS_MAP } from '../constants';
import type { ProjectAddOptions } from './project/add';
export type AddOptions = {
cwd?: string,
deps: Array<Dependency>,
type: configDependencyType
};
export function toAddOptions(
args: options.Args,
flags: options.Flags
): AddOptions {
let depsArgs = [];
let type = 'dependencies';
// args is each of our dependencies we are adding which may have "@version" parts to them
args.forEach(dep => {
depsArgs.push(options.toDependency(dep));
});
Object.keys(DEPENDENCY_TYPE_FLAGS_MAP).forEach(depTypeFlag => {
if (flags[depTypeFlag]) {
type = DEPENDENCY_TYPE_FLAGS_MAP[depTypeFlag];
// check if value of dependency flag is a package name and then push to dependency arguments
if (typeof flags[depTypeFlag] === 'string') {
depsArgs.push(options.toDependency(flags[depTypeFlag]));
}
}
});
return {
cwd: options.string(flags.cwd, 'cwd'),
deps: depsArgs,
type
};
}
export async function add(opts: AddOptions | ProjectAddOptions) {
let cwd = opts.cwd || process.cwd();
let project = await Project.init(cwd);
let pkg = await Package.closest(cwd);
await addDependenciesToPackage(project, pkg, opts.deps, opts.type);
}