1 | 'use strict';
|
2 |
|
3 | var childProcess = require('child_process');
|
4 | var path = require('path');
|
5 | var shelljs = require('shelljs');
|
6 |
|
7 | var util = require('./util.js');
|
8 |
|
9 | var CWD = process.cwd();
|
10 | var TEMP = path.join(CWD, 'node_modules', '.npm-install-version-temp');
|
11 |
|
12 | function install(npmPackage) {
|
13 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
14 | var _options$destination = options.destination,
|
15 | destination = _options$destination === undefined ? util.sanitize(npmPackage) : _options$destination,
|
16 | _options$overwrite = options.overwrite,
|
17 | overwrite = _options$overwrite === undefined ? false : _options$overwrite,
|
18 | _options$quiet = options.quiet,
|
19 | quiet = _options$quiet === undefined ? false : _options$quiet;
|
20 |
|
21 |
|
22 | var log = quiet ? function () {} : function () {
|
23 | var _console;
|
24 |
|
25 | return (_console = console).log.apply(_console, arguments);
|
26 | };
|
27 |
|
28 | if (!npmPackage) util.error();
|
29 | var destinationPath = path.join(CWD, 'node_modules', destination);
|
30 | if (!overwrite && util.directoryExists(destinationPath)) {
|
31 | return log('Directory at ' + destinationPath + ' already exists, skipping');
|
32 | }
|
33 |
|
34 | var errored = false;
|
35 | try {
|
36 |
|
37 | shelljs.rm('-rf', TEMP);
|
38 | shelljs.mkdir('-p', path.join(TEMP, 'node_modules'));
|
39 |
|
40 |
|
41 | var npmrcFile = path.join(CWD, '.npmrc');
|
42 | if (shelljs.test('-f', npmrcFile)) {
|
43 | shelljs.cp(npmrcFile, TEMP);
|
44 | }
|
45 |
|
46 |
|
47 | var installOptions = {
|
48 | cwd: TEMP,
|
49 | stdio: [null, null, null]
|
50 | };
|
51 | var command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
52 | childProcess.spawnSync(command, ['install', npmPackage], installOptions);
|
53 |
|
54 |
|
55 | var packageName = util.getPackageName(npmPackage);
|
56 |
|
57 |
|
58 | shelljs.mkdir('-p', path.join(TEMP, 'node_modules', packageName, 'node_modules'));
|
59 | shelljs.ls(path.join(TEMP, 'node_modules')).forEach(function (dep) {
|
60 | if (dep === packageName) return;
|
61 | var from = path.join(TEMP, 'node_modules', dep).toString();
|
62 | var to = path.join(TEMP, 'node_modules', packageName, 'node_modules', dep).toString();
|
63 | shelljs.mv(from, to);
|
64 | });
|
65 |
|
66 |
|
67 | shelljs.rm('-rf', destinationPath);
|
68 | shelljs.mv(path.join(TEMP, 'node_modules', packageName), destinationPath);
|
69 |
|
70 | log('Installed ' + npmPackage + ' to ' + destinationPath);
|
71 | } catch (err) {
|
72 | errored = true;
|
73 | console.error('Error installing ' + npmPackage);
|
74 | console.error(err.toString());
|
75 | } finally {
|
76 |
|
77 | shelljs.rm('-rf', TEMP);
|
78 |
|
79 | if (errored) process.exit(1);
|
80 | }
|
81 | }
|
82 |
|
83 | module.exports = install;
|
84 |
|
\ | No newline at end of file |