-
Notifications
You must be signed in to change notification settings - Fork 38
/
Gruntfile.js
185 lines (170 loc) · 5.85 KB
/
Gruntfile.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
module.exports = function(grunt) {
'use strict';
process.env.TEST = 'test';
// Project configuration.
grunt.initConfig({
env: {
options: {
globalOption: 'foo'
},
testData: {
TEST: 'test',
data: 'bar'
},
testOptions: {
options: {
localOption: 'baz',
USER: 'fritz'
}
},
testDotEnv: {
src: ['.env', '.env.json', '.env.ini', '.env.yaml']
},
testEnvdir: {
src: ['.envdir/*'],
options: {
envdir: true
}
},
testDirectives: {
ADD_NEGATIVE: 'should not change',
PATHLIKE: 'foo:bar:baz',
UNSHIFTBASIC: 'bar',
UNSHIFTOBJECT: 'bar',
REPLACE: 'foo',
options: {
add: {
ADD_POSITIVE: 'should be this',
ADD_NEGATIVE: 'should not change to this'
},
replace: {
REPLACE: 'bar',
REPLACE_NEGATIVE: '',
},
push: {
PATHLIKE: {
value: 'qux',
delimiter: ':'
}
},
unshift: {
UNSHIFTBASIC: 'foo',
UNSHIFTOBJECT: {
value: 'foo'
}
}
}
},
testFunctions: {
DATA_FROM_FUNCTION: function() {
return process.env.DATA_FROM_FUNCTION || '123';
},
A_STRING: 'string'
}
},
clean: {
env: ['.env*']
}
});
// Load local tasks.
grunt.loadTasks('tasks');
grunt.loadNpmTasks('grunt-contrib-clean');
var assert = require('assert');
grunt.registerTask('testData', function() {
assert.equal(process.env.globalOption, 'foo', 'globalOption should be set');
assert.equal(process.env.data, 'bar', 'data should be set');
delete process.env.globalOption;
delete process.env.data;
});
grunt.registerTask('testOptions', function() {
assert.equal(process.env.globalOption, 'foo', 'globalOption should be set');
assert.equal(process.env.localOption, 'baz', 'localOption should be set');
delete process.env.globalOption;
delete process.env.localOption;
});
grunt.registerTask('testFunctions', function() {
assert.equal(process.env.DATA_FROM_FUNCTION, '123', 'should set from function');
assert.equal(process.env.A_STRING, 'string', 'should set from string');
delete process.env.DATA_FROM_FUNCTION;
delete process.env.A_STRING;
});
grunt.registerTask('testDirectives', function() {
assert.equal(process.env.ADD_NEGATIVE, 'should not change', 'add should not change existing env var');
assert.equal(process.env.ADD_POSITIVE, 'should be this', 'add should create new var if it does not exist');
assert.equal(process.env.PATHLIKE, 'foo:bar:baz:qux', 'extend should take delimiters into account');
assert.equal(process.env.UNSHIFTBASIC, 'foobar', 'should extend without specifying object');
assert.equal(process.env.UNSHIFTOBJECT, 'foobar', 'should extend while specifying object');
assert.equal(process.env.REPLACE, 'bar', 'replace should replace existing var');
delete process.env.ADD_NEGATIVE;
delete process.env.ADD_POSITIVE;
delete process.env.PATHLIKE;
delete process.env.UNSHIFTBASIC;
delete process.env.UNSHIFTOBJECT;
delete process.env.REPLACE;
delete process.env.globalOption;
});
grunt.registerTask('writeDotEnv', function() {
grunt.file.write('.env', 'dotEnvFileData=bar\ndotEnvFileOption=baz');
var json = {
jsonValue: 'foo',
push: {
PATHLIKE: 'jsonPath'
}
};
grunt.file.write('.env.json', JSON.stringify(json, null, 2));
grunt.file.write('.env.ini', 'dotEnvIniFileData=bar.ini\ndotEnvIniFileOption=baz.ini\n');
grunt.file.write('.env.yaml', 'yamlValue: foo');
});
grunt.registerTask('testDotEnv', function() {
assert(!process.env.src, 'Should not include src');
assert.equal(process.env.jsonValue, 'foo', 'value from json env file should be set');
assert.equal(process.env.PATHLIKE, 'jsonPath', 'should process directives in json');
assert.equal(process.env.globalOption, 'foo', 'should still get global options');
assert.equal(process.env.dotEnvFileData, 'bar', 'dotEnvFileData should be set');
assert.equal(process.env.dotEnvFileOption, 'baz', 'dotEnvFileOption should be set');
assert.equal(process.env.dotEnvIniFileData, 'bar.ini', 'dotEnvIniFileData should be set');
assert.equal(process.env.dotEnvIniFileOption, 'baz.ini', 'ndotEnvIniFileOption should be set');
assert.equal(process.env.yamlValue, 'foo', 'yamlValue should be set');
delete process.env.jsonValue;
delete process.env.dotEnvFileData;
delete process.env.dotEnvFileOption;
delete process.env.PATHLIKE;
delete process.env.globalOption;
delete process.env.dotEnvIniFileData;
delete process.env.dotEnvIniFileOption;
delete process.env.yamlValue;
});
grunt.registerTask('writeEnvdir', function() {
grunt.file.mkdir('.envdir/bar');
grunt.file.write('.envdir/ENVDIR', 'envdir');
grunt.file.write('.envdir/BAZ', 'baz');
grunt.file.write('.envdir/bar/FOO', 'foo');
});
grunt.registerTask('testEnvdir', function() {
assert(!process.env.envdir, 'Should not include src');
assert.equal(process.env.ENVDIR, 'envdir', 'value from envdir file should be set');
assert.equal(process.env.BAZ, 'baz', 'value from envdir file should be set');
assert(!process.env.FOO, 'Should not include subdirectories');
delete process.env.ENVDIR;
delete process.env.BAR;
});
// Default task.
grunt.registerTask('default', [
'clean',
'env:testData',
'testData',
'env:testOptions',
'testOptions',
'writeDotEnv',
'env:testDotEnv',
'testDotEnv',
'env:testDirectives',
'testDirectives',
'env:testFunctions',
'testFunctions',
'writeEnvdir',
'env:testEnvdir',
'testEnvdir',
'clean'
]);
};