Deno: How to substitute npm scripts (package.json)
Asked Answered
D

5

14

Since it is not necessary to have package.json file with deno, how can I as a developer have a similar experience just as we have with npm scripts in package.json?

Double answered 12/5, 2020 at 22:54 Comment(1)
Deno 1.31 presents the support for package.json. You can define your scripts as you would in node.jsDouble
D
9

velociraptor may be of help, especially if you want to run arbitrary shell scripts.

It accepts yaml, json and ts script configuration files. The following example illustrates the main features:

# scripts.yaml
scripts:

  start: deno run server.ts # Scripts can be simple command strings

  opts:                     # Or objects
    cmd: deno run server.ts
    desc: Starts the server
    tsconfig: tsconfig.json # Deno cli options
    imap: importmap.json
    allow:
      - read
      - net
    env:                    # Env vars
      PORT: 8080

  compact: server.ts        # `deno run` is automatically prepended
                            # when the script starts with a .ts file

  multiple:                 # Lists of commands are executed in series
    - echo one
    - echo two

  concurrent:               # Use the pll property to declare
    pll:                    # concurrent scripts
      - echo one
      - echo two

env:                        # Top level options are sent to all the scripts
  PORT: 3000
allow:
  - write

Run vr without arguments to see a list of available scripts. To execute a script run:

$ vr <script name> [additional args]...
# or
$ vr run <script name> [additional args]...
# Additional args are passed to the script

ie

vr start

Disclaimer: I'm the author

Drumlin answered 18/5, 2020 at 8:53 Comment(0)
T
7

deno install

You can create executable, aliased scripts with deno install.

It will provide a thin shell/cmd wrapper for the specified main module and CLI arguments. Example:

deno install --root . -n serve --allow-read --allow-net https://deno.land/[email protected]/http/file_server.ts

The result is a serve script, similar to npm "scripts": { "serve": ... }:

./bin/serve # run `serve` script (~ npm run serve)

If the project's bin folder is added to PATH environment, the command shortens up to serve.

What deno install does

  • creates bin folder, if not existent
  • adds serve/serve.cmd file with following content (Windows here):
    % generated by deno install %
    

@deno.exe "run" "--allow-read" "--allow-net" "https://deno.land/[email protected]/http/file_server.ts" %* ```

  • -n is the command name used later on (can be left out)
  • --root specifies the bin root location (otherwise ~/.deno)
  • -f option overwrites an existent alias

Side note: Any .js/.ts script is a valid reference - source code location (local/URL) does not matter. If external shell scripts are to be included, you can also run them inside a subprocess.

Example: Custom npm run build

// ./scripts/build.ts

// create subprocess
const p = Deno.run({
  cmd: ["deno", "cache", "-r", "--unstable", "main.ts"],
});

await p.status();
deno install --root . --allow-run scripts\build.ts

./bin/build # execute build script

Built-in deno commands

Deno already comes with built-in solutions for common ecosystem tasks, e.g. bundle, fmt, test and lint later on (see deno help). You can invoke these commands directly - no need to define custom scripts:

deno test
deno fmt
deno cache -r main.ts # similar to `npm run build` / `tsc`
# ...
Thora answered 2/6, 2020 at 12:46 Comment(0)
P
6

I've been working on a solution that mimics the package.json scripts sections while adding some Deno specific functionalities.

You need first to install denox you can find the instructions here https://github.com/BentoumiTech/denox

Then create a .deno-workspace file where you specify your scripts list :

scripts:
  # "denox run start" will execute main.ts with example.com networking permissions
  start:
    file: main.ts
    permissions:
      allow-net: example.com
  # "denox run develop" will execute main.ts with localhost networking permissions
  develop:
    file: main.ts
    permissions:
      allow-net: localhost

You can then run

  1. $ denox run start
  2. $ denox run develop
Premiere answered 13/5, 2020 at 16:13 Comment(5)
Great! How about --reload? Where should I specify it?Double
@Cea as a temporary solution you can add it in the permissions, I'm currently working on adding support for more optionsPremiere
@Cea if you need guidance for using the tool I'll be happy to walk you throughPremiere
Thanks! Currently working on another project so I could not give a try for the denox, yet. The --reload just flashed through my mind and that's why I asked it. I'll let you know if I come across any obstacles.Double
how would this incorporate with a pnpm workspace(monorepo) where deno is a project?Pedestrianize
W
5

Deno has a built-in Task Runner since v1.20 (released Mar 17, 2022).

From the docs (as of Oct 16, 2023):

Task Runner

deno task provides a cross platform way to define and execute custom commands specific to a codebase.

To get started, define your commands in your codebase's Deno configuration file under a "tasks" key.

For example:

{
  "tasks": {
    "data": "deno task collect && deno task analyze",
    "collect": "deno run --allow-read=. --allow-write=. scripts/collect.js",
    "analyze": "deno run --allow-read=. scripts/analyze.js"
  }
}

Listing tasks

To get an output showing all the defined tasks, run:

deno task

Executing a task

To execute a specific task, run:

deno task task-name [additional args]...

In the example above, to run the data task we would do:

deno task data

More details in the Task Runner docs. (This link points to the latest version's docs.)

Weald answered 16/10, 2022 at 12:51 Comment(0)
P
1

You can create your own files as denoDept.js

export { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
export { green, bold } from "https://deno.land/[email protected]/fmt/colors.ts";

And you can add all your dependencies in a single file and use it so it will look like package manager.

Primogeniture answered 18/5, 2020 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.