tsconfig.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. import argparse
  3. import glob
  4. import json
  5. import os
  6. import re
  7. import sys
  8. from pathlib import Path
  9. from . import Command
  10. from odoo.modules.module import MANIFEST_NAMES
  11. class TSConfig(Command):
  12. """ Generates tsconfig files for javascript code """
  13. def get_module_list(self, path):
  14. return [
  15. mod.split(os.path.sep)[-2]
  16. for mname in MANIFEST_NAMES
  17. for mod in glob.glob(os.path.join(path, f'*/{mname}'))
  18. ]
  19. def clean_path(self, path):
  20. return re.sub(r"/{2,}", "/", path)
  21. def prefix_suffix_path(self, path, prefix, suffix):
  22. return self.clean_path(f"{prefix}/{path}/{suffix}")
  23. def remove_(self, modules, module):
  24. for name, path in modules:
  25. if module == name:
  26. modules.remove((name, path))
  27. def run(self, cmdargs):
  28. parser = argparse.ArgumentParser(
  29. prog=f'{Path(sys.argv[0]).name} {self.name}',
  30. description=self.__doc__.strip()
  31. )
  32. parser.add_argument('--addons-path', type=str, nargs=1, dest="paths")
  33. args = parser.parse_args(args=cmdargs)
  34. paths = list(map(self.clean_path, args.paths[0].split(',')))
  35. modules = {}
  36. owl_path = ""
  37. for path in paths:
  38. for module in self.get_module_list(path):
  39. modules[module] = self.prefix_suffix_path(module, path, "/static/src/*")
  40. if module == "web":
  41. owl_path = self.prefix_suffix_path(module, path, "/static/lib/owl/owl.js")
  42. content = self.generate_file_content(modules, paths)
  43. content["compilerOptions"]["paths"]["@odoo/owl"] = [owl_path]
  44. # pylint: disable=bad-builtin
  45. print(json.dumps(content, indent=2))
  46. def generate_imports(self, modules):
  47. return {
  48. f'@{module}/*': [path]
  49. for module, path in modules.items()
  50. }
  51. def generate_file_content(self, modules, paths):
  52. return {
  53. 'compilerOptions': {
  54. "baseUrl": ".",
  55. "target": "es2019",
  56. "checkJs": True,
  57. "allowJs": True,
  58. "noEmit": True,
  59. "typeRoots": list(map(lambda p: p + "/web/tooling/types", paths)),
  60. "paths": self.generate_imports(modules)
  61. }, "exclude": self.generate_excludes()
  62. }
  63. def generate_excludes(self):
  64. return [
  65. "/**/*.po",
  66. "/**/*.py",
  67. "/**/*.pyc",
  68. "/**/*.xml",
  69. "/**/*.png",
  70. "/**/*.md",
  71. "/**/*.dat",
  72. "/**/*.scss",
  73. "/**/*.jpg",
  74. "/**/*.svg",
  75. "/**/*.pot",
  76. "/**/*.csv",
  77. "/**/*.mo",
  78. "/**/*.txt",
  79. "/**/*.less",
  80. "/**/*.bcmap",
  81. "/**/*.properties",
  82. "/**/*.html",
  83. "/**/*.ttf",
  84. "/**/*.rst",
  85. "/**/*.css",
  86. "/**/*.pack",
  87. "/**/*.idx",
  88. "/**/*.h",
  89. "/**/*.map",
  90. "/**/*.gif",
  91. "/**/*.sample",
  92. "/**/*.doctree",
  93. "/**/*.so",
  94. "/**/*.pdf",
  95. "/**/*.xslt",
  96. "/**/*.conf",
  97. "/**/*.woff",
  98. "/**/*.xsd",
  99. "/**/*.eot",
  100. "/**/*.jst",
  101. "/**/*.flow",
  102. "/**/*.sh",
  103. "/**/*.yml",
  104. "/**/*.pfb",
  105. "/**/*.jpeg",
  106. "/**/*.crt",
  107. "/**/*.template",
  108. "/**/*.pxd",
  109. "/**/*.dylib",
  110. "/**/*.pem",
  111. "/**/*.rng",
  112. "/**/*.xsl",
  113. "/**/*.xls",
  114. "/**/*.cfg",
  115. "/**/*.pyi",
  116. "/**/*.pth",
  117. "/**/*.markdown",
  118. "/**/*.key",
  119. "/**/*.ico",
  120. ]