syncStyleFromAntd.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const { Octokit } = require('@octokit/rest');
  2. const Base64 = require('js-base64').Base64;
  3. const fs = require('fs');
  4. const fse = require('fs-extra');
  5. const path = require('path');
  6. const owner = 'ant-design';
  7. const repo = 'ant-design';
  8. const tag = '3.26.13';
  9. const clientId = '5f6ccfdc4cdc69f8ba12';
  10. const clientSecret = process.env.CLIENT_SECRET;
  11. const github = new Octokit();
  12. async function syncFiles(data = []) {
  13. for (const item of data) {
  14. try {
  15. const { data: itemData } = await github.repos.getContents({
  16. owner,
  17. repo,
  18. path: `${item.path}`,
  19. ref: tag,
  20. client_id: clientId,
  21. client_secret: clientSecret,
  22. });
  23. if (Array.isArray(itemData)) {
  24. syncFiles(itemData);
  25. } else {
  26. const toPath = path.join(__dirname, '..', itemData.path.replace(`/${itemData.name}`, ''));
  27. if (!fs.existsSync(toPath)) {
  28. fse.ensureDirSync(toPath);
  29. }
  30. // eslint-disable-next-line no-console
  31. console.log('update style: ', path.join(toPath, itemData.name.replace('.tsx', '.js')));
  32. const content = Base64.decode(itemData.content);
  33. fs.writeFileSync(path.join(toPath, itemData.name.replace('.tsx', '.js')), content);
  34. }
  35. } catch (e) {}
  36. }
  37. }
  38. async function syncStyle() {
  39. const { data = [] } = await github.repos.getContents({
  40. owner,
  41. repo,
  42. path: 'components',
  43. ref: tag,
  44. client_id: clientId,
  45. client_secret: clientSecret,
  46. });
  47. for (const item of data) {
  48. try {
  49. if (item.name === 'style') {
  50. syncFiles([item]);
  51. } else {
  52. const { data: itemData } = await github.repos.getContents({
  53. owner,
  54. repo,
  55. path: `${item.path}/style`,
  56. ref: tag,
  57. client_id: clientId,
  58. client_secret: clientSecret,
  59. });
  60. syncFiles(itemData);
  61. }
  62. } catch (e) {}
  63. }
  64. }
  65. syncStyle();