From 20806670e40eba11df881a5403a18f34b3cb95a9 Mon Sep 17 00:00:00 2001 From: Francis Asante Date: Wed, 24 Jun 2020 11:17:01 +0200 Subject: [PATCH] Target ES5 and remove `esm` module dependency. Fixes #145. Also removed makefile and use explicit import path to functions --- Makefile | 14 ------ README.md | 7 ++- prepare.js => build.js | 59 ++++++----------------- index.js | 2 - package-lock.json | 3 +- package.json | 23 +++------ runtest.sh | 2 +- src/operators/accumulator/mergeObjects.ts | 2 +- src/operators/expression/literal.ts | 2 +- src/operators/pipeline/replaceWith.ts | 2 +- src/operators/pipeline/set.ts | 2 +- src/operators/projection/slice.ts | 2 +- src/util.ts | 5 +- test/support.js | 10 ++-- tsconfig.json | 3 +- 15 files changed, 45 insertions(+), 93 deletions(-) delete mode 100644 Makefile rename prepare.js => build.js (54%) delete mode 100644 index.js diff --git a/Makefile b/Makefile deleted file mode 100644 index ee9751016..000000000 --- a/Makefile +++ /dev/null @@ -1,14 +0,0 @@ - -release: - @npm run release - -build: - @npm run build - -clean: - @npm run clean - -test: - @npm run test - -.PHONY: test \ No newline at end of file diff --git a/README.md b/README.md index db70ce36c..b14ef9e6a 100644 --- a/README.md +++ b/README.md @@ -111,12 +111,15 @@ Any extra utility may be imported directly from the specific module. ### Importing submodules -Submodule imports are supported for both ES6 and ES5. For ES5 projects using commonJS style `require()` syntax, the [esm](https://www.npmjs.com/package/esm) is used to load the main entry point for compatibility. +Submodule imports are supported for both ES6 and ES5. The following two examples are equivalent. #### ES6 +This work natively in typescript since it knows how to load commonJS modules as ES6. +You may optionally install the [esm](https://www.npmjs.com/package/esm) module to use this syntax. + ```js import { $unwind } from 'mingo/operators/pipeline' ``` @@ -230,7 +233,7 @@ let result = agg.run(collection) ## Contributing - Squash changes into one commit -- Run `make test` to build and execute unit tests +- Run `npm test` to build and execute unit tests - Submit pull request ## License diff --git a/prepare.js b/build.js similarity index 54% rename from prepare.js rename to build.js index 3b477b118..3b6eb909c 100755 --- a/prepare.js +++ b/build.js @@ -1,25 +1,11 @@ -let fs = require('fs') -let path = require('path') -let execSync = require('child_process').execSync -let rimraf = require('rimraf') -let packageJson = require('./package.json') +const fs = require('fs') +const path = require('path') +const cp = require('child_process') +const packageJson = require('./package.json') const LIB_DIR = path.resolve('lib') - const CMD = process.argv.slice(2).join(' ') -const MAIN_JS_FILE = 'main.js' -const MAIN_JS_DATA = [ - 'require = require("esm")(module/*, options*/)', - 'module.exports = require("./index.js")' -].join('\n') - -// files to cleanup to avoid packing -const CLEANUP_FILES = [ - 'node_modules', - 'package-lock.json' -] - // .npmignore const NPM_IGNORE = [ '.*', @@ -28,43 +14,32 @@ const NPM_IGNORE = [ 'package-lock.json' ] -function cleanup() { - CLEANUP_FILES.forEach(f => rimraf.sync(path.join(LIB_DIR, f))) -} - /** - * Prepares the lib directory for distributting + * Create module in LIB_DIR */ -function prepare() { +function createModule() { + + console.log("Creating module at " + LIB_DIR) // ensure directory exists if (!fs.existsSync(LIB_DIR)) fs.mkdirSync(LIB_DIR) - console.log("Preparing", LIB_DIR) - - cleanup() - // write ignore file fs.writeFileSync(path.join(LIB_DIR, '.npmignore'), NPM_IGNORE.join('\n')) // copy all the allowed files to the lib directory - packageJson.files = packageJson.files.reduce((files, p) => { - if (!p.match(/(lib|index\.js)/)) { - fs.copyFileSync(path.resolve(p), path.join(LIB_DIR, p)) - files.push(p) - } + packageJson.files = ["LICENSE", "README.md", "CHANGELOG.md"].reduce((files, p) => { + fs.copyFileSync(path.resolve(p), path.join(LIB_DIR, p)) + files.push(p) return files }, [ '**/*.js', '**/*.ts' ]) - // write main entry files using esm - fs.writeFileSync(path.join(LIB_DIR, MAIN_JS_FILE), MAIN_JS_DATA) - // override entry files - packageJson.main = MAIN_JS_FILE - packageJson.module = 'index.js' + packageJson.main = 'index.js' + packageJson.module = packageJson.main packageJson.typings = 'index.d.ts' // clear all scripts @@ -75,12 +50,10 @@ function prepare() { // write new package.json for lib fs.writeFileSync(path.join(LIB_DIR, 'package.json'), data) - - console.log("Prepared", LIB_DIR) } function main() { - prepare() + createModule() if (CMD) { // execute within lib dir @@ -89,7 +62,7 @@ function main() { console.log("\nExecuting command:", npm_cmd, "\n") // execute command - execSync(npm_cmd, { cwd: LIB_DIR, env: process.env, stdio: 'inherit' }) + cp.execSync(npm_cmd, { cwd: LIB_DIR, env: process.env, stdio: 'inherit' }) console.log("\nCompleted command\n") @@ -101,8 +74,6 @@ function main() { fs.renameSync(tarballPath, path.join(path.dirname(LIB_DIR), tarball)) } } - - cleanup() } main() \ No newline at end of file diff --git a/index.js b/index.js deleted file mode 100644 index 127dc5a90..000000000 --- a/index.js +++ /dev/null @@ -1,2 +0,0 @@ -require = require("esm")(module/*, options*/) -module.exports = require("./lib") \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index e4fa511d0..b346c2f77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -591,7 +591,8 @@ "esm": { "version": "3.2.25", "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz", - "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==" + "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==", + "dev": true }, "esprima": { "version": "4.0.1", diff --git a/package.json b/package.json index 80c7d20b6..0a13b456e 100644 --- a/package.json +++ b/package.json @@ -2,20 +2,17 @@ "name": "mingo", "version": "3.0.2", "description": "JavaScript implementation of MongoDB query language", - "main": "index.js", + "main": "./lib/index.js", "module": "./lib/index.js", "typings": "./lib/index.d.ts", "scripts": { "docs": "typedoc && touch docs/.nojekyll", - "build": "tsc", - "clean": "rm -fr lib", - "release": "npm run clean && npm test", - "testall": "NODE_ENV=test nyc --reporter=lcov --reporter=text ./runtest.sh", - "test": "npm run build && npm run testall", + "build": "tsc && node ./build.js", + "test": "npm run build && NODE_ENV=test nyc --reporter=lcov --reporter=text ./runtest.sh", + "dist": "npm run test && cd lib && npm publish", "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", - "postpack": "node ./prepare.js pack", - "prepublishOnly": "npm run release && node ./prepare.js publish", - "postinstall": "node ./prepare.js install" + "prepublishOnly": "echo \"use 'npm run dist'\" && false", + "postinstall": "node ./build.js install" }, "repository": { "type": "git", @@ -23,23 +20,19 @@ }, "files": [ "lib/", - "index.js", "LICENSE", "README.md", "CHANGELOG.md" ], - "dependencies": { - "esm": "3.2.25" - }, "devDependencies": { "codecov": "3.7.0", + "esm": "3.2.25", "lodash": "4.17.19", "nyc": "15.1.0", "perf_hooks": "0.0.1", "tape": "5.0.1", "typescript": "3.9.6", - "typedoc": "0.17.8", - "rimraf": "3.0.2" + "typedoc": "0.17.8" }, "keywords": [ "util", diff --git a/runtest.sh b/runtest.sh index 58cd52c52..b966020a6 100755 --- a/runtest.sh +++ b/runtest.sh @@ -26,4 +26,4 @@ process.argv.slice(2).forEach(function (file) { eof # execute test with esm -node -r esm -r './lib/init/system' ${FILE} ${TESTS} \ No newline at end of file +node -r esm ${FILE} ${TESTS} \ No newline at end of file diff --git a/src/operators/accumulator/mergeObjects.ts b/src/operators/accumulator/mergeObjects.ts index 7c04e2478..0371b6c13 100644 --- a/src/operators/accumulator/mergeObjects.ts +++ b/src/operators/accumulator/mergeObjects.ts @@ -1,4 +1,4 @@ -import { $mergeObjects as __mergeObjects } from '../expression' +import { $mergeObjects as __mergeObjects } from '../expression/object/mergeObjects' /** * Combines multiple documents into a single document. diff --git a/src/operators/expression/literal.ts b/src/operators/expression/literal.ts index 9737e0271..244614cf9 100644 --- a/src/operators/expression/literal.ts +++ b/src/operators/expression/literal.ts @@ -1,6 +1,6 @@ // Literal Expression Operators: https://docs.mongodb.com/manual/reference/operator/aggregation/#literal-expression-operator -import { Options } from "../../core"; +import { Options } from "../../core" /** * Return a value without parsing. diff --git a/src/operators/pipeline/replaceWith.ts b/src/operators/pipeline/replaceWith.ts index 62abcbff7..67c7158bd 100644 --- a/src/operators/pipeline/replaceWith.ts +++ b/src/operators/pipeline/replaceWith.ts @@ -1,4 +1,4 @@ -import { $replaceRoot } from "./replaceRoot"; +import { $replaceRoot } from "./replaceRoot" /** * Alias for $replaceRoot diff --git a/src/operators/pipeline/set.ts b/src/operators/pipeline/set.ts index 177fa07f1..d078a5a6e 100644 --- a/src/operators/pipeline/set.ts +++ b/src/operators/pipeline/set.ts @@ -1,4 +1,4 @@ -import { $addFields } from "./addFields"; +import { $addFields } from "./addFields" /** diff --git a/src/operators/projection/slice.ts b/src/operators/projection/slice.ts index 90d2db30d..b4c26148f 100644 --- a/src/operators/projection/slice.ts +++ b/src/operators/projection/slice.ts @@ -2,7 +2,7 @@ import { isArray, resolve } from '../../util' import { Options } from '../../core' -import { $slice as __slice } from '../expression/array' +import { $slice as __slice } from '../expression/array/slice' /** * Limits the number of elements projected from an array. Supports skip and limit slices. diff --git a/src/util.ts b/src/util.ts index 8c4ac7ffa..1e7f9d25f 100644 --- a/src/util.ts +++ b/src/util.ts @@ -613,9 +613,10 @@ export function resolve(obj: object | any[], selector: string, options?: Resolve if (i === 0 && depth > 0) break depth += 1 - path = path.slice(i) + // only look at the rest of the path + let subpath = path.slice(i) value = value.reduce((acc: any[], item: any) => { - let v = resolve2(item, path) + let v = resolve2(item, subpath) if (v !== undefined) acc.push(v) return acc }, []) diff --git a/test/support.js b/test/support.js index dbc09a29b..087e98ecd 100644 --- a/test/support.js +++ b/test/support.js @@ -1,12 +1,12 @@ -import fs from 'fs' import test from 'tape' import mingo from '../lib' import { computeValue } from '../lib/core' +import '../lib/init/system' -export const personData = JSON.parse(fs.readFileSync(__dirname + '/data/person.json')) -export const simpleGradesData = JSON.parse(fs.readFileSync(__dirname + '/data/grades_simple.json')) -export const complexGradesData = JSON.parse(fs.readFileSync(__dirname + '/data/grades_complex.json')) -export const studentsData = JSON.parse(fs.readFileSync(__dirname + '/data/students.json')) +export const personData = require('./data/person.json') +export const simpleGradesData = require('./data/grades_simple.json') +export const complexGradesData = require('./data/grades_complex.json') +export const studentsData = require('./data/students.json') export const groupByObjectsData = [ {'date_buckets': {'date': '2015-04-29T00:17:03.107Z', 'day': 28, 'hour': 18, 'minute': 17, 'sec': 3, 'hour_minute': '18:17'}, 'Keyword ID': 'sr3_4VzRD3sp', 'Creative ID': '5184986203', 'Keyword': 'Bathroom Cleaning Tips', 'Match Type': 'be', 'Device': 'm', 'Conversions': [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 'Revenues': [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 'account_id': 'baron'}, diff --git a/tsconfig.json b/tsconfig.json index b1fb28852..da09a3745 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,8 @@ { "compilerOptions": { "outDir": "lib", - "target": "es6", + "target": "es5", "moduleResolution": "node", - "module": "es2015", "lib": ["es6", "es2017", "dom"], "declaration": true, "allowSyntheticDefaultImports": true