UNPKG

2.21 kBJavaScriptView Raw
1'use strict';
2
3var _promise = require('babel-runtime/core-js/promise');
4
5var _promise2 = _interopRequireDefault(_promise);
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
9var _ = require('lodash');
10var path = require('path');
11var globalModulesPath = require('global-modules');
12var readPackageJson = require('../in/read-package-json');
13var globalPackages = require('../in/get-installed-packages');
14var emoji = require('../out/emoji');
15var fs = require('fs');
16var chalk = require('chalk');
17
18function 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
61module.exports = init;
\No newline at end of file