-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
93 lines (75 loc) · 2.15 KB
/
utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var fs = require('fs.extra');
var path = require('path');
var _ = require('lodash');
var archiver = require('archiver');
var lodir = function(dir) {
var newArgs = [__dirname];
var args = Array.prototype.slice.call(arguments);
newArgs = newArgs.concat(args);
return path.join.apply(this, newArgs);
};
var getHome = function() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
};
var getConfig = function() {
var config;
try {
config = JSON.parse(fs.readFileSync(path.join(getHome(), '.sona.json')));
} catch (e) {
config = {
guid: null,
email: null,
};
fs.writeFileSync(path.join(getHome(), '.sona.json'), JSON.stringify(config, null, 2));
}
return config;
};
var getGUID = function() {
return getConfig().guid;
};
var setGUID = function(guid) {
var config = JSON.parse(fs.readFileSync(path.join(getHome(), '.sona.json')));
config.guid = guid;
fs.writeFileSync(path.join(getHome(), '.sona.json'), JSON.stringify(config, null, 2));
};
var getEmail = function() {
return getConfig().email;
};
var setEmail = function(email) {
var config = JSON.parse(fs.readFileSync(path.join(getHome(), '.sona.json')));
config.email = email;
fs.writeFileSync(path.join(getHome(), '.sona.json'), JSON.stringify(config, null, 2));
};
var zip = function(dir, name, cb) {
var output = fs.createWriteStream(lodir(name + '.zip'));
var archive = archiver('zip');
output.on('close', function() {
cb();
});
archive.on('error', function(err) {
throw err;
});
archive.pipe(output);
archive.directory(dir, name);
archive.finalize();
};
var warning = function(txt) {
console.log(txt.yellow);
};
var error = function(txt) {
console.log(txt.red);
};
var success = function(txt) {
console.log(txt.green);
};
module.exports.lodir = lodir;
module.exports.getConfig = getConfig;
module.exports.getGUID = getGUID;
module.exports.setGUID = setGUID;
module.exports.getEmail = getEmail;
module.exports.setEmail = setEmail;
module.exports.getHome = getHome;
module.exports.warning = warning;
module.exports.error = error;
module.exports.success = success;
module.exports.zip = zip;