deploy.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const LineReader = require('n-readlines');
  2. const zip = require('zip-dir');
  3. const copydir = require('copy-dir');
  4. const file = require('./src/file');
  5. const deploy = require('./deploy.json');
  6. // Constante
  7. const OUT = './dist/';
  8. // Creation dossier destination
  9. file.makedir(OUT);
  10. // Séparation de chaque programme
  11. deploy.programs.forEach(program => {
  12. // Création du dossier
  13. const distPath = OUT + program.name;
  14. file.makedir(distPath);
  15. // Creation dossier data
  16. file.makedir(distPath + '/data');
  17. file.put(distPath + '/data/.gitkeep', '');
  18. // Copie des fichiers commun
  19. file.makedir(distPath + '/src');
  20. file.fs.readdirSync('./src').forEach(f => {
  21. if (!file.isDir('./src/' + f)) {
  22. file.fs.copyFile('./src/' + f, distPath + '/src/' + f, (err) => {
  23. if (err) throw err;
  24. });
  25. }
  26. });
  27. // Copie dossier propre à chaque programme
  28. copydir('./src/' + program.name, distPath + '/src/' + program.name, {
  29. utimes: true,
  30. mode: true,
  31. cover: true
  32. }, (err) => {
  33. if (err) throw err;
  34. });
  35. // Copie fichier main
  36. file.fs.copyFile(program.main, distPath + '/main.js', (err) => {
  37. if (err) throw err;
  38. });
  39. // Copie package.json
  40. const pckg = require('./package.json');
  41. pckg.scripts = program.scripts;
  42. pckg.version = program.version;
  43. program.rm_dependencies.forEach(elt => {
  44. delete pckg.dependencies[elt];
  45. });
  46. let json = JSON.stringify(pckg, null, 4);
  47. json = json.replace(new RegExp(program.main, 'g'), 'main.js');
  48. file.put(distPath + '/package.json', json);
  49. // Ecriture ReadMe
  50. file.put(distPath + '/README.md', readMe(program.part).replace(new RegExp(program.main, 'g'), 'main.js'));
  51. // Zip du code pour distribution
  52. zip(distPath, {saveTo: distPath + '.zip'}, (err, buffer) => {
  53. if (err) {
  54. console.error('Error when zipping files', err);
  55. }
  56. });
  57. });
  58. // Fonction découpage readme
  59. function readMe(part) {
  60. const liner = new LineReader('./README.md');
  61. let data = '';
  62. let line = liner.next().toString();
  63. while (line !== 'false' && line.indexOf('## Partie ') === -1) {
  64. data += '\n' + line;
  65. line = liner.next().toString();
  66. }
  67. while (line !== 'false' && line.indexOf('## Partie ' + part) === -1) {
  68. line = liner.next().toString();
  69. }
  70. data += '\n' + line;
  71. line = liner.next().toString();
  72. while (line !== 'false' && line.indexOf('## Partie ') === -1) {
  73. data += '\n' + line;
  74. line = liner.next().toString();
  75. }
  76. if (line !== 'false') {
  77. liner.close();
  78. }
  79. return data;
  80. }