customBuild.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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.readFile(path.join(__dirname, './customBuildTpl.js'), 'utf8', function (err, data) {
  37. fs.writeFile(path.join(__dirname, '../src/nutui-custom.js'), str + data, (err) => {
  38. if (err) throw err;
  39. console.log(`生成nutui-custom.js文件成功`);
  40. });
  41. });
  42. fs.writeFile(destPath, JSON.stringify(choices, null, 2), (err) => {
  43. if (err) throw err;
  44. console.log(`生成配置文件custom.json成功`);
  45. });
  46. });