Skip to content

Commit 7cf6f71

Browse files
committed
feat(staticLoader): a static loader to support packaging
Signed-off-by: Tobias Gurtzick <[email protected]>
1 parent 7ae7125 commit 7cf6f71

File tree

6 files changed

+54
-10
lines changed

6 files changed

+54
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
migrations/
22
node_modules/
3+
lib/commands/generated.js
34
.tern-port
45
VCSeeder/
56
Seeder/

api.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
module.exports.version = require('./package.json').version;
44

5-
var load = require('./lib/commands');
5+
let load;
66
var log = require('db-migrate-shared').log;
77
var Promise;
8-
var onComplete = load('on-complete');
8+
let onComplete;
99
var config = require('rc')('db-migrate');
1010

1111
// constant hooks for this file
@@ -19,18 +19,25 @@ var APIHooks = {
1919
};
2020

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

2530
this.internals = {
2631
onComplete: onComplete,
27-
migrationProtocol: 1
32+
migrationProtocol: 1,
33+
load
2834
};
2935
if (typeof isModule !== 'function') {
3036
this.internals.isModule = isModule;
3137
}
3238
var internals = this.internals;
3339

40+
console.log(load('fn/plugin'));
3441
this.internals.plugins = load('fn/plugin')(plugins);
3542

3643
if (typeof callback === 'function') this.internals.onComplete = callback;

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
@@ -64,15 +64,12 @@ function loadPlugins (options) {
6464
return hooks;
6565
}
6666

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

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

lib/commands/run.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
var log = require('db-migrate-shared').log;
44
var yargs = require('yargs');
5-
var load = require('./');
6-
var transition = load('transition');
75

86
function run (internals, config) {
7+
const { load } = internals;
8+
const transition = load('transition');
9+
console.log(load);
910
var action = internals.argv._.shift();
1011
var folder = action.split(':');
1112

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"sinon": "^9.0.2"
7878
},
7979
"scripts": {
80+
"prepublishOnly": "node generateLoader.js",
8081
"pretest": "eslint *.js lib/*.js",
8182
"test": "lab",
8283
"test:coverage": "lab -r html -o coverage.html"

0 commit comments

Comments
 (0)