index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var jscodeshift = require('jscodeshift');
  2. function createArrowFunction(value) {
  3. return '() => ' + '\'' + value + '\'';
  4. }
  5. function createRegularFunction(value) {
  6. return 'function () {\n return ' + '\'' + value + '\'' + '\n}';
  7. }
  8. function createDynamicPromise(arr) {
  9. if(Array.isArray(arr)) {
  10. return '() => new Promise((resolve) => resolve([' + arr.map( (n) => {
  11. return '\'' + n + '\'';
  12. }) + ']))';
  13. } else {
  14. return '() => new Promise((resolve) => resolve(' + '\'' + arr + '\'' + '))';
  15. }
  16. }
  17. function createAssetFilterFunction(value) {
  18. return 'function (assetFilename) {\n return assetFilename.endsWith(' + '\'' + '.' + value + '\'' + ');\n}';
  19. }
  20. function createExternalFunction(regexp) {
  21. return '\n function (context, request, callback) {\n if ('
  22. + '/' + regexp + '/.test(request)){' + '\n' + ' return callback(null, \'commonjs\' + request);\n}\n'
  23. + 'callback();\n}';
  24. }
  25. function parseValue(regexp) {
  26. return jscodeshift(regexp);
  27. }
  28. function createCommonsChunkPlugin(value) {
  29. return 'new webpack.optimize.CommonsChunkPlugin({name:' + '\'' + value + '\'' + ',filename:' + '\'' + value + '\-[hash].min.js\'})';
  30. }
  31. function createRequire(val) {
  32. return 'const ' + val + ' = ' + 'require(' + '\'' + val + '\'' + ');';
  33. }
  34. function List(name, message, choices) {
  35. return ({
  36. type: 'list',
  37. name: name,
  38. message: message,
  39. choices: choices
  40. });
  41. }
  42. function RawList(name, message, choices) {
  43. return ({
  44. type: 'rawlist',
  45. name: name,
  46. message: message,
  47. choices: choices
  48. });
  49. }
  50. function CheckList(name, message, choices) {
  51. return ({
  52. type: 'checkbox',
  53. name: name,
  54. message: message,
  55. choices: choices
  56. });
  57. }
  58. function Input(name, message) {
  59. return ({
  60. type: 'input',
  61. name: name,
  62. message: message
  63. });
  64. }
  65. function InputValidate(name, message, cb) {
  66. return ({
  67. type: 'input',
  68. name: name,
  69. message: message,
  70. validate: cb
  71. });
  72. }
  73. function Confirm(name, message) {
  74. return ({
  75. type: 'confirm',
  76. name: name,
  77. message: message,
  78. });
  79. }
  80. module.exports = {
  81. createArrowFunction,
  82. createDynamicPromise,
  83. createRegularFunction,
  84. createAssetFilterFunction,
  85. createExternalFunction,
  86. parseValue,
  87. createCommonsChunkPlugin,
  88. createRequire,
  89. List,
  90. RawList,
  91. CheckList,
  92. Input,
  93. InputValidate,
  94. Confirm
  95. };