copyStatics.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import fs from 'fs';
  2. import ncp from 'ncp';
  3. import * as path from 'path';
  4. import { fileURLToPath } from 'url';
  5. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  6. class CopyStaticsTask {
  7. static SourcePath = './static';
  8. static DestinationPath = './build/static'
  9. static AppPath = `${CopyStaticsTask.DestinationPath}/App.js`;
  10. static start() {
  11. const pck = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../package.json')).toString());
  12. const replacementMap = [
  13. { regexp: /#_version_/g, replacement: pck.version }
  14. ];
  15. ncp(CopyStaticsTask.SourcePath, CopyStaticsTask.DestinationPath, function (err) {
  16. if (err) {
  17. return console.error(err);
  18. }
  19. CopyStaticsTask.applyGlobals(replacementMap);
  20. });
  21. }
  22. static applyGlobals(replacementMap) {
  23. fs.readFile(CopyStaticsTask.AppPath, 'utf8', function (err, data) {
  24. if (err) {
  25. return console.error(`Unable to read file ${CopyStaticsTask.AppPath}: ` + err);
  26. }
  27. for(const replacement of replacementMap) {
  28. data = data.replace(replacement.regexp, replacement.replacement);
  29. }
  30. fs.writeFile(CopyStaticsTask.AppPath, data, 'utf8', function (err) {
  31. if (err) return console.error('Unable to write file: ' + err);
  32. });
  33. });
  34. }
  35. }
  36. CopyStaticsTask.start();