Coverage for src/su6_plugin_svelte_check/cli.py: 100%

1"""

2 This module contains an example of both methods of adding commands to su6.

3"""

4 from pathlib import Path

5

6 from su6.plugins import PluginConfig, register, run_tool

7

8 from .helpers import chdir, find_project_root

9

10

11@register

12 class SvelteCheckPluginConfig(PluginConfig):

13 """

14 Config without state, loads [tool.su6.demo] from pyproject.toml into self.

15 """

16

17 strict: bool = False

18 tsconfig: str = "./tsconfig.json"

19

20

21 config = SvelteCheckPluginConfig()

22

23

24@register

25 def install_svelte_check(target_dir: str = None) -> int:

26 """

27 Install the svelte-check tool using npm.

28 """

29 target_dir = target_dir or "./"

30

31 with chdir(target_dir):

32 return run_tool("npm", "install", "svelte-check")

33

34

35@register(add_to_all=True)

36 def svelte_check(strict: bool = None, tsconfig: str = None, node_modules: str = None) -> int:

37 """

38 Run the svelte-check tool.

39 """

40 config.update(strict=strict, tsconfig=tsconfig)

41 # svelte-check --tsconfig ./tsconfig.json --threshold error

42

43 node_modules = node_modules or "node_modules"

44 root, _ = find_project_root((node_modules,))

45 executable = str(root / "node_modules/svelte-check/bin/svelte-check")

46

47 args = []

48 if config.strict:

49 args.append("--fail-on-warnings")

50 else:

51 args.extend(["--threshold", "error"])

52

53 if Path(config.tsconfig).exists():

54 args.extend(["--tsconfig", config.tsconfig])

55

56 return run_tool(executable, *args)