Skip to content

Commit a2785ab

Browse files
committed
feat(env): implement environment loading from file
1 parent 9a776a6 commit a2785ab

File tree

5 files changed

+251
-84
lines changed

5 files changed

+251
-84
lines changed

dist/amd/index.js

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,65 @@ define(['exports'], function (exports) {
33

44
exports.__esModule = true;
55
exports.load = load;
6-
exports.parse = parse;
76
var defaultOptions = {
87
path: './',
9-
file: 'package.json'
8+
file: 'aurelia.env'
109
};
1110

12-
function isFileApiAvailable() {
13-
if (window.File && window.FileReader && window.FileList && window.Blob) {
14-
return true;
15-
}
11+
function load(options) {
12+
window.env = {};
13+
var _options = Object.assign({}, options, defaultOptions);
1614

17-
return false;
18-
}
15+
return new Promise(function (resolve, reject) {
16+
var xhr = new XMLHttpRequest();
17+
xhr.open('GET', _options.path + _options.file);
18+
xhr.onload = function () {
19+
if (this.status >= 200 && this.status < 300) {
20+
(function () {
21+
var parsedObject = parse(xhr.response);
22+
Object.keys(parsedObject).forEach(function (key) {
23+
window.env[key] = parsedObject[key];
24+
});
1925

20-
function load(options) {
21-
if (isFileApiAvailable() === false) {
22-
console.log('File api is not available to load the aurelia environment.');
23-
}
26+
resolve();
27+
})();
28+
} else {
29+
reject({
30+
status: this.status,
31+
statusText: xhr.statusText
32+
});
33+
}
34+
};
2435

25-
var _options = Object.assign({}, options, defaultOptions);
26-
var envFile = new XMLHttpRequest();
27-
envFile.open('GET', _options.path + _options.file, true);
28-
envFile.send();
36+
xhr.onerror = function () {
37+
reject({
38+
status: this.status,
39+
statusText: xhr.statusText
40+
});
41+
};
42+
43+
xhr.send();
44+
});
2945
}
3046

31-
function parse(content) {}
47+
function parse(content) {
48+
var obj = {};
49+
50+
content.split('\n').forEach(function (line) {
51+
var keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
52+
if (keyValueArr !== null) {
53+
var key = keyValueArr[1];
54+
var value = keyValueArr[2] ? keyValueArr[2] : '';
55+
var len = value ? value.length : 0;
56+
if (len > 0 && value.charAt(0) === '\"' && value.charAt(len - 1) === '\"') {
57+
value = value.replace(/\\n/gm, '\n');
58+
}
59+
value = value.replace(/(^['"]|['"]$)/g, '').trim();
60+
61+
obj[key] = value;
62+
}
63+
});
64+
65+
return obj;
66+
}
3267
});

dist/commonjs/index.js

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,64 @@
22

33
exports.__esModule = true;
44
exports.load = load;
5-
exports.parse = parse;
65
var defaultOptions = {
76
path: './',
8-
file: 'package.json'
7+
file: 'aurelia.env'
98
};
109

11-
function isFileApiAvailable() {
12-
if (window.File && window.FileReader && window.FileList && window.Blob) {
13-
return true;
14-
}
10+
function load(options) {
11+
window.env = {};
12+
var _options = Object.assign({}, options, defaultOptions);
1513

16-
return false;
17-
}
14+
return new Promise(function (resolve, reject) {
15+
var xhr = new XMLHttpRequest();
16+
xhr.open('GET', _options.path + _options.file);
17+
xhr.onload = function () {
18+
if (this.status >= 200 && this.status < 300) {
19+
(function () {
20+
var parsedObject = parse(xhr.response);
21+
Object.keys(parsedObject).forEach(function (key) {
22+
window.env[key] = parsedObject[key];
23+
});
1824

19-
function load(options) {
20-
if (isFileApiAvailable() === false) {
21-
console.log('File api is not available to load the aurelia environment.');
22-
}
25+
resolve();
26+
})();
27+
} else {
28+
reject({
29+
status: this.status,
30+
statusText: xhr.statusText
31+
});
32+
}
33+
};
2334

24-
var _options = Object.assign({}, options, defaultOptions);
25-
var envFile = new XMLHttpRequest();
26-
envFile.open('GET', _options.path + _options.file, true);
27-
envFile.send();
35+
xhr.onerror = function () {
36+
reject({
37+
status: this.status,
38+
statusText: xhr.statusText
39+
});
40+
};
41+
42+
xhr.send();
43+
});
2844
}
2945

30-
function parse(content) {}
46+
function parse(content) {
47+
var obj = {};
48+
49+
content.split('\n').forEach(function (line) {
50+
var keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
51+
if (keyValueArr !== null) {
52+
var key = keyValueArr[1];
53+
var value = keyValueArr[2] ? keyValueArr[2] : '';
54+
var len = value ? value.length : 0;
55+
if (len > 0 && value.charAt(0) === '\"' && value.charAt(len - 1) === '\"') {
56+
value = value.replace(/\\n/gm, '\n');
57+
}
58+
value = value.replace(/(^['"]|['"]$)/g, '').trim();
59+
60+
obj[key] = value;
61+
}
62+
});
63+
64+
return obj;
65+
}

dist/es6/index.js

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
11
let defaultOptions = {
22
path: './',
3-
file: 'package.json'
3+
file: 'aurelia.env'
44
};
55

6-
function isFileApiAvailable() {
7-
if (window.File && window.FileReader && window.FileList && window.Blob) {
8-
return true;
9-
}
6+
export function load(options) {
7+
window.env = {};
8+
let _options = Object.assign({}, options, defaultOptions);
109

11-
return false;
12-
}
10+
return new Promise((resolve, reject) => {
11+
let xhr = new XMLHttpRequest();
12+
xhr.open('GET', _options.path + _options.file);
13+
xhr.onload = function() {
14+
if (this.status >= 200 && this.status < 300) {
15+
let parsedObject = parse(xhr.response);
16+
Object.keys(parsedObject).forEach(key => {
17+
window.env[key] = parsedObject[key];
18+
});
1319

14-
export function load(options : any) {
15-
if (isFileApiAvailable() === false) {
16-
console.log('File api is not available to load the aurelia environment.');
17-
}
20+
resolve();
21+
} else {
22+
reject({
23+
status: this.status,
24+
statusText: xhr.statusText
25+
});
26+
}
27+
};
1828

19-
let _options = Object.assign({}, options, defaultOptions);
20-
let envFile = new XMLHttpRequest();
21-
envFile.open('GET', _options.path + _options.file, true);
22-
envFile.send();
29+
xhr.onerror = function() {
30+
reject({
31+
status: this.status,
32+
statusText: xhr.statusText
33+
});
34+
};
35+
36+
xhr.send();
37+
});
2338
}
2439

25-
export function parse(content : string) {
40+
function parse(content) {
41+
let obj = {};
42+
43+
content.split('\n').forEach(line => {
44+
let keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
45+
if (keyValueArr !== null) {
46+
let key = keyValueArr[1];
47+
let value = keyValueArr[2] ? keyValueArr[2] : '';
48+
let len = value ? value.length : 0;
49+
if (len > 0 && value.charAt(0) === '\"' && value.charAt(len - 1) === '\"') {
50+
value = value.replace(/\\n/gm, '\n');
51+
}
52+
value = value.replace(/(^['"]|['"]$)/g, '').trim();
53+
54+
obj[key] = value;
55+
}
56+
});
2657

58+
return obj;
2759
}

dist/system/index.js

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,68 @@ System.register([], function (_export) {
55

66
_export('load', load);
77

8-
_export('parse', parse);
8+
function load(options) {
9+
window.env = {};
10+
var _options = Object.assign({}, options, defaultOptions);
911

10-
function isFileApiAvailable() {
11-
if (window.File && window.FileReader && window.FileList && window.Blob) {
12-
return true;
13-
}
12+
return new Promise(function (resolve, reject) {
13+
var xhr = new XMLHttpRequest();
14+
xhr.open('GET', _options.path + _options.file);
15+
xhr.onload = function () {
16+
if (this.status >= 200 && this.status < 300) {
17+
(function () {
18+
var parsedObject = parse(xhr.response);
19+
Object.keys(parsedObject).forEach(function (key) {
20+
window.env[key] = parsedObject[key];
21+
});
1422

15-
return false;
16-
}
23+
resolve();
24+
})();
25+
} else {
26+
reject({
27+
status: this.status,
28+
statusText: xhr.statusText
29+
});
30+
}
31+
};
1732

18-
function load(options) {
19-
if (isFileApiAvailable() === false) {
20-
console.log('File api is not available to load the aurelia environment.');
21-
}
33+
xhr.onerror = function () {
34+
reject({
35+
status: this.status,
36+
statusText: xhr.statusText
37+
});
38+
};
2239

23-
var _options = Object.assign({}, options, defaultOptions);
24-
var envFile = new XMLHttpRequest();
25-
envFile.open('GET', _options.path + _options.file, true);
26-
envFile.send();
40+
xhr.send();
41+
});
2742
}
2843

29-
function parse(content) {}
44+
function parse(content) {
45+
var obj = {};
3046

47+
content.split('\n').forEach(function (line) {
48+
var keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
49+
if (keyValueArr !== null) {
50+
var key = keyValueArr[1];
51+
var value = keyValueArr[2] ? keyValueArr[2] : '';
52+
var len = value ? value.length : 0;
53+
if (len > 0 && value.charAt(0) === '\"' && value.charAt(len - 1) === '\"') {
54+
value = value.replace(/\\n/gm, '\n');
55+
}
56+
value = value.replace(/(^['"]|['"]$)/g, '').trim();
57+
58+
obj[key] = value;
59+
}
60+
});
61+
62+
return obj;
63+
}
3164
return {
3265
setters: [],
3366
execute: function () {
3467
defaultOptions = {
3568
path: './',
36-
file: 'package.json'
69+
file: 'aurelia.env'
3770
};
3871
}
3972
};

0 commit comments

Comments
 (0)