make.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env node
  2. /* global require, process */
  3. var { writeFileSync, readdirSync, statSync } = require('fs');
  4. var { execSync, spawn } = require('child_process');
  5. var cldr = require('cldr');
  6. // All files required.
  7. var FILES = [
  8. 'broker.js',
  9. 'config.js',
  10. 'proxypair.js',
  11. 'snowflake.js',
  12. 'ui.js',
  13. 'util.js',
  14. 'websocket.js',
  15. 'shims.js'
  16. ];
  17. var FILES_SPEC = [
  18. 'spec/broker.spec.js',
  19. 'spec/init.spec.js',
  20. 'spec/proxypair.spec.js',
  21. 'spec/snowflake.spec.js',
  22. 'spec/ui.spec.js',
  23. 'spec/util.spec.js',
  24. 'spec/websocket.spec.js'
  25. ];
  26. var STATIC = 'static';
  27. var SHARED_FILES = [
  28. 'embed.html',
  29. 'embed.css',
  30. 'popup.js',
  31. 'assets',
  32. '_locales',
  33. ];
  34. var concatJS = function(outDir, init, outFile, pre) {
  35. var files = FILES.concat(`init-${init}.js`);
  36. var outPath = `${outDir}/${outFile}`;
  37. writeFileSync(outPath, pre, 'utf8');
  38. execSync(`cat ${files.join(' ')} >> ${outPath}`);
  39. };
  40. var copyTranslations = function(outDir) {
  41. execSync('git submodule update --init -- translation')
  42. execSync(`cp -rf translation/* ${outDir}/_locales/`);
  43. };
  44. var getDisplayName = function(locale) {
  45. var code = locale.split("_")[0];
  46. try {
  47. var name = cldr.extractLanguageDisplayNames(code)[code];
  48. }
  49. catch(e) {
  50. return '';
  51. }
  52. if (name === undefined) {
  53. return '';
  54. }
  55. return name;
  56. }
  57. var availableLangs = function() {
  58. let out = "const availableLangs = new Set([\n";
  59. let dirs = readdirSync('translation').filter((f) => {
  60. const s = statSync(`translation/${f}`);
  61. return s.isDirectory();
  62. });
  63. dirs.push('en_US');
  64. dirs.sort();
  65. dirs = dirs.map(d => ` '${d}',`);
  66. out += dirs.join("\n");
  67. out += "\n]);\n\n";
  68. return out;
  69. };
  70. var translatedLangs = function() {
  71. let out = "const availableLangs = {\n";
  72. let dirs = readdirSync('translation').filter((f) => {
  73. const s = statSync(`translation/${f}`);
  74. return s.isDirectory();
  75. });
  76. dirs.push('en_US');
  77. dirs.sort();
  78. dirs = dirs.map(d => `'${d}': {"name": '${getDisplayName(d)}'},`);
  79. out += dirs.join("\n");
  80. out += "\n};\n\n";
  81. return out;
  82. };
  83. var tasks = new Map();
  84. var task = function(key, msg, func) {
  85. tasks.set(key, {
  86. msg, func
  87. });
  88. };
  89. task('test', 'snowflake unit tests', function() {
  90. var jasmineFiles, outFile, proc;
  91. execSync('mkdir -p test');
  92. execSync('jasmine init >&-');
  93. // Simply concat all the files because we're not using node exports.
  94. jasmineFiles = FILES.concat('init-testing.js', FILES_SPEC);
  95. outFile = 'test/bundle.spec.js';
  96. execSync('echo "TESTING = true" > ' + outFile);
  97. execSync('cat ' + jasmineFiles.join(' ') + ' | cat >> ' + outFile);
  98. proc = spawn('jasmine', ['test/bundle.spec.js'], {
  99. stdio: 'inherit'
  100. });
  101. proc.on("exit", function(code) {
  102. process.exit(code);
  103. });
  104. });
  105. task('build', 'build the snowflake proxy', function() {
  106. const outDir = 'build';
  107. execSync(`rm -rf ${outDir}`);
  108. execSync(`cp -r ${STATIC}/ ${outDir}/`);
  109. copyTranslations(outDir);
  110. concatJS(outDir, 'badge', 'embed.js', availableLangs());
  111. writeFileSync(`${outDir}/index.js`, translatedLangs(), 'utf8');
  112. execSync(`cat ${STATIC}/index.js >> ${outDir}/index.js`);
  113. console.log('Snowflake prepared.');
  114. });
  115. task('webext', 'build the webextension', function() {
  116. const outDir = 'webext';
  117. execSync(`git clean -f -x -d ${outDir}/`);
  118. execSync(`cp -r ${STATIC}/{${SHARED_FILES.join(',')}} ${outDir}/`, { shell: '/bin/bash' });
  119. copyTranslations(outDir);
  120. concatJS(outDir, 'webext', 'snowflake.js', '');
  121. console.log('Webextension prepared.');
  122. });
  123. task('node', 'build the node binary', function() {
  124. execSync('mkdir -p build');
  125. concatJS('build', 'node', 'snowflake.js', '');
  126. console.log('Node prepared.');
  127. });
  128. task('pack-webext', 'pack the webextension for deployment', function() {
  129. try {
  130. execSync(`rm -f source.zip`);
  131. execSync(`rm -f webext/webext.zip`);
  132. } catch (error) {
  133. //Usually this happens because the zip files were removed previously
  134. console.log('Error removing zip files');
  135. }
  136. execSync(`git submodule update --remote`);
  137. var version = process.argv[3];
  138. console.log(version);
  139. var manifest = require('./webext/manifest.json')
  140. manifest.version = version;
  141. writeFileSync('./webext/manifest.json', JSON.stringify(manifest, null, 2), 'utf8');
  142. execSync(`git commit -am "bump version to ${version}"`);
  143. try {
  144. execSync(`git tag webext-${version}`);
  145. } catch (error) {
  146. console.log('Error creating git tag');
  147. // Revert changes
  148. execSync(`git reset HEAD~`);
  149. execSync(`git checkout ./webext/manifest.json`);
  150. execSync(`git submodule update`);
  151. return;
  152. }
  153. execSync(`git archive -o source.zip HEAD .`);
  154. execSync(`npm run webext`);
  155. execSync(`cd webext && zip -Xr webext.zip ./*`);
  156. });
  157. task('clean', 'remove all built files', function() {
  158. execSync('rm -rf build test spec/support');
  159. });
  160. var cmd = process.argv[2];
  161. if (tasks.has(cmd)) {
  162. var t = tasks.get(cmd);
  163. console.log(t.msg);
  164. t.func();
  165. } else {
  166. console.error('Command not supported.');
  167. }