main.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const fs = require('fs');
  2. const path = require('path');
  3. const root = process.cwd();
  4. const readdirSync = p => fs.readdirSync(p);
  5. const existsSync = p => fs.existsSync(p);
  6. const isFile = p => fs.statSync(p).isFile();
  7. const isDirectory = p => fs.statSync(p).isDirectory();
  8. const getBaseName = p => path.basename(p);
  9. const getExtName = p => path.extname(p);
  10. const getFileName = p => getBaseName(p);
  11. const getDirName = p => path.dirname(p);
  12. const join = (...ps) => path.join(root, ...ps);
  13. const writeJsonToFile = p => json =>
  14. fs.writeFileSync(p, JSON.stringify(json, null, 2));
  15. const tools = require('./src/tools')
  16. const config = {
  17. // origin: 'https://cdn-ali.jianyu360.com',
  18. libName: 'lib',
  19. origin: '',
  20. root: '/'
  21. }
  22. // 递归获取目录下所有文件,并组合为对象树
  23. const search = p => {
  24. if (existsSync(p)) {
  25. if (isFile(p)) {
  26. return {
  27. name: getFileName(p),
  28. path: getDirName(p)
  29. };
  30. } else if (isDirectory(p)) {
  31. const paths = readdirSync(p);
  32. return paths.map(_p => {
  33. const tmp = path.join(p, _p);
  34. if (isFile(tmp)) {
  35. return search(tmp);
  36. } else {
  37. return {
  38. name: getFileName(_p),
  39. list: search(tmp)
  40. };
  41. }
  42. });
  43. }
  44. }
  45. };
  46. // 将手机号
  47. const getSourceMap = m => {
  48. // 最外层库名
  49. return m.filter(lib => lib.list).map(lib => {
  50. // 下一层版本号
  51. const versions = lib.list.map(version => {
  52. // 当前路径
  53. const rPath = `/${config.libName}/${lib.name}/${version.name}/`
  54. const sourceArr = tools.getAllFilePath(join(rPath))
  55. const sourcePathArr = sourceArr.map(s => config.origin + s.replace(__dirname, '').replace(/\\/g, '\/'))
  56. console.log(rPath)
  57. return {
  58. version: version.name,
  59. // sourcePath: version.list,
  60. sourcePath: sourcePathArr
  61. }
  62. })
  63. return {
  64. lib: lib.name,
  65. versions
  66. }
  67. })
  68. }
  69. writeJsonToFile('./libs.json')(getSourceMap(search(config.libName)))