/
Seeder.js
198 lines (178 loc) · 5.28 KB
/
Seeder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Seeder
// -------
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const Bluebird = require('bluebird');
const {
filter,
includes,
map,
bind,
template,
each,
extend,
} = require('lodash');
const { writeJsFileUsingTemplate } = require('../util/template');
// The new seeds we're performing, typically called from the `knex.seed`
// interface on the main `knex` object. Passes the `knex` instance performing
// the seeds.
class Seeder {
constructor(knex) {
this.knex = knex;
this.config = this.setConfig(knex.client.config.seeds);
}
// Runs seed files for the given environment.
async run(config) {
this.config = this.setConfig(config);
const [all] = await this._seedData();
const files =
config && config.specific
? all.filter((file) => file === config.specific)
: all;
return this._runSeeds(files);
}
// Creates a new seed file, with a given name.
async make(name, config) {
this.config = this.setConfig(config);
if (!name)
throw new Error('A name must be specified for the generated seed');
await this._ensureFolder(config);
const seedPath = await this._writeNewSeed(name);
return seedPath;
}
// Lists all available seed files as a sorted array.
async _listAll(config) {
this.config = this.setConfig(config);
const loadExtensions = this.config.loadExtensions;
return Bluebird.promisify(fs.readdir, { context: fs })(
this._absoluteConfigDir()
)
.bind(this)
.then((seeds) =>
filter(seeds, function(value) {
const extension = path.extname(value);
return includes(loadExtensions, extension);
}).sort()
);
}
// Gets the seed file list from the specified seed directory.
_seedData() {
return Bluebird.join(this._listAll());
}
// Ensures a folder for the seeds exist, dependent on the
// seed config settings.
_ensureFolder() {
const dir = this._absoluteConfigDir();
return Bluebird.promisify(fs.stat, { context: fs })(dir).catch(() =>
Bluebird.promisify(mkdirp)(dir)
);
}
// Run seed files, in sequence.
_runSeeds(seeds) {
return Bluebird.all(map(seeds, bind(this._validateSeedStructure, this)))
.bind(this)
.then(function(seeds) {
return Bluebird.bind(this).then(function() {
return this._waterfallBatch(seeds);
});
});
}
// Validates seed files by requiring and checking for a `seed` function.
_validateSeedStructure(name) {
const seed = require(path.join(this._absoluteConfigDir(), name));
if (typeof seed.seed !== 'function') {
throw new Error(`Invalid seed file: ${name} must have a seed function`);
}
return name;
}
_getStubPath() {
return (
this.config.stub ||
path.join(__dirname, 'stub', this.config.extension + '.stub')
);
}
// Generates the stub template for the current seed file, returning a compiled template.
_generateStubTemplate() {
const stubPath = this._getStubPath();
return Bluebird.promisify(fs.readFile, { context: fs })(stubPath).then(
(stub) => template(stub.toString(), { variable: 'd' })
);
}
_getNewStubFileName(name) {
if (name[0] === '-') name = name.slice(1);
return name + '.' + this.config.extension;
}
_getNewStubFilePath(name) {
return path.join(this._absoluteConfigDir(), this._getNewStubFileName(name));
}
// Write a new seed to disk, using the config and generated filename,
// passing any `variables` given in the config to the template.
async _writeNewSeed(name) {
const seedPath = this._getNewStubFilePath(name);
await writeJsFileUsingTemplate(
seedPath,
this._getStubPath(),
{ variable: 'd' },
this.config.variables || {}
);
return seedPath;
}
// Runs a batch of seed files.
_waterfallBatch(seeds) {
const { knex } = this;
const seedDirectory = this._absoluteConfigDir();
let current = Bluebird.bind({ failed: false, failedOn: 0 });
const log = [];
each(seeds, (seed) => {
const name = path.join(seedDirectory, seed);
seed = require(name);
// Run each seed file.
current = current.then(() =>
// Nesting promise to prevent bubbling up of error on catch
Promise.resolve()
.then(() => seed.seed(knex))
.then(() => log.push(name))
.catch((originalError) => {
const error = new Error(
`Error while executing "${name}" seed: ${originalError.message}`
);
error.original = originalError;
error.stack =
error.stack
.split('\n')
.slice(0, 2)
.join('\n') +
'\n' +
originalError.stack;
throw error;
})
);
});
return current.then(() => [log]);
}
_absoluteConfigDir() {
return path.resolve(process.cwd(), this.config.directory);
}
setConfig(config) {
return extend(
{
extension: 'js',
directory: './seeds',
loadExtensions: [
'.co',
'.coffee',
'.eg',
'.iced',
'.js',
'.litcoffee',
'.ls',
'.ts',
],
},
this.config || {},
config
);
}
}
module.exports = Seeder;