Skip to content

Commit e183046

Browse files
committed
feat(staticLoader): a static loader to support packaging
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
1 parent cc19a2b commit e183046

File tree

6 files changed

+63
-17
lines changed

6 files changed

+63
-17
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
migrations/
22
node_modules/
3+
lib/commands/generated.js
4+
.tern-port
35
VCSeeder/
46
Seeder/
57
*.db
@@ -11,6 +13,7 @@ database.json
1113
*.sublime-workspace
1214
archive
1315
.db-migraterc
16+
coverage.html
1417

1518
# Vim swap files
1619
.*.sw[a-z]

api.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
22

3-
var load = require('./lib/commands');
3+
module.exports.version = require('./package.json').version;
4+
5+
let load;
46
var log = require('db-migrate-shared').log;
5-
require('pkginfo')(module, 'version'); // jshint ignore:line
67
var Promise;
7-
var onComplete = load('on-complete');
8+
let onComplete;
89

910
// constant hooks for this file
1011
var APIHooks = {
@@ -17,12 +18,18 @@ var APIHooks = {
1718
};
1819

1920
function dbmigrate (plugins, isModule, options, callback) {
21+
if (!options.staticLoader) load = require('./lib/commands');
22+
else load = require('./lib/commands/generated.js');
23+
24+
onComplete = load('on-complete');
25+
2026
var dotenv = require('dotenv');
2127
var setDefaultArgv = load('set-default-argv');
2228

2329
this.internals = {
2430
onComplete: onComplete,
25-
migrationProtocol: 1
31+
migrationProtocol: 1,
32+
load
2633
};
2734
if (typeof isModule !== 'function') {
2835
this.internals.isModule = isModule;

generateLoader.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const recursive = require('final-fs').readdirRecursive;
5+
const start = path.join(__dirname, 'lib/commands');
6+
const Promise = require('bluebird');
7+
const fs = require('fs');
8+
9+
(async () => {
10+
const files = await recursive(start, true);
11+
const template = `
12+
'use strict';
13+
14+
const path = require('path');
15+
16+
const files = {
17+
${files
18+
.map(x => ` "${x.substring(0, x.indexOf('.js'))}": require('./${x}')`)
19+
.join(',\n')}
20+
}
21+
22+
function register (module) {
23+
return files[module];
24+
}
25+
26+
module.exports = register;
27+
`;
28+
29+
fs.writeFile(
30+
path.join(__dirname, 'lib/commands/generated.js'),
31+
template,
32+
'utf8',
33+
err => {
34+
if (err) throw err;
35+
}
36+
);
37+
})();

index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,12 @@ function loadPlugins (options) {
6363
return hooks;
6464
}
6565

66-
module.exports.getInstance = function (
67-
isModule,
68-
options = { cwd: process.cwd() },
69-
callback
70-
) {
66+
module.exports.getInstance = function (isModule, options = {}, callback) {
7167
delete require.cache[require.resolve('./api.js')];
7268
delete require.cache[require.resolve('optimist')];
7369
var Mod = require('./api.js');
7470
var plugins = {};
71+
options.cwd = options.cwd || process.cwd();
7572

7673
try {
7774
if (!options || !options.noPlugins) plugins = loadPlugins(options);

lib/commands/run.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
22

33
var log = require('db-migrate-shared').log;
4-
var optimist = require('optimist');
5-
var load = require('./');
6-
var transition = load('transition');
4+
var yargs = require('yargs');
5+
var transition;
76

87
function run (internals, config) {
8+
const { load } = internals;
9+
const transition = load('transition');
10+
console.log(load);
911
var action = internals.argv._.shift();
1012
var folder = action.split(':');
1113

@@ -117,7 +119,7 @@ function run (internals, config) {
117119
'Invalid Action: Must be [up|down|check|create|reset|sync|' +
118120
'db|transition].'
119121
);
120-
optimist.showHelp();
122+
yargs.showHelp();
121123
process.exit(1);
122124
}
123125
break;

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
],
3434
"version": "0.11.6",
3535
"engines": {
36-
"node": ">=0.6.0"
36+
"node": ">=8.0.0"
3737
},
3838
"bugs": {
3939
"url": "https://github.com/db-migrate/node-db-migrate/issues"
@@ -52,14 +52,13 @@
5252
"final-fs": "^1.6.0",
5353
"inflection": "^1.10.0",
5454
"mkdirp": "~0.5.0",
55-
"optimist": "~0.6.1",
5655
"parse-database-url": "~0.3.0",
57-
"pkginfo": "^0.4.0",
5856
"prompt": "^1.0.0",
5957
"rc": "^1.2.8",
6058
"resolve": "^1.1.6",
6159
"semver": "^5.3.0",
62-
"tunnel-ssh": "^4.0.0"
60+
"tunnel-ssh": "^4.0.0",
61+
"yargs": "^15.3.1"
6362
},
6463
"devDependencies": {
6564
"code": "^4.1.0",
@@ -78,6 +77,7 @@
7877
"sinon": "^4.1.2"
7978
},
8079
"scripts": {
80+
"prepublishOnly": "node generateLoader.js",
8181
"pretest": "eslint *.js lib/*.js test/*.js bin/*",
8282
"test": "make test-cov"
8383
}

0 commit comments

Comments
 (0)