-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
154 lines (123 loc) · 4.3 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
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
'use strict';
const fs = require('fs');
const path = require('path');
const esprima = require('esprima');
const util = require('util');
const stringifyObject = require("stringify-object");
const noop = function(){};
function outputFileSync (filePath, contents, fileSystem = fs) {
const dir = path.dirname(filePath);
fileSystem.mkdirSync(dir, {recursive: true});
fileSystem.writeFileSync(filePath, contents);
}
class ConfigFile {
constructor(filePath, fileSystem = fs) {
this.filePath = filePath;
this.fileSystem = fileSystem;
this.config = null;
/**
* null = never read
* var = config was read with var require = {...}
* requirejs = config was read with requirejs.config({...})
* require = config was read with require.config({...})
* empty = no config expression was found (but read() was called)
* create-if-not-exists = createIfNotExists() was called so it might not have an existing file
*/
this.type = null;
/**
* The position where the object expression should be written back to
*/
this.range = null;
this.contents = null;
}
/**
* returns the config object from the read file
*/
read() {
try {
const data = this.fileSystem.readFileSync(this.filePath);
this.contents = data.toString();
} catch (err) {
if (err.code === 'ENOENT' && this.type === 'create-if-not-exists') {
return this.config;
} else {
throw err;
}
}
let program;
try {
program = esprima.parse(this.contents, {range: true});
} catch (ex) {
throw new Error(`could not read: ${this.filePath} because it has syntax errors: ${ex}`);
}
this.type = 'empty';
if (program.type === 'Program') {
program.body.forEach(statement => {
if (statement.expression && statement.expression.type === 'CallExpression') {
const call = statement.expression;
if (call.callee.type === 'MemberExpression' && (call.callee.object.name === 'requirejs' || call.callee.object.name === 'require') && call.callee.property.name === 'config') {
this.type = call.callee.object.name === 'require' ? 'require' : 'requirejs';
this.readObjectExpression(call.arguments[0], noop);
return false;
}
} else if(statement.type === 'VariableDeclaration') {
statement.declarations.forEach(declarator => {
if (declarator.id.name === 'require') {
this.type = 'var';
this.readObjectExpression(declarator.init, noop);
return false;
}
});
if (this.type === 'var') return false;
}
});
}
if (this.type === 'empty') {
this.config = {};
}
return this.config;
}
write() {
let contents;
if (this.type === 'empty' || this.type === 'create-if-not-exists') {
contents = util.format("/* globals requirejs */\nrequirejs.config(%s);\n", this.buildConfig());
} else {
if (!this.range) {
throw new Error('The config cannot be written. Was it read() before? The config expression has to be found to allow writing. You can use createIfNotExists() to create an empty config.');
}
contents = this.contents.substring(0, this.range[0]) + this.buildConfig() + this.contents.substring(this.range[1]);
}
outputFileSync(this.filePath, contents, this.fileSystem);
}
/**
* Creates the config(File) if not already existing
*
* notice: you still need to write() it to have a physical existing file.
*/
createIfNotExists(config = {}) {
this.config = config;
this.type = 'create-if-not-exists';
}
buildConfig() {
return stringifyObject(
this.config,
{
indent: ' '
}
);
}
readObjectExpression(objectExpression, callback) {
/* jshint evil:true */
if (objectExpression && objectExpression.type === 'ObjectExpression') {
try {
this.config = eval(`(${this.contents.substring(objectExpression.range[0], objectExpression.range[1])})`);
} catch (syntaxError) {
return callback(syntaxError, null);
}
this.range = objectExpression.range;
return callback(null, this.config);
}
return callback('cannot read objectExpression from '+util.inspect(objectExpression));
}
}
module.exports = {ConfigFile};