migrate_old.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(example) {
  63. // return new Promise((resolve, reject) => {
  64. const { slug, title } = example;
  65. const config = {
  66. title,
  67. html: ['example.html'],
  68. js: ['script.js'],
  69. css: [],
  70. libsCss: ['styles.css'],
  71. libsJs: ['jquery-3.2.1.min.js']
  72. };
  73. const configJson = beautify(config, null, 2, 100);
  74. // resolve(configJson);
  75. return fs.writeFileAsync(examplesDir + '/' + slug + '/config.json', configJson);
  76. // });
  77. }
  78. Promise.map(listJson, writeExampleConfig)
  79. .then(filesPerExample => console.log(filesPerExample));