customBuild.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const inquirer = require('inquirer');
  2. const conf = require('../config.json');
  3. const fs = require('fs');
  4. const path = require('path');
  5. inquirer
  6. .prompt([
  7. {
  8. type: 'checkbox',
  9. message: '请选择需要打包的组件',
  10. name: 'packages',
  11. choices: conf.packages,
  12. validate: function(answer) {
  13. if (answer.length < 1) {
  14. return '请至少选择一项.';
  15. }
  16. return true;
  17. }
  18. }
  19. ])
  20. .then(answers => {
  21. const destPath = path.join(__dirname, '../custom.json');
  22. const choices = {
  23. 'version':conf.version,
  24. 'packages':[]
  25. };
  26. let str = 'let pkg = {};\n';
  27. answers.packages.map((name)=>{
  28. conf.packages.map((pkg)=>{
  29. if(pkg.name==name){
  30. choices.packages.push(pkg);
  31. const loName = name.toLowerCase();
  32. str += "pkg['" + loName + "'] = require('./package/" + loName + "/index.js').default;\n";
  33. }
  34. });
  35. });
  36. fs.writeFile(destPath, JSON.stringify(choices, null, 2), (err) => {
  37. if (err) throw err;
  38. console.log(`生成配置文件custom.json成功`);
  39. fs.readFile(path.join(__dirname, './customBuildTpl.js'), 'utf8', function (err, data) {
  40. fs.writeFile(path.join(__dirname, '../src/nutui-custom.js'), str + data, (err) => {
  41. if (err) throw err;
  42. console.log(`生成nutui-custom.js文件成功`);
  43. console.log([
  44. '',
  45. 'if there are some errors about "JavaScript heap out of memory",',
  46. 'you can have a read about "https://www.npmjs.com/package/increase-memory-limit"',
  47. ''
  48. ].join('\n'));
  49. });
  50. });
  51. });
  52. });