forked from bhurlow/machine-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.js
executable file
·60 lines (47 loc) · 1.29 KB
/
import.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
#! /usr/bin/env node
var fs = require('fs');
var path = require('path');
var fse = require('fs-extra');
var tgz = require('tar.gz');
var util = require('./util');
var args = process.argv.slice(2);
var zipfile = args[0];
if (!zipfile) {
console.log('machine-import <machine.tar.gz>');
process.exit(1);
}
var machine = path.basename(zipfile, '.tar.gz');
var dm = new util.dm(machine);
try {
fs.statSync(dm.configDir);
console.log(`Machine "${machine}" already exists`);
process.exit(2);
} catch (e) {
//ok
}
fse.removeSync(dm.tmpDir);
tgz().extract(zipfile, dm.temp)
.then( function() {
processConfig(dm.tmpConfigFile);
fse.copySync(dm.tmpDir, dm.configDir);
fse.removeSync(dm.tmpDir);
console.log('Done');
process.exit(0);
})
.catch(function(err) {
console.log('Error ', err.stack);
process.exit(3);
});
function fromPortableConfig(key, value) {
if (typeof value === 'string' && value.includes('{{HOME}}')) {
value = value.replace('{{HOME}}', dm.home);
if( path.sep == '\\') {
value = value.replace(/\//g, path.sep);
}
}
return value;
}
function processConfig(configFile) {
var config = fse.readJsonSync(configFile);
fs.writeFileSync(configFile, JSON.stringify(config, fromPortableConfig, 4));
}