-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (52 loc) · 1.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
const os = require('os');
const fse = require('fs-extra');
const path = require('path');
const spawn = require('child_process').spawn;
const packagesRoot = 'packages';
exports = {};
module.exports = exports;
exports.distill = function (options, callback) {
if (!{}.hasOwnProperty.call(options, 'package')) {
return callback('No Package Provided');
}
let packagePath = path.join(__dirname, packagesRoot, options.package);
let packageJson = false;
if (fse.existsSync(packagePath)) {
packageJson = path.join(packagePath, 'package.json');
console.log('Using Local Package Definition');
} else {
packagePath = path.join(__dirname, 'catchall.js');
}
let packageName = options.package;
if (packageJson && fse.existsSync(packageJson)) {
const json = require(packageJson);
packageName = json.name || packageName;
if (json.version) {
packageName += `@${json.version}`;
}
}
let tempDir = options.tempDir || process.env.DISTILLER_TEMP || `${os.homedir()}/.distiller/temp`;
tempDir = `${tempDir}/distilled-${packageName}`;
fse.mkdirpSync(tempDir);
options.tempDir = tempDir;
const tempPackageJson = path.join(tempDir, 'package.json');
fse.writeFileSync(tempPackageJson, '{"name":"distiller-temp","description":"na","respository":"na","license":"none","private":true}');
const npmInstall = spawn('npm', ['install', packageName], {
stdio: 'inherit',
cwd: tempDir
});
npmInstall.on('close', () => {
const pkg = require(packagePath);
return pkg(options, error => {
if (error) {
return callback(error);
}
// Prevent deleting current working directory
if (process.cwd() !== path.resolve(tempDir) && !options.keepTemp) {
fse.removeSync(tempDir);
}
callback();
});
});
};