resetExampleRepos.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const Promise = require('bluebird');
  2. const path = require('path');
  3. const execAsync = require('./execAsync');
  4. const appRoot = path.normalize(__dirname + '/..');
  5. const isTesting = process.env.NODE_ENV === 'testing';
  6. const examplesDir = ! isTesting ? appRoot + '/exemples' :
  7. appRoot + '/test/integration/test-examples';
  8. // Promisified exec of git mv src dst
  9. function gitStatusAsync(src, dst) {
  10. var cmd = 'git status ' + examplesDir;
  11. return execAsync(cmd)
  12. // .then(({ stdout, stderr }) => {
  13. // // console.log(`stdout: ${stdout}`);
  14. // // console.log(`stderr: ${stderr}`);
  15. // })
  16. .catch(error => {
  17. console.error(`exec error: ${error}`);
  18. throw error;
  19. });
  20. }
  21. function getUntrackedReposAndExamples() {
  22. return gitStatusAsync()
  23. .then(({ stdout }) => {
  24. let idx = stdout.indexOf('Untracked files:');
  25. const output = stdout.substr(idx);
  26. let files = output.split('\n');
  27. files.splice(0, 3);
  28. idx = 0;
  29. while(files[idx]) idx++;
  30. files.splice(idx, files.length - idx);
  31. files.forEach((line, i) => {
  32. files[i] = line.trim();
  33. });
  34. return files;
  35. });
  36. }
  37. function resetExampleRepos() {
  38. return getUntrackedReposAndExamples()
  39. .then(files => Promise.map(files,
  40. file => execAsync('rm -r ' + appRoot + '/' + file)
  41. // file => console.log('rm -r ' + appRoot + '/' + file)
  42. ))
  43. }
  44. module.exports = resetExampleRepos;