Comparing version 2.0.0-alpha.1 to 2.0.0-alpha.2
@@ -116,7 +116,18 @@ 'use strict'; | ||
if (!initialized) { | ||
throw new Error( | ||
`Migrations directory: ${migrationParams.dir} doesn't exist. ` + | ||
'You should run `init` command to initialize migrations or change ' + | ||
'`dir` option.' | ||
); | ||
const {dir, sourceDir} = migrationParams; | ||
if (sourceDir === dir) { | ||
throw new Error( | ||
`Migrations directory: ${dir} doesn't exist. ` + | ||
'You should run `init` command to initialize migrations or change ' + | ||
'`dir` option.' | ||
); | ||
} else { | ||
throw new Error( | ||
`Migration executable dir "${dir}" or ` + | ||
`source dir "${sourceDir}" dosn't exist. ` + | ||
'You should run "init" command to initialize migrations or ' + | ||
'change "dir", "sourceDir" options.' | ||
); | ||
} | ||
} | ||
@@ -123,0 +134,0 @@ |
'use strict'; | ||
module.exports = | ||
function _getMigrationFileTypeParams(migrationFileType) { | ||
if (migrationFileType === 'executable') { | ||
return { | ||
dir: this.params.dir, | ||
extension: this.params.migrationExtension | ||
}; | ||
} | ||
if (migrationFileType === 'source') { | ||
return { | ||
dir: this.params.sourceDir, | ||
extension: this.params.sourceMigrationExtension | ||
}; | ||
} | ||
module.exports = function _getMigrationFileTypeParams(migrationFileType) { | ||
if (migrationFileType === 'executable') { | ||
const {dir, migrationExtension: extension} = this.params; | ||
return {dir, extension}; | ||
} else if (migrationFileType === 'source') { | ||
const {sourceDir: dir, sourceMigrationExtension: extension} = this.params; | ||
return {dir, extension}; | ||
} else { | ||
throw new Error( | ||
"Invalid migration file type, expected 'executable' or 'source' " + | ||
`but got '${migrationFileType}'` | ||
'Invalid migration file type, expected "executable" or "source" ' + | ||
`but got "${migrationFileType}"` | ||
); | ||
}; | ||
} | ||
}; |
@@ -6,27 +6,28 @@ 'use strict'; | ||
module.exports = | ||
function getAllMigrationNames(migrationFileType = 'executable') { | ||
return Promise.resolve() | ||
.then(() => { | ||
const {dir} = this._getMigrationFileTypeParams(migrationFileType); | ||
module.exports = function getAllMigrationNames( | ||
migrationFileType = 'executable' | ||
) { | ||
return Promise.resolve() | ||
.then(() => { | ||
const {dir} = this._getMigrationFileTypeParams(migrationFileType); | ||
return fse.readdir(dir); | ||
}) | ||
.then((paths) => { | ||
const {extension} = this._getMigrationFileTypeParams(migrationFileType); | ||
const names = paths | ||
.filter((path) => { | ||
const file = pathUtils.parse(path); | ||
return fse.readdir(dir); | ||
}) | ||
.then((paths) => { | ||
const {extension} = this._getMigrationFileTypeParams(migrationFileType); | ||
const names = paths | ||
.filter((path) => { | ||
const file = pathUtils.parse(path); | ||
return file.ext === `.${extension}` && this._nameRegExp.test(file.name); | ||
}) | ||
.sort((fileNameOne, fileNameTwo) => { | ||
return ( | ||
this._getNumber(fileNameOne) - this._getNumber(fileNameTwo) | ||
); | ||
}) | ||
.map((path) => this.getMigrationNameByPath(path, migrationFileType)); | ||
return file.ext === `.${extension}` && this._nameRegExp.test(file.name); | ||
}) | ||
.sort((fileNameOne, fileNameTwo) => { | ||
return ( | ||
this._getNumber(fileNameOne) - this._getNumber(fileNameTwo) | ||
); | ||
}) | ||
.map((path) => this.getMigrationNameByPath(path, migrationFileType)); | ||
return names; | ||
}); | ||
}; | ||
return names; | ||
}); | ||
}; |
@@ -5,6 +5,8 @@ 'use strict'; | ||
module.exports = | ||
function getMigrationNameByPath(path, migrationFileType = 'executable') { | ||
const {extension} = this._getMigrationFileTypeParams(migrationFileType); | ||
return pathUtils.basename(path, `.${extension}`); | ||
}; | ||
module.exports = function getMigrationNameByPath( | ||
path, | ||
migrationFileType = 'executable' | ||
) { | ||
const {extension} = this._getMigrationFileTypeParams(migrationFileType); | ||
return pathUtils.basename(path, `.${extension}`); | ||
}; |
@@ -5,6 +5,8 @@ 'use strict'; | ||
module.exports = | ||
function getMigrationPathByName(name, migrationFileType = 'executable') { | ||
const {dir, extension} = this._getMigrationFileTypeParams(migrationFileType); | ||
return pathUtils.resolve(pathUtils.join(dir, `${name}.${extension}`)); | ||
}; | ||
module.exports = function getMigrationPathByName( | ||
name, | ||
migrationFileType = 'executable' | ||
) { | ||
const {dir, extension} = this._getMigrationFileTypeParams(migrationFileType); | ||
return pathUtils.resolve(pathUtils.join(dir, `${name}.${extension}`)); | ||
}; |
@@ -25,3 +25,2 @@ 'use strict'; | ||
exports.getMigrationNameByPath = require('./getMigrationNameByPath'); | ||
exports._getMigrationFileTypeParams = require('./_getMigrationFileTypeParams'); | ||
exports.separateNames = require('./separateNames'); | ||
@@ -55,1 +54,2 @@ exports.normalizeNames = require('./normalizeNames'); | ||
exports._filterMigrationNamesByTag = require('./_filterMigrationNamesByTag'); | ||
exports._getMigrationFileTypeParams = require('./_getMigrationFileTypeParams'); |
@@ -8,22 +8,29 @@ 'use strict'; | ||
.then(() => { | ||
return Promise.all([this.isDirExists(), this.isDirExists('source')]); | ||
return Promise.all([ | ||
this.isDirExists('executable'), | ||
this.isDirExists('source') | ||
]); | ||
}) | ||
.then(([dirExists, sourceDirExists]) => { | ||
const migrationsDir = this.params.dir; | ||
const sourcesDir = this.params.sourceDir; | ||
const {dir, sourceDir} = this.params; | ||
if (dirExists) { | ||
throw new Error( | ||
`Migration executables directory "${migrationsDir}" already exists` | ||
); | ||
if (dirExists && sourceDirExists) { | ||
if (sourceDir === dir) { | ||
throw new Error(`Migration directory "${dir}" already exists`); | ||
} else { | ||
throw new Error( | ||
`Migration executables directory "${dir}" and sources` + | ||
`directory "${sourceDir}" already exist` | ||
); | ||
} | ||
} | ||
if (sourceDirExists) { | ||
throw new Error( | ||
`Migration sources directory "${sourcesDir}" already exists` | ||
); | ||
} | ||
return migrationsDir === sourcesDir | ||
? fse.mkdir(migrationsDir) | ||
: Promise.all([fse.mkdir(migrationsDir), fse.mkdir(sourcesDir)]); | ||
// create unexisting paths but only once (even dirs are equal) | ||
const pathsSet = new Set(); | ||
if (!dirExists) pathsSet.add(dir); | ||
if (!sourceDirExists) pathsSet.add(sourceDir); | ||
const paths = Array.from(pathsSet.values()); | ||
return Promise.all(paths.map((path) => fse.mkdir(path))); | ||
}); | ||
}; |
@@ -5,10 +5,11 @@ 'use strict'; | ||
module.exports = | ||
function isMigrationExists(name, migrationFileType = 'executable') { | ||
return Promise.resolve() | ||
.then(() => { | ||
const path = this.getMigrationPathByName(name, migrationFileType); | ||
return fse.pathExists(path); | ||
}); | ||
}; | ||
module.exports = function isMigrationExists( | ||
name, | ||
migrationFileType = 'executable' | ||
) { | ||
return Promise.resolve() | ||
.then(() => { | ||
const path = this.getMigrationPathByName(name, migrationFileType); | ||
return fse.pathExists(path); | ||
}); | ||
}; |
@@ -9,3 +9,3 @@ 'use strict'; | ||
return Promise.all([ | ||
this.isMigrationExists(name), | ||
this.isMigrationExists(name, 'executable'), | ||
this.isMigrationExists(name, 'source') | ||
@@ -15,14 +15,14 @@ ]); | ||
.then(([executableExists, sourceExists]) => { | ||
const filesToDelete = new Set(); | ||
const pathsSet = new Set(); | ||
if (executableExists) { | ||
filesToDelete.add(this.getMigrationPathByName(name)); | ||
pathsSet.add(this.getMigrationPathByName(name, 'executable')); | ||
} | ||
if (sourceExists) { | ||
filesToDelete.add(this.getMigrationPathByName(name, 'source')); | ||
pathsSet.add(this.getMigrationPathByName(name, 'source')); | ||
} | ||
return Promise.all( | ||
[...filesToDelete.values()].map((path) => fse.unlink(path)) | ||
); | ||
const paths = Array.from(pathsSet.values()); | ||
return Promise.all(paths.map((path) => fse.unlink(path))); | ||
}); | ||
}; |
{ | ||
"name": "east", | ||
"description": "node.js database migration tool for mongodb, sqlite, postgres, mysql, couchbase", | ||
"version": "2.0.0-alpha.1", | ||
"version": "2.0.0-alpha.2", | ||
"author": "Oleg Korobenko <oleg.korobenko@gmail.com>", | ||
@@ -31,4 +31,5 @@ "license": "MIT", | ||
"lintChangelog": "remark --rc-path .remarkrc-lintChangelog.js --frail > /dev/null CHANGELOG.md", | ||
"lintTsTypes": "tsc lib/index.d.ts", | ||
"lintJs": "eslint ./", | ||
"lint": "npm run lintChangelog && npm run lintJs" | ||
"lint": "npm run lintChangelog && npm run lintTsTypes && npm run lintJs" | ||
}, | ||
@@ -63,3 +64,4 @@ "bin": { | ||
"tap": "12.7.0", | ||
"ts-node": "8.8.1" | ||
"ts-node": "8.8.2", | ||
"typescript": "3.8.3" | ||
}, | ||
@@ -66,0 +68,0 @@ "engines": { |
@@ -471,7 +471,8 @@ # east | ||
it doesn't work on the other way around, e.g. if you specify | ||
``` | ||
--sourceDir mySourceDir --sourceMigrationExtension ts | ||
``` | ||
then `--dir` and | ||
`--migrationExtension` will habe `migrations` and `js` values by default, | ||
then `--dir` and `--migrationExtension` will have `migrations` and `js` values by default, | ||
so it is recommended to specify at least `--dir`, `--sourceDir` and `--sourceMigrationExtension` | ||
@@ -482,12 +483,18 @@ when you are building a transpiled language. | ||
if you don't want to transpile you migration scripts before running them: | ||
```sh | ||
ts-node $(which east) migrate | ||
``` | ||
Just be sure to specify `--migrationExtension ts` so that `east` does look for | ||
TypeScript files when `require()`-ing the migration scripts. | ||
### TypeScript typings | ||
`east` exposes TypeScript declarations of the `Adapter` interface. | ||
`east` exposes TypeScript declarations of the `Adapter`, `MigrationManager` | ||
and other related interfaces. | ||
You can access it by importing the interfaces from `east` module itself: | ||
```ts | ||
@@ -494,0 +501,0 @@ import { DbClient } from 'some-mainstream-db'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83897
1865
518
14