about summary refs log tree commit diff
path: root/pkgs/by-name/sp/spectral-language-server/package.nix
blob: 8193ff3acebd0768131c76a9bfab5b5aca70ec58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
{ lib
, buildNpmPackage
, mkYarnPackage
, fetchYarnDeps
, fetchFromGitHub
, typescript
, jq
, fetchpatch
}:
let
  # Instead of the build script that spectral-language-server provides (ref: https://github.com/luizcorreia/spectral-language-server/blob/master/script/vscode-spectral-build.sh), we build vscode-spectral manually.
  # This is because the script must go through the network and will not work under the Nix sandbox environment.
  vscodeSpectral = mkYarnPackage rec {
    pname = "vscode-spectral";
    version = "1.1.2";

    src = fetchFromGitHub {
      owner = "stoplightio";
      repo = "vscode-spectral";
      rev = "v${version}";
      hash = "sha256-TWy+bC6qhTKDY874ORTBbvCIH8ycpmBiU8GLYxBIiAs=";
    };

    packageJSON = ./package.json;

    offlineCache = fetchYarnDeps {
      yarnLock = src + "/yarn.lock";
      hash = "sha256-am27A9VyFoXuOlgG9mnvNqV3Q7Bi7GJzDqqVFGDVWIA=";
    };

    nativeBuildInputs = [ typescript jq ];

    postPatch = ''
      cp server/tsconfig.json server/tsconfig.json.bak
      jq '.compilerOptions += {"module": "NodeNext", "moduleResolution": "NodeNext"}' server/tsconfig.json.bak > server/tsconfig.json
    '';

    dontConfigure = true;

    buildPhase = ''
      runHook preBuild
      # FIXME: vscode-spactral depends on @rollup/pluginutils, but it may have a bug that doesn't provide the type definitions for NodeNext module resolution. (ref: https://github.com/rollup/plugins/issues/1192)
      # tsc detects some type errors in the code. However we ignore this because it's not a problem for the final build if server/dist is generated.
      tsc -p server || true
      test -d server/dist
      runHook postBuild
    '';

    installPhase = ''
      runHook preInstall
      mkdir -p $out
      cp -R server/dist $out
      runHook postInstall
    '';

    doDist = false;

    meta = with lib; {
      homepage = "https://github.com/stoplightio/vscode-spectral";
      description = "VS Code extension bringing the awesome Spectral JSON/YAML linter with OpenAPI/AsyncAPI support";
      license = licenses.asl20;
    };
  };
in
buildNpmPackage rec {
  pname = "spectral-language-server";
  version = "1.0.8-unstable-2023-06-06";

  src = fetchFromGitHub {
    owner = "luizcorreia";
    repo = "spectral-language-server";
    rev = "c9a7752b08e6bba937ef4f5435902afd41b6957f";
    hash = "sha256-VD2aAzlCnJ6mxPUSbNRfMOlslM8kLPqrAI2ah6sX9cU=";
  };

  npmDepsHash = "sha256-ixAXy/rRkyWL3jdAkrXJh1qhWcKIkr5nH/Bhu2JV6k8=";

  patches = [
    # https://github.com/luizcorreia/spectral-language-server/pull/15
    (fetchpatch {
      name = "fix-package-lock.patch";
      url = "https://github.com/luizcorreia/spectral-language-server/commit/909704850dd10e7b328fc7d15f8b07cdef88899d.patch";
      hash = "sha256-+mN93xP4HCll4dTcnh2W/m9k3XovvgnB6AOmuJpZUZ0=";
    })
  ];

  dontNpmBuild = true;

  npmFlags = [ "--ignore-scripts" ];

  installPhase = ''
    runHook preInstall
    mkdir -p $out/bin
    mkdir -p $out/node_modules
    mkdir -p $out/dist/spectral-language-server
    cp -R ${vscodeSpectral}/dist/* $out/dist/spectral-language-server/
    cp ./bin/* $out/bin
    cp -R ./node_modules/* $out/node_modules
    runHook postInstall
  '';

  meta = with lib; {
    homepage = "https://github.com/luizcorreia/spectral-language-server";
    description = "Awesome Spectral JSON/YAML linter with OpenAPI/AsyncAPI support";
    maintainers = with maintainers; [ momeemt ];
    license = licenses.mit;
    mainProgram = "spectral-language-server";
  };
}