1 | 'use strict';
|
2 | const _ = require('lodash');
|
3 | const path = require('path');
|
4 | const globalModulesPath = require('global-modules');
|
5 | const readPackageJson = require('../in/read-package-json');
|
6 | const globalPackages = require('../in/get-installed-packages');
|
7 | const emoji = require('../out/emoji');
|
8 | const fs = require('fs');
|
9 | const chalk = require('chalk');
|
10 |
|
11 | function init(currentState, userOptions) {
|
12 | return new Promise((resolve, reject) => {
|
13 | _.each(userOptions, (value, key) => currentState.set(key, value));
|
14 |
|
15 | if (currentState.get('global')) {
|
16 | let modulesPath = globalModulesPath;
|
17 |
|
18 | if (process.env.NODE_PATH) {
|
19 | if (process.env.NODE_PATH.indexOf(path.delimiter) !== -1) {
|
20 | modulesPath = process.env.NODE_PATH.split(path.delimiter)[0];
|
21 | console.log(chalk.yellow('warning: Using the first of multiple paths specified in NODE_PATH'));
|
22 | } else {
|
23 | modulesPath = process.env.NODE_PATH;
|
24 | }
|
25 | }
|
26 |
|
27 | if (!fs.existsSync(modulesPath)) {
|
28 | throw new Error('Path "' + modulesPath + '" does not exist. Please check the NODE_PATH environment variable.');
|
29 | }
|
30 |
|
31 | console.log(chalk.green('The global path you are searching is: ' + modulesPath));
|
32 |
|
33 | currentState.set('cwd', globalModulesPath);
|
34 | currentState.set('globalPackages', globalPackages(modulesPath));
|
35 | } else {
|
36 | const cwd = path.resolve(currentState.get('cwd'));
|
37 | const pkg = readPackageJson(path.join(cwd, 'package.json'));
|
38 | currentState.set('cwdPackageJson', pkg);
|
39 | currentState.set('cwd', cwd);
|
40 | }
|
41 |
|
42 | emoji.enabled(currentState.get('emoji'));
|
43 |
|
44 | if (currentState.get('cwdPackageJson').error) {
|
45 | return reject(currentState.get('cwdPackageJson').error);
|
46 | }
|
47 |
|
48 | return resolve(currentState);
|
49 | });
|
50 | }
|
51 |
|
52 | module.exports = init;
|