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.

205 lines
6.0 KiB

  1. #!/usr/bin/env node
  2. 'use strict';
  3. var fs = require('fs');
  4. var path = require('path');
  5. var mkdirp = require('mkdirp');
  6. var jsdom = require('jsdom');
  7. var Q = require('q');
  8. var onlyStandAlone = [
  9. "0117-basic-routing-show-hide-map-example.html"
  10. ];
  11. var isAnExample = function(filename) {
  12. if (filename === '0000-viewer.html') {
  13. return false;
  14. }
  15. return /[0-9][0-9][0-9][0-9].*\.html/.test(filename);
  16. };
  17. var isJavascript = function(filename) {
  18. return /.*\.js/.test(filename);
  19. };
  20. var deleteFileIfJavascript = function(filename) {
  21. var df = Q.defer();
  22. if (isJavascript(filename)) {
  23. fs.unlink(filename, function() {
  24. df.resolve();
  25. });
  26. } else {
  27. df.resolve();
  28. }
  29. return df.promise;
  30. };
  31. var cleanJavascriptFilesFromControllersDirectory = function(dir) {
  32. var df = Q.defer();
  33. fs.readdir(dir, function(err, list) {
  34. var l = [];
  35. var files = list.map(function(file) {
  36. return path.join(dir, file);
  37. });
  38. files.forEach(deleteFileIfJavascript);
  39. Q.allSettled(l).then(function(result) {
  40. df.resolve();
  41. });
  42. });
  43. return df.promise;
  44. };
  45. var writeController = function(script, examplefile, controllers_directory) {
  46. var df = Q.defer();
  47. var scriptLines = script.split('\n');
  48. var outfilename;
  49. var outScript = [];
  50. for (var i = 0; i < scriptLines.length; i++) {
  51. var line = scriptLines[i];
  52. // Remove empty lines
  53. if (line.replace(/^\s+|\s+$/g, '') === '') {
  54. continue;
  55. }
  56. // Remove the module initializacion line
  57. if (line.search('angular.module') !== -1) {
  58. continue;
  59. }
  60. // Extract controller name
  61. if (line.search('app.controller') !== -1 && !outfilename) {
  62. var controller = line.match(/app.controller\((\'|\")([^'"]*)/);
  63. if (controller && controller.length > 2 && controller[2]) {
  64. outfilename = controller[2] + '.js';
  65. }
  66. }
  67. outScript.push(line);
  68. }
  69. if (outfilename) {
  70. outfilename = path.join(controllers_directory, outfilename);
  71. if (!fs.existsSync(outfilename)) {
  72. fs.writeFile(outfilename, outScript.join('\n'), function() {
  73. df.resolve();
  74. });
  75. } else {
  76. console.log('The controller name is duplicated: ' + outfilename)
  77. df.reject();
  78. }
  79. } else {
  80. console.log('Can\'t identify the controller name in the example ' + examplefile)
  81. df.reject();
  82. }
  83. return df.promise;
  84. };
  85. var generateControllersFromExamples = function(examples_directory, controllers_directory) {
  86. var df = Q.defer();
  87. fs.readdir(examples_directory, function(err, list) {
  88. var l = [];
  89. list.forEach(function(filename) {
  90. if (isAnExample(filename)) {
  91. var html = fs.readFileSync(path.join(__dirname, 'examples', filename));
  92. jsdom.env({
  93. html: html.toString(),
  94. done: function(err, window) {
  95. var scripts = window.document.getElementsByTagName('script');
  96. var last = scripts.length -1;
  97. var script = scripts[last].innerHTML;
  98. l.push(writeController(script, filename, controllers_directory));
  99. }
  100. });
  101. }
  102. });
  103. Q.allSettled(l).then(function() {
  104. df.resolve();
  105. });
  106. });
  107. return df.promise;
  108. };
  109. var extractId = function(filename) {
  110. var arr = filename.replace('.html', '').split('-');
  111. arr.splice(0,2);
  112. return arr.join('-');
  113. };
  114. var extractTitle = function(filename) {
  115. var html = fs.readFileSync(path.join(__dirname, 'examples', filename));
  116. var title;
  117. var arr = html.toString().split('\n');
  118. for (var i = 0; i< arr.length; i++) {
  119. var line = arr[i];
  120. if (line.search('<h1>') !== -1) {
  121. title = line.replace('<h1>', '').replace('</h1>', '').replace(/^ */, '');
  122. }
  123. }
  124. return title;
  125. };
  126. var extractDescription = function(filename) {
  127. };
  128. var extractDate = function(filename) {
  129. var stats = fs.statSync(filename);
  130. return stats.mtime;
  131. };
  132. var generateExamplesJSONFile = function(examples_directory, json_file) {
  133. var df = Q.defer();
  134. var examples = {};
  135. fs.readdir(examples_directory, function(err, list) {
  136. list.forEach(function(filename) {
  137. if (isAnExample(filename)) {
  138. var section = filename.split('-')[1];
  139. var id = extractId(filename);
  140. var extUrl = filename;
  141. var title = extractTitle(filename);
  142. var description = extractDescription(filename);
  143. var date = extractDate(path.join(examples_directory, filename));
  144. if (!(section in examples)) {
  145. examples[section] = [];
  146. }
  147. examples[section].push({
  148. date: date,
  149. section: section,
  150. onlyStandAlone: onlyStandAlone.indexOf(extUrl) !== -1,
  151. id: '/' + section + '/' + id,
  152. extUrl: extUrl,
  153. title: title,
  154. description: description
  155. });
  156. }
  157. });
  158. fs.writeFile(json_file, JSON.stringify(examples, null, 4), function(err) {
  159. df.resolve();
  160. });
  161. });
  162. return df.promise;
  163. };
  164. var controllers_directory = path.join(__dirname, 'examples', 'js', 'controllers');
  165. mkdirp(controllers_directory, function(err) {
  166. cleanJavascriptFilesFromControllersDirectory(controllers_directory).then(function() {
  167. var examples_directory = path.join(__dirname, 'examples');
  168. generateControllersFromExamples(examples_directory, controllers_directory).then(function() {
  169. var json_file = path.join(__dirname, 'examples', 'json', 'examples.json');
  170. generateExamplesJSONFile(examples_directory, json_file);
  171. });
  172. });
  173. });