Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 1474d5e

Browse files
committed
feat: using browser field for browsers environment
1 parent 662d89b commit 1474d5e

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,28 +173,35 @@ Each migration must follow this API. It must export an object in its `index.js`
173173
* `migrate` (function) - Function that performs the migration (see signature of this function below)
174174
* `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.
175175

176-
#### `.migrate(repoPath, repoOptions, isBrowser)`
176+
#### `.migrate(repoPath, repoOptions)`
177177

178178
_Do not confuse this function with the `require('ipfs-repo-migrations').migrate()` function that drives the whole migration process!_
179179

180180
Arguments:
181181
* `repoPath` (string) - absolute path to the root of the repo
182182
* `repoOptions` (object, optional) - object containing `IPFSRepo` options, that should be used to construct a datastore instance.
183-
* `isBrowser` (bool) - indicates if the migration is run in a browser environment (as opposed to NodeJS)
184183

185-
#### `.revert(repoPath, repoOptions, isBrowser)`
184+
#### `.revert(repoPath, repoOptions)`
186185

187186
_Do not confuse this function with the `require('ipfs-repo-migrations').revert()` function that drives the whole backward migration process!_
188187

189188
Arguments:
190189
* `repoPath` (string) - path to the root of the repo
191190
* `repoOptions` (object, optional) - object containing `IPFSRepo` options, that should be used to construct the datastore instance.
192-
* `isBrowser` (bool) - indicates if the migration is run in a browser environment (as opposed to NodeJS)
193191

194192
### Browser vs. NodeJS environments
195193

196-
The migration might need to distinguish in which environment it runs (browser vs. NodeJS). For this reason there is an argument
197-
`isBrowser` passed to migrations functions. But with simple migrations it should not be necessary to distinguish between
194+
The migration might need to perform specific tasks in browser or NodeJS environment. In such a case create
195+
migration file `/migrations/migration-<number>/index_browser.js` which have to follow the same API is described before.
196+
Then add entry in `package.json` to the `browser` field as follow:
197+
198+
```
199+
'./migrations/migration-<number>/index.js': './migrations/migration-<number>/index_browser.js'
200+
```
201+
202+
In browser environments then `index.js` will be replaced with `index_browser.js`.
203+
204+
Simple migrations should not need to distinguish between
198205
these environments as the datastore implementation will handle the main differences.
199206

200207
There are currently two main datastore implementations:

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"datastore-level": "~0.12.1",
5050
"debug": "^4.1.0",
5151
"interface-datastore": "~0.8.0",
52-
"is-electron": "^2.2.0",
5352
"proper-lockfile": "^4.1.1",
5453
"yargs": "^14.2.0",
5554
"yargs-promise": "^1.1.0"

src/index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ const defaultMigrations = require('../migrations')
44
const repoVersion = require('./repo/version')
55
const repoLock = require('./repo/lock')
66
const errors = require('./errors')
7-
const isElectron = require('is-electron')
8-
9-
const IS_ENV_WITH_DOM = typeof window === 'object' && typeof document === 'object' && document.nodeType === 9
10-
const IS_ELECTRON = isElectron()
11-
const IS_BROWSER = IS_ENV_WITH_DOM && !IS_ELECTRON
127

138
const log = require('debug')('repo-migrations:migrator')
149

@@ -98,7 +93,7 @@ async function migrate (path, toVersion, { ignoreLock = false, repoOptions, onPr
9893
counter++
9994
log(`Migrating version ${migration.version}`)
10095
try {
101-
if (!isDryRun) await migration.migrate(path, repoOptions, IS_BROWSER)
96+
if (!isDryRun) await migration.migrate(path, repoOptions)
10297
} catch (e) {
10398
const lastSuccessfullyMigratedVersion = migration.version - 1
10499
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
188183
counter++
189184
log(`Reverting migration version ${migration.version}`)
190185
try {
191-
if (!isDryRun) await migration.revert(path, repoOptions, IS_BROWSER)
186+
if (!isDryRun) await migration.revert(path, repoOptions)
192187
} catch (e) {
193188
const lastSuccessfullyRevertedVersion = migration.version
194189
log(`An exception was raised during execution of migration. Setting the repo's version to last successfully reverted version: ${lastSuccessfullyRevertedVersion}`)

0 commit comments

Comments
 (0)