build.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var spawn = require('child_process').spawn;
  2. const path = require('path');
  3. const fs = require('fs');
  4. const writeApiJson = function () {
  5. /* API Url set by build parameter */
  6. let apiConfig = {
  7. "course_api_url": "http://m-xyyf-api.ai160.com/",
  8. "lesson_api_url": "http://m-xyyf-api.ai160.com/lesson",
  9. "tts_api_url": "https://yfxxt-api.ai160.com/baidu/accessToken",
  10. "xiaomi_qr": "https://h5.tv.mi.com/store/thirdparty/pricetag/shortkey/",
  11. "en": "pro"
  12. };
  13. let options = process.argv;
  14. for (let i = 0; i < options.length; i++) {
  15. if (options[i].indexOf('-test') == 0) {
  16. apiConfig = { // 开发环境
  17. "course_api_url": "http://m-xyyf-api.ai160.com/",
  18. "lesson_api_url": "http://m-xyyf-api.ai160.com/lesson",
  19. "tts_api_url": "https://yfxxt-api.ai160.com/baidu/accessToken",
  20. "xiaomi_qr": "https://h5.tv.mi.com/store/thirdparty/pricetag/shortkey/",
  21. "en": "test"
  22. };
  23. }
  24. }
  25. fs.writeFileSync(path.join(__dirname, 'src/res/values/api.json'), JSON.stringify(apiConfig));
  26. console.log('Write API Config Success!');
  27. }
  28. const writeTimeStamp = function () {
  29. /* JavaScript and CSS Import TimeStamp set by UnixTimeStamp */
  30. let ts = Math.floor(new Date().getTime() / 1000);
  31. let fileStr = fs.readFileSync(path.join(__dirname, 'build/stage/index/index.html')) + '';
  32. fileStr = fileStr.replace(/\[timestamp\]/g, ts); //时间戳标记替换为时间戳
  33. fileStr = fileStr.replace(/\/index.js/g, '/index.min.js'); //index.js替换为index.min.js
  34. fileStr = fileStr.replace(/\/index.css/g, '/index.min.css'); //index.js替换为index.min.js
  35. fs.writeFileSync(path.join(__dirname, 'build/stage/index/index.html'), fileStr);
  36. console.log('Write JavaScript and CSS Import TimeStamp = ' + ts + ' Success!');
  37. }
  38. //因为引用在代码里面的json文件,在webpack打包时会直接打在生成后的index.js里面,所以需要在执行moye b 之前,就修改src目录内的api.json
  39. writeApiJson();
  40. //启动
  41. const moyeb = spawn('moye', ['b'], {
  42. shell: true
  43. });
  44. // 捕获标准输出并将其打印到控制台
  45. moyeb.stdout.on('data', function (data) {
  46. let stdoutStr = data.toString();
  47. if (stdoutStr.indexOf("Finished \'default\' after") >= 0) {
  48. writeTimeStamp();
  49. setTimeout(function () {
  50. console.log('TV Package Build Success!');
  51. moyeb.kill();
  52. }, 1000);
  53. }
  54. });
  55. // 捕获标准错误输出并将其打印到控制台
  56. moyeb.stderr.on('data', function (data) {
  57. console.log('' + data);
  58. });
  59. // 注册子进程关闭事件
  60. moyeb.on('exit', function (code, signal) {
  61. if (code && code != 0) {
  62. console.log('Build Error!! Code:' + code);
  63. return;
  64. }
  65. console.log('Build Done!!');
  66. });