|
1 | 1 | var fs = require('fs'); |
| 2 | +var path = require('path'); |
2 | 3 |
|
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'); |
53 | 5 |
|
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){ |
64 | 7 |
|
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; |
67 | 9 |
|
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 | +} |
0 commit comments