From c0211878f93cd9ee02df2a90f7766c9cf3cecf7f Mon Sep 17 00:00:00 2001 From: Viatorus Date: Wed, 20 Jun 2018 13:43:47 +0200 Subject: [PATCH] build: add lokidb as meta package to install all packages at once --- README.md | 23 ++++--- dist/packages/lokidb/common.js | 102 ++++++++++++++++++++++++++++++ dist/packages/lokidb/install.js | 20 ++++++ dist/packages/lokidb/package.json | 15 +++++ dist/packages/lokidb/uninstall.js | 11 ++++ package.json | 8 ++- packages/fs-storage/package.json | 2 +- packages/loki/package.json | 2 +- packages/lokidb/common.js | 102 ++++++++++++++++++++++++++++++ packages/lokidb/install.js | 20 ++++++ packages/lokidb/package.json | 15 +++++ packages/lokidb/uninstall.js | 11 ++++ scripts/build.ts | 61 ++++++++++-------- scripts/common.ts | 11 ++-- 14 files changed, 359 insertions(+), 44 deletions(-) create mode 100644 dist/packages/lokidb/common.js create mode 100644 dist/packages/lokidb/install.js create mode 100644 dist/packages/lokidb/package.json create mode 100644 dist/packages/lokidb/uninstall.js create mode 100644 packages/lokidb/common.js create mode 100644 packages/lokidb/install.js create mode 100644 packages/lokidb/package.json create mode 100644 packages/lokidb/uninstall.js diff --git a/README.md b/README.md index 008ba4f5..43851408 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![npm status][npm]][npm-url] +[![npm status][npm]][lokidb-npm-url] [![build status][build]][build-url] [![coverage status][coverage]][coverage-url] @@ -12,23 +12,27 @@ LokiDB is the official successor of [LokiJS][lokijs-url]. ## Install -Install with npm: +Install all packages at once with: -```bash -npm install @lokidb/loki +``` +npm install lokidb ``` ## Documentation Check out our interactive [documentation](https://LokiJS-Forge.github.io/LokiDB/). -## Plugins +## Packages + +|Name|Description| +|:---|:----------| +|[@lokidb/lokidb][loki-npm-url] | A fast and feature-rich document oriented in-memory database. | ### Storage and Adapter |Name|Description| |:---|:----------| -|[@lokidb/fs-storage][fs-storage-npm-url] | A persistence adapter which persists to node fs module storage. | +|[@lokidb/fs-storage][fs-storage-npm-url] | A persistence adapter which persists to node's filesystem storage. | |[@lokidb/local-storage][local-storage-npm-url] | A persistence adapter which persists to web browser's local storage. | |[@lokidb/indexed-storage][indexed-storage-npm-url] | A persistence adapter which persists to web browser's indexed db storage. | |[@lokidb/memory-storage][memory-storage-npm-url] | A persistence adapter which persists to memory. | @@ -48,10 +52,13 @@ Check out our interactive [documentation](https://LokiJS-Forge.github.io/LokiDB/ [coverage]: https://codecov.io/gh/LokiJS-Forge/LokiDB/branch/master/graph/badge.svg [coverage-url]: https://codecov.io/gh/LokiJS-Forge/LokiDB/branch/master +[npm]: https://img.shields.io/npm/v/lokidb.svg +[lokidb-npm-url]: https://www.npmjs.com/package/lokidb + [lokijs-url]: https://github.com/techfort/LokiJS -[npm]: https://img.shields.io/npm/v/@lokidb/loki.svg -[npm-url]: https://www.npmjs.com/package/@lokidb/loki +[loki]: https://github.com/LokiJS-Forge/LokiDB +[loki-npm-url]: https://www.npmjs.com/package/@lokidb/loki [fs-storage]: https://github.com/LokiJS-Forge/LokiDB [fs-storage-npm-url]: https://www.npmjs.com/package/@lokidb/fs-storage diff --git a/dist/packages/lokidb/common.js b/dist/packages/lokidb/common.js new file mode 100644 index 00000000..a06875c5 --- /dev/null +++ b/dist/packages/lokidb/common.js @@ -0,0 +1,102 @@ +const {spawn} = require("child_process"); +const fs = require("fs"); +const path = require("path"); + +const PACKAGES = [ + "fs-storage", + "full-text-search", + "full-text-search-language", + "full-text-search-language-de", + "full-text-search-language-en", + "indexed-storage", + "local-storage", + "loki", + "memory-storage", + "partitioning-adapter", +]; + +/// MIT © Sindre Sorhus +function pathExistsSync(fp) { + try { + fs.accessSync(fp); + return true; + } catch (err) { + return false; + } +} + +/// MIT © Sindre Sorhus +function locatePathSync(iterable, options) { + options = Object.assign({ + cwd: process.cwd() + }, options); + + for (const el of iterable) { + if (pathExistsSync(path.resolve(options.cwd, el))) { + return el; + } + } +} + +/// MIT © Sindre Sorhus +function findUpSync(filename, opts = {}) { + let dir = path.resolve(opts.cwd || ""); + const {root} = path.parse(dir); + + const filenames = [].concat(filename); + + // eslint-disable-next-line no-constant-condition + while (true) { + const file = locatePathSync(filenames, {cwd: dir}); + + if (file) { + return path.join(dir, file); + } + + if (dir === root) { + return null; + } + + dir = path.dirname(dir); + } +} + +function getRootDirectory() { + return process.env.INIT_CWD || path.dirname(findUpSync("package.json")); +} + +function getRootPackageJSON() { + return require(path.join(getRootDirectory(), "package.json")); +} + +function getPackageDependencyType(packageJson, packageName) { + if (packageJson.dependencies && Object.keys(packageJson.dependencies).includes(packageName)) { + return "production"; + } else if (packageJson.devDependencies && Object.keys(packageJson.devDependencies).includes(packageName)) { + return "development"; + } + return null; +} + +function run(command, args = []) { + const child = spawn(command, args); + child.stdout.on("data", (data) => { + console.log(data.toString("utf8")); + }); + child.stderr.on("data", (data) => { + console.error(data.toString("utf8")); + }); +} + +function print(txt, lb = "\n") { + process.stdout.write(txt + lb); +} + +module.exports = { + PACKAGES, + getRootDirectory, + getRootPackageJSON, + getPackageDependencyType, + run, + print +}; diff --git a/dist/packages/lokidb/install.js b/dist/packages/lokidb/install.js new file mode 100644 index 00000000..80b19c98 --- /dev/null +++ b/dist/packages/lokidb/install.js @@ -0,0 +1,20 @@ +const common = require("./common"); + +const ROOT_DIRECTORY = common.getRootDirectory(); +const PACKAGE_JSON = common.getRootPackageJSON(); + +let DEPENDENCY_ARGUMENT = ""; +const DEPENDENCY_TYPE = common.getPackageDependencyType(PACKAGE_JSON, "lokidb"); +if (DEPENDENCY_TYPE === "production") { + DEPENDENCY_ARGUMENT = "--save-prod"; +} else if (DEPENDENCY_TYPE === "development") { + DEPENDENCY_ARGUMENT = "--save-dev"; +} + +// Bundle all packages. +const packages = common.PACKAGES.map((packageName) => `@lokidb/${packageName}`); + +common.print("Install all @lokidb packages."); + +// Install packages. +common.run("npm", ["install", "--prefix", ROOT_DIRECTORY, DEPENDENCY_ARGUMENT, ...packages]); diff --git a/dist/packages/lokidb/package.json b/dist/packages/lokidb/package.json new file mode 100644 index 00000000..18870d03 --- /dev/null +++ b/dist/packages/lokidb/package.json @@ -0,0 +1,15 @@ +{ + "name": "lokidb", + "version": "0.0.1-beta.27", + "description": "Metapackage for LokiDB.", + "author": "Various authors", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/LokiJS-Forge/LokiDB.git" + }, + "scripts": { + "postinstall": "node install.js", + "preuninstall": "node uninstall.js" + } +} diff --git a/dist/packages/lokidb/uninstall.js b/dist/packages/lokidb/uninstall.js new file mode 100644 index 00000000..4b9b5553 --- /dev/null +++ b/dist/packages/lokidb/uninstall.js @@ -0,0 +1,11 @@ +const common = require("./common"); + +const ROOT_DIRECTORY = common.getRootDirectory(); + +// Bundle all packages. +const packages = common.PACKAGES.map((packageName) => `@lokidb/${packageName}`); + +common.print("Uninstall all @lokidb packages."); + +// Uninstall packages. +common.run("npm", ["uninstall", "--prefix", ROOT_DIRECTORY, ...packages]); diff --git a/package.json b/package.json index 62cd9a50..02bfb771 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "@lokidb/lokidb-src", + "name": "@lokidb/sources", "version": "2.0.0-beta.6", - "description": "Fast document oriented javascript in-memory database", + "description": "Top-level scripts and dependencies for the LokiDB monorepo. Not meant to be published to npm.", "author": "Various authors", "license": "(MIT OR Apache-2.0)", "scripts": { @@ -26,12 +26,14 @@ }, "keywords": [ "javascript", + "typescript", "document-oriented", "mmdb", "database", + "in-memory", "json", "lokidb", - "in-memory" + "lokijs" ], "bugs": { "url": "https://github.com/LokiJS-Forge/LokiDB/issues" diff --git a/packages/fs-storage/package.json b/packages/fs-storage/package.json index 00fe7471..8858b2c3 100644 --- a/packages/fs-storage/package.json +++ b/packages/fs-storage/package.json @@ -1,6 +1,6 @@ { "name": "@lokidb/fs-storage", - "description": "A persistence adapter which persists to node fs module storage.", + "description": "A persistence adapter which persists to node's filesystem storage.", "author": "Various authors", "license": "MIT", "repository": { diff --git a/packages/loki/package.json b/packages/loki/package.json index eb1b963a..c18ceb91 100644 --- a/packages/loki/package.json +++ b/packages/loki/package.json @@ -1,6 +1,6 @@ { "name": "@lokidb/loki", - "description": "Fast document oriented javascript in-memory database", + "description": "A fast and feature-rich document oriented in-memory database.", "author": "Various authors", "license": "MIT", "repository": { diff --git a/packages/lokidb/common.js b/packages/lokidb/common.js new file mode 100644 index 00000000..a06875c5 --- /dev/null +++ b/packages/lokidb/common.js @@ -0,0 +1,102 @@ +const {spawn} = require("child_process"); +const fs = require("fs"); +const path = require("path"); + +const PACKAGES = [ + "fs-storage", + "full-text-search", + "full-text-search-language", + "full-text-search-language-de", + "full-text-search-language-en", + "indexed-storage", + "local-storage", + "loki", + "memory-storage", + "partitioning-adapter", +]; + +/// MIT © Sindre Sorhus +function pathExistsSync(fp) { + try { + fs.accessSync(fp); + return true; + } catch (err) { + return false; + } +} + +/// MIT © Sindre Sorhus +function locatePathSync(iterable, options) { + options = Object.assign({ + cwd: process.cwd() + }, options); + + for (const el of iterable) { + if (pathExistsSync(path.resolve(options.cwd, el))) { + return el; + } + } +} + +/// MIT © Sindre Sorhus +function findUpSync(filename, opts = {}) { + let dir = path.resolve(opts.cwd || ""); + const {root} = path.parse(dir); + + const filenames = [].concat(filename); + + // eslint-disable-next-line no-constant-condition + while (true) { + const file = locatePathSync(filenames, {cwd: dir}); + + if (file) { + return path.join(dir, file); + } + + if (dir === root) { + return null; + } + + dir = path.dirname(dir); + } +} + +function getRootDirectory() { + return process.env.INIT_CWD || path.dirname(findUpSync("package.json")); +} + +function getRootPackageJSON() { + return require(path.join(getRootDirectory(), "package.json")); +} + +function getPackageDependencyType(packageJson, packageName) { + if (packageJson.dependencies && Object.keys(packageJson.dependencies).includes(packageName)) { + return "production"; + } else if (packageJson.devDependencies && Object.keys(packageJson.devDependencies).includes(packageName)) { + return "development"; + } + return null; +} + +function run(command, args = []) { + const child = spawn(command, args); + child.stdout.on("data", (data) => { + console.log(data.toString("utf8")); + }); + child.stderr.on("data", (data) => { + console.error(data.toString("utf8")); + }); +} + +function print(txt, lb = "\n") { + process.stdout.write(txt + lb); +} + +module.exports = { + PACKAGES, + getRootDirectory, + getRootPackageJSON, + getPackageDependencyType, + run, + print +}; diff --git a/packages/lokidb/install.js b/packages/lokidb/install.js new file mode 100644 index 00000000..8473fb1d --- /dev/null +++ b/packages/lokidb/install.js @@ -0,0 +1,20 @@ +const common = require("./common"); + +const ROOT_DIRECTORY = common.getRootDirectory(); +const PACKAGE_JSON = common.getRootPackageJSON(); + +let DEPENDENCY_ARGUMENT = ""; +const DEPENDENCY_TYPE = common.getPackageDependencyType(PACKAGE_JSON, "lokidb"); +if (DEPENDENCY_TYPE === "production") { + DEPENDENCY_ARGUMENT = "--save-prod"; +} else if (DEPENDENCY_TYPE === "development") { + DEPENDENCY_ARGUMENT = "--save-dev"; +} + +// Bundle all packages. +const packages = common.PACKAGES.map((packageName) => `@lokidb/${packageName}`); + +common.print("Install all @lokidb packages..."); + +// Install packages. +common.run("npm", ["install", "--prefix", ROOT_DIRECTORY, DEPENDENCY_ARGUMENT, ...packages]); diff --git a/packages/lokidb/package.json b/packages/lokidb/package.json new file mode 100644 index 00000000..6988870a --- /dev/null +++ b/packages/lokidb/package.json @@ -0,0 +1,15 @@ +{ + "name": "lokidb", + "version": "0.0.1-beta.26", + "description": "Metapackage for LokiDB.", + "author": "Various authors", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/LokiJS-Forge/LokiDB.git" + }, + "scripts": { + "postinstall": "node install.js", + "preuninstall": "node uninstall.js" + } +} diff --git a/packages/lokidb/uninstall.js b/packages/lokidb/uninstall.js new file mode 100644 index 00000000..ce1f1913 --- /dev/null +++ b/packages/lokidb/uninstall.js @@ -0,0 +1,11 @@ +const common = require("./common"); + +const ROOT_DIRECTORY = common.getRootDirectory(); + +// Bundle all packages. +const packages = common.PACKAGES.map((packageName) => `@lokidb/${packageName}`); + +common.print("Uninstall all @lokidb packages..."); + +// Uninstall packages. +common.run("npm", ["uninstall", "--prefix", ROOT_DIRECTORY, ...packages]); diff --git a/scripts/build.ts b/scripts/build.ts index ee5f0ec6..a1af6a27 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -36,37 +36,43 @@ function build() { const NPM_PACKAGES_DIR = path.join(ROOT_DIR, "dist", "packages-dist"); const NPM_DIR = path.join(NPM_PACKAGES_DIR, PACKAGE); const NPM_PACKAGE_JSON = path.join(NPM_DIR, "package.json"); + const IS_REGULAR_PACKAGE = fs.existsSync(SRC_WEBPACK_CONFIG); print(`====== [${PACKAGE}]: PACKING =====`); remove_dir(OUT_DIR); - run("webpack-cli", ["--config=" + SRC_WEBPACK_CONFIG, "--output-path=" + OUT_DIR]); - - // Update script tag export of UMD to use default module export. - { - const bundle = fs.readFileSync(OUT_DIR_FILENAME).toString(); - - // Split on script export of UMD. - const script_start = bundle.search(/root\[.+] = factory.+\);/); - const script_end = script_start + bundle.slice(script_start).indexOf(";") + 1; - - const umd_part = bundle.slice(0, script_start); - let script_part = bundle.slice(script_start, script_end); - const library_part = bundle.slice(script_end); + if (IS_REGULAR_PACKAGE) { + run("webpack-cli", ["--config=" + SRC_WEBPACK_CONFIG, "--output-path=" + OUT_DIR]); // Update script tag export of UMD to use default module export. - let library_name = script_part.match(/root\["@lokidb\/(.+?)"/)[1]; - // Transform library name to Loki. - let simple_name = library_name.replace(/(?:-|^)([a-z])/ig, (_, letter) => { - return letter.toUpperCase(); - }); - if (!simple_name.startsWith("Loki")) { - simple_name = "Loki" + simple_name; - } + { + const bundle = fs.readFileSync(OUT_DIR_FILENAME).toString(); + + // Split on script export of UMD. + const script_start = bundle.search(/root\[.+] = factory.+\);/); + const script_end = script_start + bundle.slice(script_start).indexOf(";") + 1; + + const umd_part = bundle.slice(0, script_start); + let script_part = bundle.slice(script_start, script_end); + const library_part = bundle.slice(script_end); + + // Update script tag export of UMD to use default module export. + let library_name = script_part.match(/root\["@lokidb\/(.+?)"/)[1]; + // Transform library name to Loki. + let simple_name = library_name.replace(/(?:-|^)([a-z])/ig, (_, letter) => { + return letter.toUpperCase(); + }); + if (!simple_name.startsWith("Loki")) { + simple_name = "Loki" + simple_name; + } - // Add default export to script. - script_part = `{ ${script_part} root["${simple_name}"] = root["@lokidb/${library_name}"].default; }`; - fs.writeFileSync(OUT_DIR_FILENAME, umd_part + script_part + library_part); + // Add default export to script. + script_part = `{ ${script_part} root["${simple_name}"] = root["@lokidb/${library_name}"].default; }`; + fs.writeFileSync(OUT_DIR_FILENAME, umd_part + script_part + library_part); + } + } else { + makeDir(OUT_DIR); + copy(SRC_DIR, OUT_PACKAGES_DIR, true); } print(`====== [${PACKAGE}]: BUNDLING =====`); @@ -78,8 +84,11 @@ function build() { copy(SRC_PACKAGE_JSON, NPM_DIR); copy(README, NPM_DIR); - print(`====== [${PACKAGE}]: MINIFY =====`); - run("node", [UGLIFYJS, OUT_DIR_FILENAME, "--output", OUT_DIR_FILENAME_MINIFIED]); + + if (IS_REGULAR_PACKAGE) { + print(`====== [${PACKAGE}]: MINIFY =====`); + run("node", [UGLIFYJS, OUT_DIR_FILENAME, "--output", OUT_DIR_FILENAME_MINIFIED]); + } print(`====== [${PACKAGE}]: VERSIONING =====`); const data = fs.readFileSync(NPM_PACKAGE_JSON).toString("utf8"); diff --git a/scripts/common.ts b/scripts/common.ts index c58d9c7b..b062095d 100644 --- a/scripts/common.ts +++ b/scripts/common.ts @@ -3,16 +3,17 @@ import {spawnSync} from "child_process"; const cash = require("cash"); export const PACKAGES = [ - "loki", - "partitioning-adapter", - "local-storage", - "indexed-storage", "fs-storage", - "memory-storage", "full-text-search", "full-text-search-language", "full-text-search-language-de", "full-text-search-language-en", + "indexed-storage", + "local-storage", + "loki", + "lokidb", + "memory-storage", + "partitioning-adapter", ]; export function run(command: string, args: string[] = [], object: { shell?: boolean } = {}) {