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

Commit

Permalink
feat: using browser field for browsers environment
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau committed Oct 22, 2019
1 parent 662d89b commit 1474d5e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<number>/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-<number>/index.js': './migrations/migration-<number>/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:
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 2 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -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}`)
Expand Down

0 comments on commit 1474d5e

Please sign in to comment.