Advertisement
Guest User

Untitled

a guest
May 5th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. var config = require('../config'),
  2. gulp = require('gulp'),
  3. fs = require('graceful-fs'),
  4. path = require('path'),
  5. spawn = require('child_process').spawn,
  6. exec = require('child_process').exec,
  7. concat = require('concat-stream'),
  8. minimist = require('minimist'),
  9. fromString = require('from2-string'),
  10. recursive = require('recursive-readdir'),
  11. Client = require('ssh2').Client;
  12.  
  13. var state = {
  14. filesRemaining: 0,
  15. sftp: null,
  16. hash: {
  17. production: '',
  18. local: ''
  19. },
  20. includes: []
  21. }
  22.  
  23. var argv = minimist(process.argv.slice(2), {
  24. alias: {
  25. h: 'host',
  26. p: 'port',
  27. u: 'username',
  28. 'pass': 'password'
  29. }
  30. });
  31.  
  32. function ftpDeployTask() {
  33. var conn = new Client();
  34. conn.on('ready', function() {
  35. console.log('sftp client connected')
  36. conn.sftp(sftpConnected);
  37. }).connect({
  38. host: argv.host,
  39. port: argv.port,
  40. username: argv.username,
  41. password: argv.password
  42. });
  43. }
  44.  
  45. function sftpConnected(err, sftp) {
  46. if (err) throw err;
  47. state.sftp = sftp;
  48. var stream = sftp.createReadStream('.revisionblah');
  49. stream.on('error', function (err) {
  50. console.error(err, '.revision not found');
  51. recursive('public_html', ['.git', 'node_modules'], function (err, files) {
  52. state.filesRemaining = files.length
  53. console.log(state)
  54. var localCommit = spawn('git', ['rev-parse', 'HEAD']);
  55. localCommit.stdout.on('data', function (buf) {
  56. var hash = buf.toString()
  57. state.hash.local = hash
  58. console.log('hash', hash)
  59. files.forEach(function (file) {
  60. if (file.indexOf('.git') >= 0 || file.indexOf('node_modules') >= 0) {
  61. console.log('skipping', file)
  62. return;
  63. }
  64. if (file.indexOf('/') >= -1) {
  65. var dir = path.dirname(file);
  66. var i = 0;
  67. var parents = dir.split('/');
  68. var parent = parents[i];
  69. sftpMkdirp(dir, parents, parent, file, i);
  70. } else {
  71. sftpFile(file);
  72. }
  73. })
  74. })
  75. })
  76. })
  77. stream.pipe(concat(handleRevision));
  78. var includes = argv.include.split(',')
  79. includes.forEach(function (file) {
  80. state.includes.push(file)
  81. if (file.indexOf('/') >= 0) {
  82. var dir = file.split('/')[1]
  83. var i = 0;
  84. var parents = dir.split('/');
  85. var parent = parents[i];
  86. sftpMkdirp(dir, parents, parent, file, i, function () {
  87. console.log('cb fired!');
  88. recursive(dir, function (err, files) {
  89. files.forEach(function (file) {
  90. state.filesRemaining++;
  91. if (file.indexOf('.git') >= 0 || file.indexOf('node_modules') >= 0) {
  92. console.log('skipping', file)
  93. return;
  94. }
  95. if (file.indexOf('/') >= 0) {
  96. var dir = path.dirname(file);
  97. var i = 0;
  98. var parents = dir.split('/');
  99. var parent = parents[i];
  100. sftpMkdirp(dir, parents, parent, file, i);
  101. } else {
  102. sftpFile(file);
  103. }
  104. })
  105. })
  106. });
  107. } else {
  108. state.filesRemaining++
  109. sftpFile(file);
  110. }
  111. })
  112. }
  113.  
  114. function handleRevision(buf) {
  115. state.hash.production = buf.toString();
  116. var localCommit = spawn('git', ['rev-parse', 'HEAD']);
  117. localCommit.stdout.pipe(concat(compareHashes));
  118. }
  119.  
  120. function compareHashes (buf) {
  121. var hash = buf.toString();
  122. state.hash.local = hash;
  123. if (state.hash.local !== state.hash.production) {
  124. var diff = 'git diff --name-status ' +
  125. state.hash.production + ' ' + hash;
  126. exec(diff, sftpDiffedFiles);
  127. }
  128. }
  129.  
  130. function sftpDiffedFiles(err, stdout, stderr) {
  131. var files = stdout.split('\n');
  132. files.pop();
  133. state.filesRemaining = files.length;
  134. files.forEach(getFileStat);
  135. }
  136.  
  137. function getFileStat(stat) {
  138. var file = stat.split('\t')[1];
  139. var status = stat.split('\t')[0];
  140. if (status !== 'D') {
  141. if (file.indexOf('/') >= 0) {
  142. var dir = path.dirname(file);
  143. var i = 0;
  144. var parents = dir.split('/');
  145. var parent = parents[i];
  146. sftpMkdirp(dir, parents, parent, file, i);
  147. } else {
  148. sftpFile(file);
  149. }
  150. }
  151. }
  152.  
  153. function sftpMkdirp(dir, parents, currentParent, file, index, cb) {
  154. var hasPublicHtml = parents.indexOf('public_html') === 0
  155. if (hasPublicHtml) {
  156. parents.shift()
  157. currentParent = parents[0]
  158. dir = dir.replace('public_html/','');
  159. console.log(dir)
  160. }
  161. state.sftp.opendir(dir, function (err, buf) {
  162. if (err) {
  163. // parent(s) don't exist
  164. state.sftp.mkdir(currentParent, function (err) {
  165. if (err) console.error(err);
  166. index++;
  167. currentParent += '/' + parents[index];
  168. sftpMkdirp(dir, parents, currentParent, file, index);
  169. })
  170. } else {
  171. if (cb) {
  172. cb()
  173. } else sftpFile(file);
  174. }
  175. })
  176. }
  177.  
  178. function sftpFile(file) {
  179. console.log('sftp', file)
  180. if (file.indexOf('node_modules') >= 0 || file.indexOf('.git') >= 0) {
  181. console.log('skipping', file)
  182. return;
  183. }
  184. var isInPublicHtml = file.indexOf('public_html') >= 0
  185. var isInIncludes = argv.includes && state.includes.indexOf(file) >= 0
  186. if (isInPublicHtml || isInIncludes) {
  187. var destFile = isInPublicHtml
  188. ? file.replace('public_html/', '')
  189. : file
  190.  
  191. var rs = fs.createReadStream(file);
  192. var ws = state.sftp.createWriteStream(destFile);
  193.  
  194. rs.pipe(ws);
  195. ws.on('error', function(err) {
  196. console.error(err, 'file:', destFile)
  197. })
  198. ws.on('finish', function() {
  199. console.log(destFile, 'is sftp\'d!');
  200. // state.filesRemaining--;
  201. if (!state.filesRemaining) {
  202. fromString(state.hash.production)
  203. .pipe(state.sftp.createWriteStream('.revision-old'))
  204. fromString(state.hash.local)
  205. .pipe(state.sftp.createWriteStream('.revision'))
  206. console.log('.revision updated:', state.hash.local)
  207. }
  208. })
  209. }
  210. }
  211.  
  212. gulp.task('deploy', ftpDeployTask);
  213. module.exports = ftpDeployTask;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement