From 190c1748050b3be9dbd5f53873f177b0401c437c Mon Sep 17 00:00:00 2001 From: Simon Legner Date: Wed, 29 Jan 2020 23:22:13 +0100 Subject: [PATCH] Use rollup for building/bundling --- .gitignore | 3 + Makefile | 10 - free.js | 2 +- index.js | 11 +- overpass-wizard | 2 +- package.json | 14 +- parser.js | 571 +++++++++++++++++++++++------------------------ rollup.config.js | 13 ++ test/test.js | 2 +- 9 files changed, 313 insertions(+), 315 deletions(-) create mode 100644 .gitignore delete mode 100644 Makefile create mode 100644 rollup.config.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9725d20 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.tgz +bundle.*.js +node_modules/ diff --git a/Makefile b/Makefile deleted file mode 100644 index 8a1a437..0000000 --- a/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -# this builds overpass turbo - -PEGJS = ./node_modules/pegjs/bin/pegjs - -all: - npm update - npm run build - -clean: - rm parser.js diff --git a/free.js b/free.js index 63d35f1..0d3ba41 100644 --- a/free.js +++ b/free.js @@ -1,5 +1,5 @@ // ffs/wizard module -module.exports = function(presets) { +export function free(presets) { var freeFormQuery = {}; if (!presets) presets = {}; diff --git a/index.js b/index.js index e3b12a2..1870255 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ -var parser = require("./parser.js"); +import {parse} from "./parser.js"; +import {free} from "./free.js"; var freeFormQuery; // todo: refactor this! // todo: normalization -> node module? @@ -44,7 +45,7 @@ function normalize(query) { return normalized_query; } -module.exports = function wizard(search, options) { +export default function wizard(search, options) { var defaults = { comment: true, outputMode: "geom", // "recursive", "recursive_meta", out <*> ("geom", "ids", …) @@ -71,9 +72,9 @@ module.exports = function wizard(search, options) { var parsedQuery; try { - parsedQuery = parser.parse(search); + parsedQuery = parse(search); } catch(e) { - console.error("couldn't parse wizard input"); + console.error("couldn't parse wizard input", e); return false; } @@ -291,7 +292,7 @@ module.exports = function wizard(search, options) { // todo: looks like some code duplication here could be reduced by refactoring if (cond_query.query === "free form") { // eventually load free form query module - if (!freeFormQuery) freeFormQuery = require('./free')(options.freeFormPresets); + if (!freeFormQuery) freeFormQuery = free(options.freeFormPresets); var ffs_clause = freeFormQuery.get_query_clause(cond_query); if (ffs_clause === false) { console.error("Couldn't find preset for free form input: "+cond_query.free) diff --git a/overpass-wizard b/overpass-wizard index 2e6c60d..1e1c6ef 100755 --- a/overpass-wizard +++ b/overpass-wizard @@ -24,7 +24,7 @@ var argv = require('yargs') fs = require('fs'), concat = require('concat-stream'), expand = require('./expand'), - wizard = require('./'); + wizard = require('./bundle.umd'); if (argv.version) { return process.stdout.write(require('./package.json').version+'\n'); diff --git a/package.json b/package.json index c76a999..59d431b 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,13 @@ "name": "overpass-wizard", "version": "0.0.9", "description": "Human friendly way to generate Overpass API queries", - "main": "index.js", + "main": "bundle.umd.js", "scripts": { - "build": "pegjs -O size < wizard.pegjs > parser.js", - "pretest": "npm ls --depth=Infinity > /dev/null", - "test": "npm run test-lib", + "build": "npm run build:pegjs && npm run build:rollup && npm run build:rollup:min", + "build:pegjs": "pegjs --format es --optimize size < wizard.pegjs > parser.js", + "build:rollup": "rollup --config", + "build:rollup:min": "rollup --config --environment NODE_ENV:production", + "test": "npm run build && npm run test-lib", "test-lib": "mocha -R spec" }, "bin": { @@ -36,7 +38,9 @@ "devDependencies": { "expect.js": "^0.3.1", "mocha": "^7.0.1", - "pegjs": "^0.10.0" + "pegjs": "pegjs/pegjs#6939d1eb8d4b36df9be08696b75d2ca526845794", + "rollup": "^1.30.1", + "rollup-plugin-terser": "^5.2.0" }, "browser": { "request-promise": false diff --git a/parser.js b/parser.js index d2d0486..194b184 100644 --- a/parser.js +++ b/parser.js @@ -1,23 +1,19 @@ -/* - * Generated by PEG.js 0.10.0. - * - * http://pegjs.org/ - */ - -"use strict"; +// Generated by PEG.js 0.10.0. +// +// https://pegjs.org/ function peg$subclass(child, parent) { - function ctor() { this.constructor = child; } - ctor.prototype = parent.prototype; - child.prototype = new ctor(); + function C() { this.constructor = child; } + C.prototype = parent.prototype; + child.prototype = new C(); } function peg$SyntaxError(message, expected, found, location) { - this.message = message; + this.message = message; this.expected = expected; - this.found = found; + this.found = found; this.location = location; - this.name = "SyntaxError"; + this.name = "SyntaxError"; if (typeof Error.captureStackTrace === "function") { Error.captureStackTrace(this, peg$SyntaxError); @@ -28,35 +24,32 @@ peg$subclass(peg$SyntaxError, Error); peg$SyntaxError.buildMessage = function(expected, found) { var DESCRIBE_EXPECTATION_FNS = { - literal: function(expectation) { - return "\"" + literalEscape(expectation.text) + "\""; - }, - - "class": function(expectation) { - var escapedParts = "", - i; - - for (i = 0; i < expectation.parts.length; i++) { - escapedParts += expectation.parts[i] instanceof Array - ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1]) - : classEscape(expectation.parts[i]); - } - - return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]"; - }, - - any: function(expectation) { - return "any character"; - }, - - end: function(expectation) { - return "end of input"; - }, - - other: function(expectation) { - return expectation.description; - } - }; + literal: function(expectation) { + return "\"" + literalEscape(expectation.text) + "\""; + }, + + class: function(expectation) { + var escapedParts = expectation.parts.map(function(part) { + return Array.isArray(part) + ? classEscape(part[0]) + "-" + classEscape(part[1]) + : classEscape(part); + }); + + return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]"; + }, + + any: function() { + return "any character"; + }, + + end: function() { + return "end of input"; + }, + + other: function(expectation) { + return expectation.description; + } + }; function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); @@ -64,28 +57,28 @@ peg$SyntaxError.buildMessage = function(expected, found) { function literalEscape(s) { return s - .replace(/\\/g, '\\\\') - .replace(/"/g, '\\"') - .replace(/\0/g, '\\0') - .replace(/\t/g, '\\t') - .replace(/\n/g, '\\n') - .replace(/\r/g, '\\r') - .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); + .replace(/\\/g, "\\\\") + .replace(/"/g, "\\\"") + .replace(/\0/g, "\\0") + .replace(/\t/g, "\\t") + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); }); } function classEscape(s) { return s - .replace(/\\/g, '\\\\') - .replace(/\]/g, '\\]') - .replace(/\^/g, '\\^') - .replace(/-/g, '\\-') - .replace(/\0/g, '\\0') - .replace(/\t/g, '\\t') - .replace(/\n/g, '\\n') - .replace(/\r/g, '\\r') - .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); + .replace(/\\/g, "\\\\") + .replace(/\]/g, "\\]") + .replace(/\^/g, "\\^") + .replace(/-/g, "\\-") + .replace(/\0/g, "\\0") + .replace(/\t/g, "\\t") + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); }); } function describeExpectation(expectation) { @@ -93,12 +86,8 @@ peg$SyntaxError.buildMessage = function(expected, found) { } function describeExpected(expected) { - var descriptions = new Array(expected.length), - i, j; - - for (i = 0; i < expected.length; i++) { - descriptions[i] = describeExpectation(expected[i]); - } + var descriptions = expected.map(describeExpectation); + var i, j; descriptions.sort(); @@ -134,202 +123,202 @@ peg$SyntaxError.buildMessage = function(expected, found) { }; function peg$parse(input, options) { - options = options !== void 0 ? options : {}; - - var peg$FAILED = {}, - - peg$startRuleIndices = { start: 0 }, - peg$startRuleIndex = 0, - - peg$consts = [ - function(x) { return x }, - "in bbox", - peg$literalExpectation("in bbox", false), - "IN BBOX", - peg$literalExpectation("IN BBOX", false), - function(x) { return { bounds:"bbox", query:x } }, - "in", - peg$literalExpectation("in", false), - "IN", - peg$literalExpectation("IN", false), - function(x, y) { return { bounds:"area", query:x, area:y } }, - "around", - peg$literalExpectation("around", false), - "AROUND", - peg$literalExpectation("AROUND", false), - function(x, y) { return { bounds:"around", query:x, area:y } }, - "global", - peg$literalExpectation("global", false), - "GLOBAL", - peg$literalExpectation("GLOBAL", false), - function(x) { return { bounds:"global", query:x } }, - "or", - peg$literalExpectation("or", false), - "OR", - peg$literalExpectation("OR", false), - "||", - peg$literalExpectation("||", false), - "|", - peg$literalExpectation("|", false), - function(x, y) { return { logical:"or", queries:[x,y] } }, - "and", - peg$literalExpectation("and", false), - "AND", - peg$literalExpectation("AND", false), - "&&", - peg$literalExpectation("&&", false), - "&", - peg$literalExpectation("&", false), - function(x, y) { return { logical:"and", queries:[x,y] } }, - "(", - peg$literalExpectation("(", false), - ")", - peg$literalExpectation(")", false), - function(x) { return x; }, - "==", - peg$literalExpectation("==", false), - "=", - peg$literalExpectation("=", false), - function(x, y) { return { query:"eq", key:x, val:y } }, - "!=", - peg$literalExpectation("!=", false), - "<>", - peg$literalExpectation("<>", false), - function(x, y) { return { query:"neq", key:x, val:y } }, - "*", - peg$literalExpectation("*", false), - function(x) { return { query:"key", key:x } }, - "is", - peg$literalExpectation("is", false), - "not", - peg$literalExpectation("not", false), - "null", - peg$literalExpectation("null", false), - "IS", - peg$literalExpectation("IS", false), - "NOT", - peg$literalExpectation("NOT", false), - "NULL", - peg$literalExpectation("NULL", false), - function(x) { return { query:"nokey", key:x } }, - "~=", - peg$literalExpectation("~=", false), - "~", - peg$literalExpectation("~", false), - "=~", - peg$literalExpectation("=~", false), - function(x, y) { return { query:"like", key:x, val:y.regex?y:{regex:y} } }, - "like", - peg$literalExpectation("like", false), - "LIKE", - peg$literalExpectation("LIKE", false), - function(x, y) { return { query:"likelike", key:x.regex?x:{regex:x}, val:y.regex?y:{regex:y} } }, - "!~", - peg$literalExpectation("!~", false), - function(x, y) { return { query:"notlike", key:x, val:y.regex?y:{regex:y} } }, - ":", - peg$literalExpectation(":", false), - function(x, y) { return { query:"substr", key:x, val:y } }, - "type", - peg$literalExpectation("type", false), - function(x) { return { query:"type", type:x } }, - "user", - peg$literalExpectation("user", false), - "uid", - peg$literalExpectation("uid", false), - "newer", - peg$literalExpectation("newer", false), - "id", - peg$literalExpectation("id", false), - function(x, y) { return { query:"meta", meta:x, val:y } }, - function(x) { return { query:"free form", free:x } }, - peg$otherExpectation("Key"), - /^[a-zA-Z0-9_:\-]/, - peg$classExpectation([["a", "z"], ["A", "Z"], ["0", "9"], "_", ":", "-"], false, false), - function(s) { return s.join(''); }, - "\"", - peg$literalExpectation("\"", false), - "'", - peg$literalExpectation("'", false), - function(parts) { - return parts[1]; - }, - peg$otherExpectation("string"), - /^[^'" ()~=!*\/:<>&|[\]{}#+@$%?\^.,]/, - peg$classExpectation(["'", "\"", " ", "(", ")", "~", "=", "!", "*", "/", ":", "<", ">", "&", "|", "[", "]", "{", "}", "#", "+", "@", "$", "%", "?", "^", ".", ","], true, false), - function(chars) { return chars.join(""); }, - "\\", - peg$literalExpectation("\\", false), - peg$anyExpectation(), - function(char_) { return char_; }, - function(sequence) { return sequence; }, - /^['"\\bfnrtv]/, - peg$classExpectation(["'", "\"", "\\", "b", "f", "n", "r", "t", "v"], false, false), - function(char_) { - return char_ - .replace("b", "\b") - .replace("f", "\f") - .replace("n", "\n") - .replace("r", "\r") - .replace("t", "\t") - .replace("v", "\x0B") // IE does not recognize "\v". - }, - "/", - peg$literalExpectation("/", false), - "i", - peg$literalExpectation("i", false), - "", - function(parts) { - return { regex: parts[1], modifier: parts[3] }; - }, - "\\/", - peg$literalExpectation("\\/", false), - function() { return "/"; }, - peg$otherExpectation("whitespace"), - /^[ \t\n\r]/, - peg$classExpectation([" ", "\t", "\n", "\r"], false, false) - ], - - peg$bytecode = [ - peg$decode("%;;/:#;!/1$;;/($8#: #!!)(#'#(\"'#&'#"), - peg$decode("%;\"/Y#$;\"\"6>7?.A &2@\"\"6@7A.5 &2B\"\"6B7C.) &2D\"\"6D7E/H$$;", + peg$literalExpectation("<>", false), + function(x, y) { return { query:"neq", key:x, val:y } }, + "*", + peg$literalExpectation("*", false), + function(x) { return { query:"key", key:x } }, + "is", + peg$literalExpectation("is", false), + "not", + peg$literalExpectation("not", false), + "null", + peg$literalExpectation("null", false), + "IS", + peg$literalExpectation("IS", false), + "NOT", + peg$literalExpectation("NOT", false), + "NULL", + peg$literalExpectation("NULL", false), + function(x) { return { query:"nokey", key:x } }, + "~=", + peg$literalExpectation("~=", false), + "~", + peg$literalExpectation("~", false), + "=~", + peg$literalExpectation("=~", false), + function(x, y) { return { query:"like", key:x, val:y.regex?y:{regex:y} } }, + "like", + peg$literalExpectation("like", false), + "LIKE", + peg$literalExpectation("LIKE", false), + function(x, y) { return { query:"likelike", key:x.regex?x:{regex:x}, val:y.regex?y:{regex:y} } }, + "!~", + peg$literalExpectation("!~", false), + function(x, y) { return { query:"notlike", key:x, val:y.regex?y:{regex:y} } }, + ":", + peg$literalExpectation(":", false), + function(x, y) { return { query:"substr", key:x, val:y } }, + "type", + peg$literalExpectation("type", false), + function(x) { return { query:"type", type:x } }, + "user", + peg$literalExpectation("user", false), + "uid", + peg$literalExpectation("uid", false), + "newer", + peg$literalExpectation("newer", false), + "id", + peg$literalExpectation("id", false), + function(x, y) { return { query:"meta", meta:x, val:y } }, + function(x) { return { query:"free form", free:x } }, + peg$otherExpectation("Key"), + /^[a-zA-Z0-9_:\-]/, + peg$classExpectation([["a", "z"], ["A", "Z"], ["0", "9"], "_", ":", "-"], false, false), + function(s) { return s.join(''); }, + "\"", + peg$literalExpectation("\"", false), + "'", + peg$literalExpectation("'", false), + function(parts) { + return parts[1]; + }, + peg$otherExpectation("string"), + /^[^'" ()~=!*\/:<>&|[\]{}#+@$%?\^.,]/, + peg$classExpectation(["'", "\"", " ", "(", ")", "~", "=", "!", "*", "/", ":", "<", ">", "&", "|", "[", "]", "{", "}", "#", "+", "@", "$", "%", "?", "^", ".", ","], true, false), + function(chars) { return chars.join(""); }, + "\\", + peg$literalExpectation("\\", false), + peg$anyExpectation(), + function(char_) { return char_; }, + function(sequence) { return sequence; }, + /^['"\\bfnrtv]/, + peg$classExpectation(["'", "\"", "\\", "b", "f", "n", "r", "t", "v"], false, false), + function(char_) { + return char_ + .replace("b", "\b") + .replace("f", "\f") + .replace("n", "\n") + .replace("r", "\r") + .replace("t", "\t") + .replace("v", "\x0B") // IE does not recognize "\v". + }, + "/", + peg$literalExpectation("/", false), + "i", + peg$literalExpectation("i", false), + "", + function(parts) { + return { regex: parts[1], modifier: parts[3] }; + }, + "\\/", + peg$literalExpectation("\\/", false), + function() { return "/"; }, + peg$otherExpectation("whitespace"), + /^[ \t\n\r]/, + peg$classExpectation([" ", "\t", "\n", "\r"], false, false) + ]; + + var peg$bytecode = [ + peg$decode("%;;/:#;!/1$;;/($8#: #!!)(#'#(\"'#&'#"), + peg$decode("%;\"/Y#$;\"\"6>7?.A &2@\"\"6@7A.5 &2B\"\"6B7C.) &2D\"\"6D7E/H$$; ({ + input: "index.js", + output: { + file: isProduction ? `bundle.${format}.min.js` : `bundle.${format}.js`, + format: format, + name: "overpassWizard" + }, + plugins: isProduction ? [terser()] : [] +})); diff --git a/test/test.js b/test/test.js index 3c02f90..b26ccd0 100644 --- a/test/test.js +++ b/test/test.js @@ -1,5 +1,5 @@ var expect = require("expect.js"), - wizard = require("../."); + wizard = require("../bundle.umd.js"); function compact(q) { q = q.replace(/\/\*[\s\S]*?\*\//g,"");