DrSensor / modern-nix-templates

Modern nix flake templates that only use both fastest and leanest toolchains
BSD Zero Clause License
0 stars 0 forks source link

Add template for Node.js #6

Open DrSensor opened 3 years ago

DrSensor commented 3 years ago

Programming language JavaScript and Typescript

Toolchains

Static Code Analysis a.k.a linter

Code Formatter

prettier or gts

Update Dependencies

Security Audit

npm audit

npm audit

Others

DrSensor commented 3 years ago

Draft

{
        javascript-node-env = {
          mkShell =
            { src ? ./.
            , lockfile ? src + "/package-lock.nix"
            , nixLock ? if hasSuffix ".nix" lockfile then lockfile else src + "/package-lock.nix"
            , yarnLock ? if hasSuffix ".lock" lockfile then lockfile else src + "/yarn.lock"
            , npmLock ? if hasSuffix ".json" lockfile then lockfile else src + "/package-lock.json"
            , pnpmLock ? if hasSuffix ".yaml" lockfile then lockfile else src + "/pnpm-lock.yaml"
            , nodejs ? pkgs.nodejs
            , ...
            }@attrs: with pkgs.lib; let
              buildNixLockCommand = lock: optionalString (isString lock) "anylock --as=nix --lockfile=${lock} --no-patch > ${nixLock}";
              mkNixLock = lock: runCommandLocal "package-lock.nix" { } (buildNixLockCommand lock);
            in
            if pathExists nixLock then
              ...
            else
              mkShell {
                packages = with pkgs.nodePackages; [ nodejs ]
                  ++ optional (hasInfix "slim" nodejs.name || lockfile == npmLock) npm
                  ++ optional (lockfile == yarnLock) yarn
                  ++ optional (lockfile == pnpmLock) pnpm
                  ++ [ anylock ];
                shellHook =
                  let
                    lockExists = lock: (lockfile == lock) && pathExists lock;
                    lockNotExists = lock: (lockfile == lock) && !(pathExists lock);
                    defaultLockfile =
                      if lockfile == npmLock then "package-lock.json" else
                      if lockfile == yarnLock then "yarn.lock" else
                      if lockfile == pnpmLock then "pnpm-lock.yaml" else
                      lockfile;
                    notExists-then = cmd: lock: optionalString (any lockNotExists (toList lock)) (cmd + "\n");
                    exists-then = cmd:
                      if isList cmd then optionalString (lockExists (last cmd)) ((concatStrings cmd) + defaultLockfile + "\n")
                      else lock: optionalString (lockExists lock) (cmd + defaultLockfile + "\n");
                  in
                  (notExists-then "npm install --package-lock-only" [ npmLock yarnLock pnpmLock ])
                    + (notExists-then "yarn import" yarnLock)
                    + (notExists-then "pnpm import" pnpmLock)
                    + (concatMapStrings (lock: exists-then [ "cp" lock ]) [ npmLock yarnLock pnpmLock ])
                    + (buildNixLockCommand defaultLockfile)
                    + (concatMapStrings (lock: exists-then "rm" lock) [ npmLock yarnLock pnpmLock ])
                    + ''git add ${lockfile}
                    echo -e "\nPlease run$(tput bold) nix develop $(tput sgr0)again."
                    exit
                  '';
              };
        };
}