copy-dist.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright 1999-2019 Seata.io Group.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. const path = require('path');
  17. const fs = require('fs');
  18. const styles = {
  19. 'red': ['\x1B[31m', '\x1B[39m'],
  20. 'green': ['\x1B[32m', '\x1B[39m'],
  21. 'yellow': ['\x1B[33m', '\x1B[39m'],
  22. };
  23. const distPath = path.join(__dirname, '../dist/');
  24. const rootPath = path.join(__dirname, '../../');
  25. console.log('\n\n> Start copying the dist directory...\n');
  26. function delDir(dest) {
  27. let paths = fs.readdirSync(dest);
  28. paths.forEach(function(p) {
  29. const target = path.join(dest, p);
  30. const st = fs.statSync(target);
  31. if (st.isFile()) {
  32. console.log(`\r${styles.red[0]}Delete File${styles.red[1]}: ${target}`);
  33. fs.unlinkSync(target);
  34. }
  35. if (st.isDirectory()) {
  36. console.log(`\r${styles.red[0]}Delete Directory${styles.red[1]}: ${target}`);
  37. delDir(target);
  38. }
  39. });
  40. paths = fs.readdirSync(dest);
  41. if (!paths.length) {
  42. fs.rmdirSync(dest);
  43. }
  44. }
  45. function copyDir(source, dest) {
  46. const paths = fs.readdirSync(source);
  47. paths.forEach(function(p) {
  48. const src = path.join(source, p);
  49. const target = path.join(dest, p);
  50. const st = fs.statSync(src);
  51. if (st.isFile()) {
  52. if (fs.existsSync(target)) {
  53. console.log(`\r${styles.red[0]}Delete File${styles.red[1]}: ${target}`);
  54. fs.unlinkSync(target);
  55. }
  56. console.log(`\r${styles.yellow[0]}Copy File${styles.yellow[1]}: ${target}`);
  57. const readStream = fs.createReadStream(src);
  58. const writeStream = fs.createWriteStream(target);
  59. readStream.pipe(writeStream);
  60. }
  61. if (st.isDirectory()) {
  62. if (fs.existsSync(target)) {
  63. console.log(`\r${styles.red[0]}Delete Directory${styles.red[1]}: ${target}`);
  64. delDir(target);
  65. }
  66. console.log(`\r${styles.yellow[0]}Create Directory${styles.yellow[1]}: ${target}`);
  67. fs.mkdirSync(target);
  68. copyDir(src, target);
  69. }
  70. });
  71. }
  72. copyDir(distPath, rootPath);
  73. console.log(`\n>${styles.green[0]} Copy complete!${styles.green[0]}\n`);