-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
98 lines (78 loc) · 3.06 KB
/
index.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
/* eslint-disable no-var */
// Dependencies
// =============================================================================
var fs = require('fs');
var path = require('path');
var util = require('util');
// Constants & Variables
// =============================================================================
var DEFAULTS = {
globalName: '__FIXTURES__',
stripPrefix: null,
transformKey: function(path) {
return path;
},
transformContent: function(path, content) {
return content;
}
};
var FILE_NAME = 'karma-file-fixtures.js';
var FILE_PATH = path.join(__dirname, FILE_NAME);
// Functions
// =============================================================================
function fileFixtures(args, config, logger, basePath) {
var log = logger.create('preprocessor.file-fixtures');
var output = '';
var settings = Object.assign({}, DEFAULTS, config.fileFixtures);
var CONFIG_FILE_PATTERNS = [].concat(config.files).map(item => item.pattern || item);
var GLOBAL_VAR = 'window.' + settings.globalName;
// Add plugin-generated JavaScript as first included <script>
config.files.unshift({
pattern : FILE_PATH,
included: true,
served : true,
watched : true
});
// Add file paths associated with plugin to config.files array
for (const key in config.preprocessors) {
const isFileFixturePath = config.preprocessors[key].indexOf('file-fixtures') !== -1;
const isInFilePatterns = CONFIG_FILE_PATTERNS.indexOf(key) !== -1;
if (isFileFixturePath && !isInFilePatterns) {
config.files.unshift({
pattern : key,
included: false,
served : false,
watched : true
});
}
}
output += util.format('%s = %s || {};\n', GLOBAL_VAR, GLOBAL_VAR);
return function(content, file, done) {
var basePath = path.posix.normalize(config.basePath + '/');
var filePath = file.originalPath
.replace(basePath, '')
.replace(settings.stripPrefix || '', '');
filePath = settings.transformKey(filePath) || filePath;
var key = util.format('%s[\'%s\']', GLOBAL_VAR, filePath);
if (output.indexOf(key) === -1) {
log.debug('Processing', file.originalPath);
content = settings.transformContent(filePath, content) || content;
content = content
.replace(/([\\'])/g, '\\$1')
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n');
output += util.format('\n%s = \'%s\';\n', key, content);
fs.writeFileSync(FILE_PATH, output);
}
done(content);
};
}
fileFixtures.$inject = ['args', 'config', 'logger', 'config.basePath'];
// Init
// =============================================================================
fs.writeFileSync(FILE_PATH, '');
// Export
// =============================================================================
module.exports = {
'preprocessor:file-fixtures': ['factory', fileFixtures]
};