-
Notifications
You must be signed in to change notification settings - Fork 200
/
export.js
executable file
·72 lines (64 loc) · 1.9 KB
/
export.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
'use strict';
var interfacer = require('./../util/interfacer');
var preparser = require('./../preparser');
var _export = {
exec: function exec(args, options) {
args = args || [];
options = options || {};
if (args.length < 1) {
options.p = true;
}
if (typeof args === 'string' || args instanceof String) {
args = [args];
}
// Parse similarly to how `alias` does
var id = args.join(' ');
var value = '';
if (String(id).indexOf('=') > -1) {
var parts = String(id).trim().split('=');
id = parts[0];
value = parts[1] || value;
if (value.match(/^".*"$/)) {
value = JSON.parse(value);
} else {
var regMatch = value.match(/^'(.*)'$/);
if (regMatch && regMatch[1]) {
value = regMatch[1];
}
}
} else {
var parts = String(id).trim().split(' ');
id = parts.shift();
value = parts.join(' ') || null;
}
var validIdRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
if (options.p) {
for (var name in process.env) {
if (process.env.hasOwnProperty(name)) {
this.log('declare -x ' + String(name) + '=' + JSON.stringify(process.env[name]).replace(/\$/g, '\\$'));
}
}
} else if (id.match(validIdRegex)) {
process.env[id] = value !== null ? value : process.env[id];
} else {
this.log('-cash: export: `' + id + '\': not a valid identifier');
return 1;
}
return 0;
}
};
module.exports = function (vorpal) {
if (vorpal === undefined) {
return _export;
}
vorpal.api.export = _export;
vorpal.command('export [name...]').parse(preparser).option('-p', 'print all defined aliases in a reusable format').action(function (args, callback) {
args.options = args.options || {};
return interfacer.call(this, {
command: _export,
args: args.name,
options: args.options,
callback: callback
});
});
};