From 1474d5ed979785c92479a298651d4c3fc8e2fba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Tue, 22 Oct 2019 12:11:00 +0200 Subject: [PATCH] feat: using browser field for browsers environment --- README.md | 19 +++++++++++++------ package.json | 1 - src/index.js | 9 ++------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 3ae081a..1bfbdc3 100644 --- a/README.md +++ b/README.md @@ -173,28 +173,35 @@ Each migration must follow this API. It must export an object in its `index.js` * `migrate` (function) - Function that performs the migration (see signature of this function below) * `revert` (function) - If defined then this function will revert the migration to the previous version. Otherwise it is assumed that it is not possible to revert this migration. -#### `.migrate(repoPath, repoOptions, isBrowser)` +#### `.migrate(repoPath, repoOptions)` _Do not confuse this function with the `require('ipfs-repo-migrations').migrate()` function that drives the whole migration process!_ Arguments: * `repoPath` (string) - absolute path to the root of the repo * `repoOptions` (object, optional) - object containing `IPFSRepo` options, that should be used to construct a datastore instance. - * `isBrowser` (bool) - indicates if the migration is run in a browser environment (as opposed to NodeJS) -#### `.revert(repoPath, repoOptions, isBrowser)` +#### `.revert(repoPath, repoOptions)` _Do not confuse this function with the `require('ipfs-repo-migrations').revert()` function that drives the whole backward migration process!_ Arguments: * `repoPath` (string) - path to the root of the repo * `repoOptions` (object, optional) - object containing `IPFSRepo` options, that should be used to construct the datastore instance. - * `isBrowser` (bool) - indicates if the migration is run in a browser environment (as opposed to NodeJS) ### Browser vs. NodeJS environments -The migration might need to distinguish in which environment it runs (browser vs. NodeJS). For this reason there is an argument -`isBrowser` passed to migrations functions. But with simple migrations it should not be necessary to distinguish between +The migration might need to perform specific tasks in browser or NodeJS environment. In such a case create +migration file `/migrations/migration-/index_browser.js` which have to follow the same API is described before. +Then add entry in `package.json` to the `browser` field as follow: + +``` +'./migrations/migration-/index.js': './migrations/migration-/index_browser.js' +``` + +In browser environments then `index.js` will be replaced with `index_browser.js`. + +Simple migrations should not need to distinguish between these environments as the datastore implementation will handle the main differences. There are currently two main datastore implementations: diff --git a/package.json b/package.json index 2f65d7d..975a68e 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,6 @@ "datastore-level": "~0.12.1", "debug": "^4.1.0", "interface-datastore": "~0.8.0", - "is-electron": "^2.2.0", "proper-lockfile": "^4.1.1", "yargs": "^14.2.0", "yargs-promise": "^1.1.0" diff --git a/src/index.js b/src/index.js index f6106d7..f0ed552 100644 --- a/src/index.js +++ b/src/index.js @@ -4,11 +4,6 @@ const defaultMigrations = require('../migrations') const repoVersion = require('./repo/version') const repoLock = require('./repo/lock') const errors = require('./errors') -const isElectron = require('is-electron') - -const IS_ENV_WITH_DOM = typeof window === 'object' && typeof document === 'object' && document.nodeType === 9 -const IS_ELECTRON = isElectron() -const IS_BROWSER = IS_ENV_WITH_DOM && !IS_ELECTRON const log = require('debug')('repo-migrations:migrator') @@ -98,7 +93,7 @@ async function migrate (path, toVersion, { ignoreLock = false, repoOptions, onPr counter++ log(`Migrating version ${migration.version}`) try { - if (!isDryRun) await migration.migrate(path, repoOptions, IS_BROWSER) + if (!isDryRun) await migration.migrate(path, repoOptions) } catch (e) { const lastSuccessfullyMigratedVersion = migration.version - 1 log(`An exception was raised during execution of migration. Setting the repo's version to last successfully migrated version: ${lastSuccessfullyMigratedVersion}`) @@ -188,7 +183,7 @@ async function revert (path, toVersion, { ignoreLock = false, repoOptions, onPro counter++ log(`Reverting migration version ${migration.version}`) try { - if (!isDryRun) await migration.revert(path, repoOptions, IS_BROWSER) + if (!isDryRun) await migration.revert(path, repoOptions) } catch (e) { const lastSuccessfullyRevertedVersion = migration.version log(`An exception was raised during execution of migration. Setting the repo's version to last successfully reverted version: ${lastSuccessfullyRevertedVersion}`)