UNPKG

2.77 kBJavaScriptView Raw
1'use strict';
2
3var childProcess = require('child_process');
4var path = require('path');
5var shelljs = require('shelljs');
6
7var util = require('./util.js');
8
9var CWD = process.cwd();
10var TEMP = path.join(CWD, 'node_modules', '.npm-install-version-temp');
11
12function 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 // make temp install dir
37 shelljs.rm('-rf', TEMP);
38 shelljs.mkdir('-p', path.join(TEMP, 'node_modules'));
39
40 // copy local .npmrc file if exists
41 var npmrcFile = path.join(CWD, '.npmrc');
42 if (shelljs.test('-f', npmrcFile)) {
43 shelljs.cp(npmrcFile, TEMP);
44 }
45
46 // install package to temp dir
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 // get real package name
55 var packageName = util.getPackageName(npmPackage);
56
57 // move deps inside package
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 // copy to node_modules/
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 // clean up temp install dir
77 shelljs.rm('-rf', TEMP);
78
79 if (errored) process.exit(1);
80 }
81}
82
83module.exports = install;
84//# sourceMappingURL=install.js.map
\No newline at end of file