index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _extend2 = require('lodash/extend');
  4. var _extend3 = _interopRequireDefault(_extend2);
  5. var _each2 = require('lodash/each');
  6. var _each3 = _interopRequireDefault(_each2);
  7. var _template2 = require('lodash/template');
  8. var _template3 = _interopRequireDefault(_template2);
  9. var _bind2 = require('lodash/bind');
  10. var _bind3 = _interopRequireDefault(_bind2);
  11. var _map2 = require('lodash/map');
  12. var _map3 = _interopRequireDefault(_map2);
  13. var _includes2 = require('lodash/includes');
  14. var _includes3 = _interopRequireDefault(_includes2);
  15. var _filter2 = require('lodash/filter');
  16. var _filter3 = _interopRequireDefault(_filter2);
  17. var _fs = require('fs');
  18. var _fs2 = _interopRequireDefault(_fs);
  19. var _path = require('path');
  20. var _path2 = _interopRequireDefault(_path);
  21. var _mkdirp = require('mkdirp');
  22. var _mkdirp2 = _interopRequireDefault(_mkdirp);
  23. var _bluebird = require('bluebird');
  24. var _bluebird2 = _interopRequireDefault(_bluebird);
  25. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  26. // The new seeds we're performing, typically called from the `knex.seed`
  27. // interface on the main `knex` object. Passes the `knex` instance performing
  28. // the seeds.
  29. // Seeder
  30. // -------
  31. function Seeder(knex) {
  32. this.knex = knex;
  33. this.config = this.setConfig(knex.client.config.seeds);
  34. }
  35. // Runs all seed files for the given environment.
  36. Seeder.prototype.run = _bluebird2.default.method(function (config) {
  37. this.config = this.setConfig(config);
  38. return this._seedData().bind(this).spread(function (all) {
  39. return this._runSeeds(all);
  40. });
  41. });
  42. // Creates a new seed file, with a given name.
  43. Seeder.prototype.make = function (name, config) {
  44. this.config = this.setConfig(config);
  45. if (!name) _bluebird2.default.rejected(new Error('A name must be specified for the generated seed'));
  46. return this._ensureFolder(config).bind(this).then(this._generateStubTemplate).then(this._writeNewSeed(name));
  47. };
  48. // Lists all available seed files as a sorted array.
  49. Seeder.prototype._listAll = _bluebird2.default.method(function (config) {
  50. this.config = this.setConfig(config);
  51. return _bluebird2.default.promisify(_fs2.default.readdir, { context: _fs2.default })(this._absoluteConfigDir()).bind(this).then(function (seeds) {
  52. return (0, _filter3.default)(seeds, function (value) {
  53. var extension = _path2.default.extname(value);
  54. return (0, _includes3.default)(['.co', '.coffee', '.eg', '.iced', '.js', '.litcoffee', '.ls', '.ts'], extension);
  55. }).sort();
  56. });
  57. });
  58. // Gets the seed file list from the specified seed directory.
  59. Seeder.prototype._seedData = function () {
  60. return _bluebird2.default.join(this._listAll());
  61. };
  62. // Ensures a folder for the seeds exist, dependent on the
  63. // seed config settings.
  64. Seeder.prototype._ensureFolder = function () {
  65. var dir = this._absoluteConfigDir();
  66. return _bluebird2.default.promisify(_fs2.default.stat, { context: _fs2.default })(dir).catch(function () {
  67. return _bluebird2.default.promisify(_mkdirp2.default)(dir);
  68. });
  69. };
  70. // Run seed files, in sequence.
  71. Seeder.prototype._runSeeds = function (seeds) {
  72. return _bluebird2.default.all((0, _map3.default)(seeds, (0, _bind3.default)(this._validateSeedStructure, this))).bind(this).then(function (seeds) {
  73. return _bluebird2.default.bind(this).then(function () {
  74. return this._waterfallBatch(seeds);
  75. });
  76. });
  77. };
  78. // Validates seed files by requiring and checking for a `seed` function.
  79. Seeder.prototype._validateSeedStructure = function (name) {
  80. var seed = require(_path2.default.join(this._absoluteConfigDir(), name));
  81. if (typeof seed.seed !== 'function') {
  82. throw new Error('Invalid seed file: ' + name + ' must have a seed function');
  83. }
  84. return name;
  85. };
  86. // Generates the stub template for the current seed file, returning a compiled template.
  87. Seeder.prototype._generateStubTemplate = function () {
  88. var stubPath = this.config.stub || _path2.default.join(__dirname, 'stub', this.config.extension + '.stub');
  89. return _bluebird2.default.promisify(_fs2.default.readFile, { context: _fs2.default })(stubPath).then(function (stub) {
  90. return (0, _template3.default)(stub.toString(), { variable: 'd' });
  91. });
  92. };
  93. // Write a new seed to disk, using the config and generated filename,
  94. // passing any `variables` given in the config to the template.
  95. Seeder.prototype._writeNewSeed = function (name) {
  96. var config = this.config;
  97. var dir = this._absoluteConfigDir();
  98. return function (tmpl) {
  99. if (name[0] === '-') name = name.slice(1);
  100. var filename = name + '.' + config.extension;
  101. return _bluebird2.default.promisify(_fs2.default.writeFile, { context: _fs2.default })(_path2.default.join(dir, filename), tmpl(config.variables || {})).return(_path2.default.join(dir, filename));
  102. };
  103. };
  104. // Runs a batch of seed files.
  105. Seeder.prototype._waterfallBatch = function (seeds) {
  106. var knex = this.knex;
  107. var seedDirectory = this._absoluteConfigDir();
  108. var current = _bluebird2.default.bind({ failed: false, failedOn: 0 });
  109. var log = [];
  110. (0, _each3.default)(seeds, function (seed) {
  111. var name = _path2.default.join(seedDirectory, seed);
  112. seed = require(name);
  113. // Run each seed file.
  114. current = current.then(function () {
  115. return seed.seed(knex, _bluebird2.default);
  116. }).then(function () {
  117. log.push(name);
  118. });
  119. });
  120. return current.thenReturn([log]);
  121. };
  122. Seeder.prototype._absoluteConfigDir = function () {
  123. return _path2.default.resolve(process.cwd(), this.config.directory);
  124. };
  125. Seeder.prototype.setConfig = function (config) {
  126. return (0, _extend3.default)({
  127. extension: 'js',
  128. directory: './seeds'
  129. }, this.config || {}, config);
  130. };
  131. exports.default = Seeder;
  132. module.exports = exports['default'];