Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

render_snap_preview.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. var page = require('webpage').create();
  2. var system = require('system');
  3. var fs = require('fs');
  4. var returnStatus = {};
  5. var rootPath = system.args[1];
  6. var staticPath = system.args[2];
  7. // Check if all paths are accessible
  8. // We assume at least read rights on all paths and files
  9. if (!fs.isDirectory(rootPath) || !fs.isDirectory(rootPath + '/' + staticPath) || !fs.isDirectory(rootPath + '/' + staticPath + 'styles/' + system.args[3] + '/') || !fs.isDirectory(rootPath + '/' + staticPath + '/stylespreview')) {
  10. //Incorrect paths, are they passed correctly?
  11. returnStatus.status = -1;
  12. console.log(JSON.stringify(returnStatus));
  13. phantom.exit();
  14. }
  15. // Switch to given stylesheet directory
  16. fs.changeWorkingDirectory(rootPath + '/' + staticPath + 'styles/' + system.args[3] + '/');
  17. if (!fs.exists('preview.html')) {
  18. // Preview file doesn't exist. Running things in the wrong order perhaps?
  19. returnStatus.status = -2;
  20. console.log(JSON.stringify(returnStatus));
  21. phantom.exit();
  22. }
  23. // Open the file, start working.
  24. page.open('preview.html', function () {
  25. if (page.framePlainText == "") {
  26. // Preview is empty. Did it get created properly?
  27. returnStatus.status = -3;
  28. console.log(JSON.stringify(returnStatus));
  29. phantom.exit();
  30. }
  31. page.viewportSize = {
  32. width: 1200,
  33. height: 800
  34. };
  35. // Save files to static
  36. fs.changeWorkingDirectory(rootPath + '/' + staticPath + '/stylespreview');
  37. if (!fs.isWritable(fs.workingDirectory)) {
  38. // Don't have write access.
  39. returnStatus.status = -4;
  40. console.log(JSON.stringify(returnStatus));
  41. phantom.exit();
  42. }
  43. page.render('full_' + system.args[3] + '.png');
  44. if (!fs.isFile('full_' + system.args[3] + '.png')) {
  45. // Failed to store full image.
  46. returnStatus.status = -5;
  47. console.log(JSON.stringify(returnStatus));
  48. phantom.exit();
  49. }
  50. // Remove temp files
  51. fs.changeWorkingDirectory(rootPath + '/' + staticPath + 'styles/' + system.args[3] + '/');
  52. if (!fs.isFile('preview.html') || !fs.isWritable('preview.html')) {
  53. // Can't find temp file to remove. Are the paths correct?
  54. returnStatus.status = -6;
  55. console.log(JSON.stringify(returnStatus));
  56. phantom.exit();
  57. }
  58. fs.remove('preview.html');
  59. // All good and done
  60. page.close();
  61. returnStatus.status = 0;
  62. console.log(JSON.stringify(returnStatus));
  63. phantom.exit();
  64. });