Skip to content

Commit

Permalink
Target ES5 and remove esm module dependency. Fixes #145.
Browse files Browse the repository at this point in the history
Also removed makefile and use explicit import path to functions
  • Loading branch information
kofrasa committed Jul 16, 2020
1 parent 716fcd6 commit 2080667
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 93 deletions.
14 changes: 0 additions & 14 deletions Makefile

This file was deleted.

7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```
Expand Down Expand Up @@ -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
Expand Down
59 changes: 15 additions & 44 deletions prepare.js → build.js
Original file line number Diff line number Diff line change
@@ -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 = [
'.*',
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")

Expand All @@ -101,8 +74,6 @@ function main() {
fs.renameSync(tarballPath, path.join(path.dirname(LIB_DIR), tarball))
}
}

cleanup()
}

main()
2 changes: 0 additions & 2 deletions index.js

This file was deleted.

3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 8 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,37 @@
"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",
"url": "https://github.com/kofrasa/mingo.git"
},
"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",
Expand Down
2 changes: 1 addition & 1 deletion runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}
node -r esm ${FILE} ${TESTS}
2 changes: 1 addition & 1 deletion src/operators/accumulator/mergeObjects.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/operators/expression/literal.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/operators/pipeline/replaceWith.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $replaceRoot } from "./replaceRoot";
import { $replaceRoot } from "./replaceRoot"

/**
* Alias for $replaceRoot
Expand Down
2 changes: 1 addition & 1 deletion src/operators/pipeline/set.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $addFields } from "./addFields";
import { $addFields } from "./addFields"


/**
Expand Down
2 changes: 1 addition & 1 deletion src/operators/projection/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any[]>((acc: any[], item: any) => {
let v = resolve2(item, path)
let v = resolve2(item, subpath)
if (v !== undefined) acc.push(v)
return acc
}, [])
Expand Down
10 changes: 5 additions & 5 deletions test/support.js
Original file line number Diff line number Diff line change
@@ -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'},
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"compilerOptions": {
"outDir": "lib",
"target": "es6",
"target": "es5",
"moduleResolution": "node",
"module": "es2015",
"lib": ["es6", "es2017", "dom"],
"declaration": true,
"allowSyntheticDefaultImports": true
Expand Down

0 comments on commit 2080667

Please sign in to comment.