generate-types.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright © 2016-2022 The Thingsboard Authors
  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 child_process = require("child_process");
  17. const fs = require('fs');
  18. const path = require('path');
  19. const typeDir = path.join('.', 'target', 'types');
  20. const srcDir = path.join('.', 'target', 'types', 'src');
  21. const moduleMapPath = path.join('src', 'app', 'modules', 'common', 'modules-map.ts');
  22. const ngcPath = path.join('.', 'node_modules', '.bin', 'ngc');
  23. const tsconfigPath = path.join('src', 'tsconfig.app.json');
  24. console.log(`Remove directory: ${typeDir}`);
  25. try {
  26. fs.rmSync(typeDir, {recursive: true, force: true,});
  27. } catch (err) {
  28. console.error(`Remove directory error: ${err}`);
  29. }
  30. const cliCommand = `${ngcPath} --p ${tsconfigPath} --declaration --outDir ${srcDir}`;
  31. console.log(cliCommand);
  32. try {
  33. child_process.execSync(cliCommand);
  34. } catch (err) {
  35. console.error("Build types", err);
  36. process.exit(1);
  37. }
  38. function fromDir(startPath, filter, callback) {
  39. if (!fs.existsSync(startPath)) {
  40. console.log("not dirs", startPath);
  41. process.exit(1);
  42. }
  43. const files = fs.readdirSync(startPath);
  44. for (let i = 0; i < files.length; i++) {
  45. const filename = path.join(startPath, files[i]);
  46. const stat = fs.lstatSync(filename);
  47. if (stat.isDirectory()) {
  48. fromDir(filename, filter, callback);
  49. } else if (filter.test(filename)) {
  50. callback(filename)
  51. }
  52. }
  53. }
  54. fromDir(srcDir, /(\.js|\.js\.map)$/, function (filename) {
  55. try {
  56. fs.rmSync(filename);
  57. } catch (err) {
  58. console.error(`Remove file error ${filename}: ${err}`);
  59. }
  60. });
  61. fs.cpSync(moduleMapPath, `${typeDir}/${moduleMapPath}`);