deploy.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const LineReader = require('n-readlines');
  2. const zipdir = require('zip-dir');
  3. const copydir = require('copy-dir');
  4. const commander = require('commander');
  5. const del = require('del');
  6. const file = require('./src/file');
  7. const deploy = require('./deploy.json');
  8. // Lecture des arguments
  9. commander.version('1.2.1');
  10. commander
  11. .option('-z, --zip', 'create a zip file for each programs', false)
  12. .option('-c, --compress', 'zip and deletes the non-zip version of programs', false);
  13. commander.parse(process.argv);
  14. const zip = commander.zip || commander.compress;
  15. const remove = commander.compress;
  16. // Constante
  17. const OUT = './dist/';
  18. // Creation dossier destination
  19. file.makedir(OUT);
  20. // Séparation de chaque programme
  21. deploy.programs.forEach(program => {
  22. // Création du dossier
  23. const distPath = OUT + program.name;
  24. file.makedir(distPath);
  25. // Creation dossier data
  26. file.makedir(distPath + '/data');
  27. file.put(distPath + '/data/.gitkeep', '');
  28. // Copie des fichiers commun
  29. file.makedir(distPath + '/src');
  30. file.list('./src').forEach(f => {
  31. if (!file.isDir('./src/' + f)) {
  32. file.copy('./src/' + f, distPath + '/src/' + f);
  33. }
  34. });
  35. // Copie dossier propre à chaque programme
  36. copydir.sync('./src/' + program.name, distPath + '/src/' + program.name, {
  37. utimes: true,
  38. mode: true,
  39. cover: true
  40. });
  41. // Copie fichier main
  42. file.copy(program.main, distPath + '/main.js');
  43. // Copie package.json
  44. const pckg = require('./package.json');
  45. pckg.scripts = program.scripts;
  46. pckg.version = program.version;
  47. program.rm_dependencies.forEach(elt => {
  48. delete pckg.dependencies[elt];
  49. });
  50. let json = JSON.stringify(pckg, null, 4);
  51. json = json.replace(new RegExp(program.main, 'g'), 'main.js');
  52. file.put(distPath + '/package.json', json);
  53. // Ecriture ReadMe
  54. file.put(distPath + '/README.md', readMe(program.part).replace(new RegExp(program.main, 'g'), 'main.js'));
  55. // Zip du code pour distribution
  56. if (zip) {
  57. //zipdir.zipFolder(distPath, distPath + '.zip', err => {});
  58. zipdir(distPath, {saveTo: distPath + '.zip'}, (err, buffer) => {
  59. if (err) {
  60. console.error('Error when zipping files', err);
  61. } else {
  62. console.info(`Zip file created for ${program.name} in ${distPath}.zip`);
  63. // Si besoins on supprime les versions non zipper des dossier
  64. if (remove) {
  65. del(distPath + '/**').then(deletedPaths => {
  66. file.fs.rmdir(distPath, () => {
  67. console.info(`Directory ${distPath} deleted`);
  68. });
  69. });
  70. }
  71. }
  72. });
  73. }
  74. // Message de fin
  75. console.info(`${program.name} deployed in ${distPath}`);
  76. });
  77. // Fonction découpage readme
  78. function readMe(part) {
  79. const liner = new LineReader('./README.md');
  80. let data = '';
  81. let line = liner.next().toString();
  82. while (line !== 'false' && line.indexOf('## Partie ') === -1) {
  83. data += '\n' + line;
  84. line = liner.next().toString();
  85. }
  86. while (line !== 'false' && line.indexOf('## Partie ' + part) === -1) {
  87. line = liner.next().toString();
  88. }
  89. data += '\n' + line;
  90. line = liner.next().toString();
  91. while (line !== 'false' && line.indexOf('## Partie ') === -1) {
  92. data += '\n' + line;
  93. line = liner.next().toString();
  94. }
  95. if (line !== 'false') {
  96. liner.close();
  97. }
  98. return data;
  99. }