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