Skip to content

Commit 54cc380

Browse files
committed
extracted code-wrapper etc. to separate module
1 parent e760cbf commit 54cc380

File tree

2 files changed

+113
-63
lines changed

2 files changed

+113
-63
lines changed

scripts/bundle.js

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,44 @@
11
var fs = require('fs');
2+
var path = require('path');
23

3-
//MOD: add comments for documentating additional features
4-
var preamble = '/**\n\
5-
* Modified JSON Lint parser (by russa)\n\
6-
* \n\
7-
* Parser has a "strict" mode which will throw an Error in case duplicate properties are encountered, e.g.\n\
8-
* e.g.: {\n\
9-
* "duplicate": false\n\
10-
* "duplicate": true\n\
11-
* }\n\
12-
* will cause an Error in "strict" mode.\n\
13-
* \n\
14-
* \n\
15-
* Parser returns position information for parsed JSON objects, i.e.\n\
16-
* the location within the input-string that is parsed.\n\
17-
* \n\
18-
* Position information is stored in property "_loc".\n\
19-
* Positions for properties are noted in the object\'s "_loc" in sub-property: "_"+ property-name\n\
20-
* e.g.: {\n\
21-
* "_loc": {\n\
22-
* "_someProperty": {\n\
23-
* ...\n\
24-
* \n\
25-
* Positions for array entries are noted in the array\'s "_loc" in sub-property: "_"+ entry-index\n\
26-
* e.g.: {\n\
27-
* "_loc": {\n\
28-
* "_0": {\n\
29-
* ...\n\
30-
* The object\'s / array\'s own position is noted in "_loc" in sub-property: "_this"\n\
31-
* e.g.: {\n\
32-
* "_loc": {\n\
33-
* "_this": { ...\n\
34-
* \n\
35-
* Each position information object has properties:\n\
36-
* { \n\
37-
* "first_line" : NUMBER\n\
38-
* "last_line" : NUMBER\n\
39-
* "first_column" : NUMBER\n\
40-
* "last_column" : NUMBER\n\
41-
* }\n\
42-
* \n\
43-
* \n\
44-
* \n\
45-
* NOTE: for modifications, see code comments with "\\\\MOD russa ..."\n\
46-
* \n\
47-
* based on:\n\
48-
* \n\
49-
* JSON Lint Parser gratefully provided by Zach Carter\n\
50-
* https://github.com/zaach/jsonlint\n\
51-
* MIT License\n\
52-
**/\n';
4+
var template = require('./template');
535

54-
var umdHeader = ";(function (root, factory) {\n\
55-
if (typeof define === 'function' && define.amd) {\n\
56-
define(['require','module','exports'], function(require,module,exports){return factory(require,module,exports);});\n\
57-
} else if (typeof module === 'object' && module.exports) {\n\
58-
module.exports = factory(require,module,exports);\n\
59-
} else {\n\
60-
root.jsonlint = factory(true,false,{});\n\
61-
}\n\
62-
}(typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : typeof global !== 'undefined' ? global : this, function (require, module, exports) {\n";
63-
var umdFooter = "\nreturn exports;\n}));";
6+
function bundle(srcPath){
647

65-
var source = preamble + umdHeader +
66-
fs.readFileSync(__dirname+'/../lib/jsonlint-ext.js', 'utf8') + umdFooter;
8+
var srcFile = fs.existsSync(srcPath)? fs.readFileSync(srcPath, 'utf8') : srcPath;
679

68-
console.log(source);
10+
var source = template.preamble + template.umdHeader +
11+
srcFile +
12+
template.moduleExports + template.umdFooter;
13+
14+
return source;
15+
}
16+
17+
function bundleTo(srcPath, targets){
18+
var source = bundle(srcPath);
19+
var t;
20+
for(var i=0,size=targets.length; i < size; ++i){
21+
t = targets[i];
22+
fs.writeFileSync(path.resolve(__dirname, '..', t), source);
23+
}
24+
}
25+
26+
module.exports = {
27+
bundle: bundle,
28+
bundleTo: bundleTo
29+
}
30+
31+
if (require.main === module) {
32+
33+
if(process.argv.length === 2){
34+
35+
var srcPath = path.resolve(__dirname, '..', 'jsonlint.js');
36+
console.log(bundle(srcPath));
37+
38+
} else {
39+
40+
var srcPath = path.resolve(__dirname, '..', process.argv[2]);
41+
var targets = process.argv.slice(3);
42+
bundleTo(srcPath, targets);
43+
}
44+
}

scripts/template.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
var moduleName = 'jsonlint';
3+
4+
//additional comments for documenting extension features
5+
var preamble = '/**\n\
6+
* Modified JSON Lint parser\n\
7+
* https://github.com/russaa/jsonlint-ext\n\
8+
* MIT License\n\
9+
* \n\
10+
* The parser has a "strict" mode which will throw an Error in case duplicate properties are encountered, e.g.\n\
11+
* e.g.: {\n\
12+
* "duplicate": false\n\
13+
* "duplicate": true\n\
14+
* }\n\
15+
* will cause an Error in "strict" mode.\n\
16+
* \n\
17+
* \n\
18+
* Parser returns position information for parsed JSON objects, i.e.\n\
19+
* the location within the input-string that is parsed.\n\
20+
* \n\
21+
* Position information is stored in property "_loc".\n\
22+
* Positions for properties are noted in the object\'s "_loc" in sub-property: "_"+ property-name\n\
23+
* e.g.: {\n\
24+
* "_loc": {\n\
25+
* "_someProperty": {\n\
26+
* ...\n\
27+
* \n\
28+
* Positions for array entries are noted in the array\'s "_loc" in sub-property: "_"+ entry-index\n\
29+
* e.g.: {\n\
30+
* "_loc": {\n\
31+
* "_0": {\n\
32+
* ...\n\
33+
* The object\'s / array\'s own position is noted in "_loc" in sub-property: "_this"\n\
34+
* e.g.: {\n\
35+
* "_loc": {\n\
36+
* "_this": { ...\n\
37+
* \n\
38+
* Each position information object has properties:\n\
39+
* { \n\
40+
* "first_line" : NUMBER\n\
41+
* "last_line" : NUMBER\n\
42+
* "first_column" : NUMBER\n\
43+
* "last_column" : NUMBER\n\
44+
* }\n\
45+
* \n\
46+
* \n\
47+
* based on:\n\
48+
* JSON Lint Parser gratefully provided by Zach Carter\n\
49+
* https://github.com/zaach/jsonlint\n\
50+
* MIT License\n\
51+
**/\n';
52+
53+
var moduleExports = '\n\
54+
exports.parser = jsonlint;\n\
55+
exports.Parser = jsonlint.Parser;\n\
56+
exports.parse = function () { return jsonlint.parse.apply(jsonlint, arguments); };\n';
57+
58+
var umdHeader = ";(function (root, factory) {\n\
59+
if (typeof define === 'function' && define.amd) {\n\
60+
define(['require','module','exports'], function(require,module,exports){return factory(require,module,exports);});\n\
61+
} else if (typeof module === 'object' && module.exports) {\n\
62+
module.exports = factory(require,module,exports);\n\
63+
} else {\n\
64+
root.jsonlint = factory(true,false,{});\n\
65+
}\n\
66+
}(typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : typeof global !== 'undefined' ? global : this, function (require, module, exports) {\n";
67+
var umdFooter = "\nreturn exports;\n}));";
68+
69+
module.exports = {
70+
preamble: preamble,
71+
moduleExports: moduleExports,
72+
umdHeader: umdHeader,
73+
umdFooter: umdFooter
74+
};

0 commit comments

Comments
 (0)