migrate_old.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const fs = require('fs');
  2. const path = require('path');
  3. const { exec } = require('child_process');
  4. const Promise = require('bluebird');
  5. const examplesDir = path.normalize(__dirname + '/../exemples/jquery');
  6. // const listJson = require('../exemples/liste.json');
  7. const beautify = require("json-beautify");
  8. Promise.promisifyAll(fs);
  9. // Promisified exec
  10. function execAsync(cmd) {
  11. return new Promise((resolve, reject) => {
  12. exec(cmd, (error, stdout, stderr) => {
  13. if (error) {
  14. return reject(error);
  15. }
  16. resolve({ stdout, stderr });
  17. });
  18. });
  19. }
  20. // Promisified exec of git mv src dst
  21. function gitMvAsync(src, dst) {
  22. var cmd = 'git mv ' + src + ' ' + dst;
  23. execAsync(cmd)
  24. .then(({stdout, stderr}) => {
  25. console.log(`stdout: ${stdout}`);
  26. console.log(`stderr: ${stderr}`);
  27. })
  28. .catch(error => {
  29. console.error(`exec error: ${error}`);
  30. throw error;
  31. });
  32. }
  33. // Promisified exec of git status
  34. // execAsync('git status')
  35. // .then(({stdout, stderr}) => {
  36. // console.log(`stdout: ${stdout}`);
  37. // console.log(`stderr: ${stderr}`);
  38. // })
  39. // .catch(error => {
  40. // console.error(`exec error: ${error}`);
  41. // });
  42. // rename all examples' html
  43. // fs.readdirAsync(examplesDir)
  44. // .then(dirContent => {
  45. // var excludes = ['liste.json', '.gitkeep', 'start-iframe.html', 'template.html'];
  46. // excludes.forEach(file => {
  47. // var idxInContent = dirContent.indexOf(file);
  48. // if(idxInContent !== -1) {
  49. // dirContent.splice(idxInContent, 1);
  50. // }
  51. // });
  52. // return dirContent;
  53. // })
  54. // .then(dirContent => Promise.map(dirContent, dir => {
  55. // const fullPath = examplesDir + '/' + dir;
  56. // const src = fullPath + '/contenu.html';
  57. // const dst = fullPath + '/example.html';
  58. // gitMvAsync(src, dst)
  59. // })
  60. // )
  61. // .catch(console.error);
  62. function writeExampleConfig(slug) {
  63. var config = require(examplesDir + '/' + slug + '/config.json');
  64. var { title, category, html, js, css, libsJs, libsCss } = config;
  65. config = { title, category, html, js, css, libsJs, libsCss };
  66. // console.log(exampleConfig)
  67. // return new Promise((resolve, reject) => {
  68. // const { slug, title } = example;
  69. // const config = {
  70. // title,
  71. // html: ['example.html'],
  72. // js: ['script.js'],
  73. // css: [],
  74. // libsCss: ['styles.css'],
  75. // libsJs: ['jquery-3.2.1.min.js']
  76. // };
  77. const configJson = beautify(config, null, 2, 100);
  78. // // resolve(configJson);
  79. return fs.writeFileAsync(examplesDir + '/' + slug + '/config.json', configJson);
  80. // });
  81. }
  82. fs.readdirAsync(examplesDir)
  83. .then(dirContent => {
  84. var excludes = ['liste.json', '.gitkeep', 'repo-config.json'];
  85. excludes.forEach(file => {
  86. var idxInContent = dirContent.indexOf(file);
  87. if(idxInContent !== -1) {
  88. dirContent.splice(idxInContent, 1);
  89. }
  90. });
  91. return dirContent;
  92. })
  93. .then(exampleList => Promise.map(exampleList, writeExampleConfig))
  94. .then(filesPerExample => console.log(filesPerExample));