|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var fs = require('fs'); |
| 4 | +var path = require('path'); |
| 5 | +var log = require('db-migrate-shared').log; |
| 6 | +var inflection = require('inflection'); |
| 7 | +var Promise = require('bluebird'); |
| 8 | +var lpad = require('db-migrate-shared').util.lpad; |
| 9 | + |
| 10 | +function formatPath (dir, name) { |
| 11 | + return path.join(dir, name); |
| 12 | +} |
| 13 | + |
| 14 | +function formatName (title, date) { |
| 15 | + return formatDate(date) + '-' + formatTitle(title); |
| 16 | +} |
| 17 | + |
| 18 | +function formatDate (date) { |
| 19 | + return [ |
| 20 | + date.getUTCFullYear(), |
| 21 | + lpad(date.getUTCMonth() + 1, '0', 2), |
| 22 | + lpad(date.getUTCDate(), '0', 2), |
| 23 | + lpad(date.getUTCHours(), '0', 2), |
| 24 | + lpad(date.getUTCMinutes(), '0', 2), |
| 25 | + lpad(date.getUTCSeconds(), '0', 2) |
| 26 | + ].join(''); |
| 27 | +} |
| 28 | + |
| 29 | +function formatTitle (title) { |
| 30 | + return inflection.dasherize(title); |
| 31 | +} |
| 32 | + |
| 33 | +function parseDate (name) { |
| 34 | + var date = new Date(); |
| 35 | + var match = name.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})-[^.]+/); |
| 36 | + date.setUTCFullYear(match[1]); |
| 37 | + date.setUTCDate(match[3]); |
| 38 | + date.setUTCMonth(match[2] - 1); |
| 39 | + date.setUTCHours(match[4]); |
| 40 | + date.setUTCMinutes(match[5]); |
| 41 | + date.setUTCSeconds(match[6]); |
| 42 | + return date; |
| 43 | +} |
| 44 | + |
| 45 | +function parseTitle (name) { |
| 46 | + var match = name.match(/\d{14}-([^.]+)/); |
| 47 | + var dashed = match[1]; |
| 48 | + return inflection.humanize(dashed, true); |
| 49 | +} |
| 50 | + |
| 51 | +var filesRegEx = /\.js$/; |
| 52 | + |
| 53 | +var File = { |
| 54 | + init: function () { |
| 55 | + if (arguments.length >= 3) { |
| 56 | + this.title = arguments[0]; |
| 57 | + this.date = arguments[2]; |
| 58 | + this.name = this.formatName(this.title, this.date); |
| 59 | + this.path = this.formatPath(arguments[1], this.name); |
| 60 | + this.templateType = arguments[3]; |
| 61 | + this.internals = arguments[4]; |
| 62 | + } else if (arguments.length === 2) { |
| 63 | + this.path = arguments[0]; |
| 64 | + this.name = this.parseName(this.path); |
| 65 | + this.date = this.parseDate(this.name); |
| 66 | + this.title = this.parseTitle(this.name); |
| 67 | + this.internals = arguments[1]; |
| 68 | + } |
| 69 | + |
| 70 | + this._super(this.internals); |
| 71 | + }, |
| 72 | + |
| 73 | + parseName: function (path) { |
| 74 | + var match = path.match(/(\d{14}-[^.]+)(?:\.*?)?/); |
| 75 | + return match[1]; |
| 76 | + }, |
| 77 | + |
| 78 | + parseTitle: parseTitle, |
| 79 | + parseDate: parseDate, |
| 80 | + formatTitle: formatTitle, |
| 81 | + formatPath: formatPath, |
| 82 | + formatName: formatName |
| 83 | +}; |
| 84 | + |
| 85 | +File.registerHook = function (Plugin, prefix, internals) { |
| 86 | + var plugin = Plugin.hook(prefix + ':hook:require'); |
| 87 | + internals.parser = internals.parser || { |
| 88 | + filesRegEx: filesRegEx, |
| 89 | + extensions: 'js' |
| 90 | + }; |
| 91 | + |
| 92 | + if (!plugin) { |
| 93 | + return Promise.resolve(null); |
| 94 | + } |
| 95 | + |
| 96 | + return Promise.resolve(plugin) |
| 97 | + .map(function (plugin) { |
| 98 | + return plugin[prefix + ':hook:require'](); |
| 99 | + }) |
| 100 | + .each(function (parser) { |
| 101 | + if (parser && parser.extensions) { |
| 102 | + internals.parser.extensions = |
| 103 | + internals.parser.extensions + '|' + parser.extensions; |
| 104 | + } |
| 105 | + }) |
| 106 | + .then(function () { |
| 107 | + internals.parser.filesRegEx = new RegExp( |
| 108 | + '\\.(' + internals.parser.extensions + ')$' |
| 109 | + ); |
| 110 | + |
| 111 | + return internals.parser; |
| 112 | + }); |
| 113 | +}; |
| 114 | + |
| 115 | +File.loadFromFileystem = function (dir, type, internals) { |
| 116 | + log.verbose('loading ' + type + ' from dir', dir); |
| 117 | + |
| 118 | + return fs |
| 119 | + .readdirAsync(dir) |
| 120 | + .filter(function (files) { |
| 121 | + return internals.parser.filesRegEx.test(files); |
| 122 | + }) |
| 123 | + .then(function (files) { |
| 124 | + return files.sort(); |
| 125 | + }) |
| 126 | + .map(function (file) { |
| 127 | + return new File(path.join(dir, file), internals); |
| 128 | + }); |
| 129 | +}; |
| 130 | + |
| 131 | +File.loadFromDatabase = function (dir, type, loader, internals) { |
| 132 | + log.verbose('loading ' + type + ' from database'); |
| 133 | + return loader() |
| 134 | + .catch(function (err) { |
| 135 | + if (internals.dryRun) { |
| 136 | + return []; |
| 137 | + } else { |
| 138 | + return Promise.reject(err); |
| 139 | + } |
| 140 | + }) |
| 141 | + .filter(function (result) { |
| 142 | + return ( |
| 143 | + result.name.substr(0, result.name.lastIndexOf('/')) === |
| 144 | + internals.matching |
| 145 | + ); |
| 146 | + }) |
| 147 | + .map(function (result) { |
| 148 | + return new File(path.join(dir, result.name), internals); |
| 149 | + }); |
| 150 | +}; |
| 151 | + |
| 152 | +Promise.promisifyAll(fs); |
| 153 | + |
| 154 | +module.exports = File; |
0 commit comments