refactor: Fix and rename arg names (#9)

This commit is contained in:
SnO₂WMaN 2022-09-10 04:06:41 +09:00 committed by GitHub
parent 638631c842
commit a2092563c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 78 deletions

View file

@ -22,26 +22,31 @@ deno cache --import-map=./import_map.json --lock lock.json --lock-write ./mod.ts
inputs.devshell.url = "github:numtide/devshell";
outputs = {
deno2nix,
self,
nixpkgs,
flake-utils,
...
}:
} @ inputs:
flake-utils.lib.eachDefaultSystem (system: let
inherit (pkgs) deno2nix;
pkgs = import nixpkgs {
inherit system;
overlays = [
overlays = with inputs; [
devshell.overlay
deno2nix.overlay
];
};
in {
packages.executable = pkgs.deno2nix.mkExecutable {
name = "example";
version = "0.1.0";
src = self;
packages.executable = deno2nix.mkExecutable {
pname = "example-executable";
version = "0.1.2";
src = ./.;
lockfile = ./lock.json;
importmap = ./import_map.json;
entrypoint = ./mod.ts;
output = "example";
entrypoint = "./mod.ts";
importMap = "./import_map.json";
};
});
}
@ -52,20 +57,18 @@ deno cache --import-map=./import_map.json --lock lock.json --lock-write ./mod.ts
#### Args
```nix
{
name,
{
pname,
version,
src,
entrypoint,
lockfile,
importmap ? null,
denoFlags ? [],
output ? pname, # generate binary name
entrypoint,
importMap ? null, # ex. "./import_map.json" to $src/${importMap}
additionalDenoFlags ? "", # ex. "--allow-net"
}
```
- `importMap = ./import_map.json`
- `denoFlags = ["--allow-net" true]`
## Thanks
- [esselius/nix-deno](https://github.com/esselius/nix-deno)

View file

@ -1,6 +1,7 @@
{
"tasks": {
"run": "deno run --import-map=./import_map.json ./mod.ts",
"compile": "deno compile --import-map=./import_map.json ./mod.ts",
"cache": "deno cache --import-map=./import_map.json --lock lock.json --lock-write ./mod.ts"
},
"fmt": {

View file

@ -40,35 +40,44 @@
in {
packages.depslink = deno2nix.internal.mkDepsLink ./lock.json;
packages.bundled = deno2nix.mkBundled {
name = "example";
pname = "example-bundled";
version = "0.1.0";
src = ./.;
lockfile = ./lock.json;
importMap = ./import_map.json;
entrypoint = ./mod.ts;
output = "bundled.js";
entrypoint = "./mod.ts";
importMap = "./import_map.json";
};
packages.wrapper = deno2nix.mkBundledWrapper {
name = "example";
packages.bundled-wrapper = deno2nix.mkBundledWrapper {
pname = "example-bundled-wrapper";
version = "0.1.0";
src = ./.;
lockfile = ./lock.json;
importMap = ./import_map.json;
entrypoint = ./mod.ts;
output = "bundled.js";
entrypoint = "./mod.ts";
importMap = "./import_map.json";
};
packages.executable = deno2nix.mkExecutable {
name = "example";
version = "0.1.0";
pname = "example-executable";
version = "0.1.2";
src = ./.;
lockfile = ./lock.json;
importMap = ./import_map.json;
entrypoint = ./mod.ts;
output = "example";
entrypoint = "./mod.ts";
importMap = "./import_map.json";
};
packages.default = self.packages.${system}.executable;
defaultPackage = self.packages.${system}.default;
apps.default = flake-utils.lib.mkApp {
drv = self.packages.${system}.executable;
};
apps.bundled-wrapper = flake-utils.lib.mkApp {drv = self.packages.${system}.bundled-wrapper;};
apps.executable = flake-utils.lib.mkApp {drv = self.packages.${system}.executable;};
apps.default = self.apps.${system}.executable;
checks = self.packages.${system};

View file

@ -4,9 +4,14 @@
deno2nix,
writeShellScriptBin,
...
}: {name, ...} @ args: let
}: {
pname,
bin ? pname,
output ? "bundled.js",
...
} @ args: let
bundled = deno2nix.mkBundled args;
in
writeShellScriptBin
"${name}"
"${deno}/bin/deno run ${bundled}/dist/bundled.js"
bin
"${deno}/bin/deno run ${bundled}/dist/${output}"

View file

@ -5,43 +5,38 @@
lib,
...
}: {
name,
pname,
version,
src,
entrypoint,
lockfile,
output ? "bundled.js",
entrypoint,
importMap ? null,
denoFlags ? [],
additionalDenoFlags ? "",
}: let
inherit (deno2nix.internal) mkDepsLink;
in
stdenv.mkDerivation {
inherit name version entrypoint;
denoFlags =
denoFlags
++ (
if importMap != null
then ["--import-map" importMap]
else []
);
src = lib.cleanSourceWith {
inherit src;
filter = path: type: (baseNameOf path != "bundled.js");
};
buildInputs = with pkgs; [
deno
jq
];
inherit pname version entrypoint src;
buildInputs = with pkgs; [deno jq];
buildPhase = ''
export DENO_DIR=`mktemp -d`
ln -s "${mkDepsLink lockfile}" $(deno info --json | jq -r .modulesCache)
deno bundle $denoFlags $entrypoint bundled.js
deno bundle \
--lock="${lockfile}" \
${
if importMap != null
then "--import-map=\"$src/${importMap}\""
else ""
} \
${additionalDenoFlags} \
"$src/${entrypoint}" \
"${output}"
'';
installPhase = ''
mkdir -p $out/dist
install -t $out/dist bundled.js
install -t $out/dist "${output}"
'';
}

View file

@ -4,39 +4,41 @@
deno2nix,
...
}: {
name,
pname,
version,
src,
entrypoint,
lockfile,
output ? pname,
entrypoint,
importMap ? null,
denoFlags ? [],
additionalDenoFlags ? "",
}: let
inherit (deno2nix.internal) mkDepsLink;
in
stdenv.mkDerivation {
inherit src name entrypoint;
denoFlags =
denoFlags
++ ["--lock" lockfile]
++ ["--cached-only"]
++ ["--output" name]
++ (
if importMap != null
then ["--import-map" importMap]
else []
);
buildInputs = with pkgs; [deno jq];
fixupPhase = ":";
inherit pname version src;
dontFixup = true;
buildInputs = with pkgs; [deno jq];
buildPhase = ''
export DENO_DIR=`mktemp -d`
ln -s "${mkDepsLink lockfile}" $(deno info --json | jq -r .modulesCache)
deno compile $denoFlags "$entrypoint"
deno compile \
--cached-only \
--lock="${lockfile}" \
--output="${output}" \
${
if importMap != null
then "--import-map=\"$src/${importMap}\""
else ""
} \
${additionalDenoFlags} \
"$src/${entrypoint}"
'';
installPhase = ''
mkdir -p $out/bin
mv "$name" "$out/bin/"
cp "${output}" "$out/bin/"
'';
}