41271

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

<h3>Question</h3>

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?


<h3>Answer1:</h3>
<h2>deno install</h2>

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/std@0.54.0/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.

<h2>What deno install does</h2> <ul><li>creates bin folder, if not existent</li> <li>adds serve/serve.cmd file with following content (Windows here): % generated by deno install % @deno.exe "run" "--allow-read" "--allow-net" "https://deno.land/std@0.54.0/http/file_server.ts" %* </li> <li>-n is the command name used later on (can be left out)</li> <li>--root specifies the bin root location (otherwise ~/.deno)</li> <li>-f option overwrites an existent alias</li> </ul>

<em>Side note</em>: 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.

<h2>Built-in deno commands</h2>

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` # ... <h3>Example: Custom npm run build</h3> // ./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
<h3>Answer2:</h3>

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 :

<pre class="lang-yaml prettyprint-override">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

<ol><li>$ denox run start</li> <li>$ denox run develop</li> </ol>
<h3>Answer3:</h3>

You can create your own files as denoDept.js

export { assert } from "https://deno.land/std@v0.39.0/testing/asserts.ts"; export { green, bold } from "https://deno.land/std@v0.39.0/fmt/colors.ts";

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


<h3>Answer4:</h3>

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

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

<pre class="lang-yaml prettyprint-override"># 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:

<pre class="lang-sh prettyprint-override">$ vr <script name> [additional args]... # or $ vr run <script name> [additional args]... # Additional args are passed to the script

ie

<pre class="lang-sh prettyprint-override">vr start <hr />

Disclaimer: I'm the author

来源:https://stackoverflow.com/questions/61763366/deno-how-to-substitute-npm-scripts-package-json

Recommend

  • How do you use Keras LeakyReLU in Python?
  • is Security free in Elastic search Stack Features?
  • Python script won't exec from php when specific library is used
  • Refreshing a page with JavaScript href for Google Translate in Firefox
  • Microsoft speech API 5.1, 5.3?
  • Best way to send continuous data in Java using Netty
  • how java graphics repaint method actually works
  • Java: How to refer to subclass's static variable in abstract class?
  • 3 transitions, pausetime between transitions
  • Find string between two substrings AND between string and the end of file
  • Redmine can't generate secret token
  • Get an image from the video
  • GLConsumer is already attached to a context for new SurfaceTexture
  • What is the diff. between default.properties and project.properties?
  • Detect when MathJax has finished loading in UIWebView
  • Sorting Custom Listview Items Using Spinner Android
  • yii rewrite url with many sub categories
  • Authorize Attribute Authentication with Postman in Web Api
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • Exclude Classes from Being Included in Shaded Jar
  • Swift manually rotate view controller
  • How to make Rss News Reader application in android …? [closed]
  • 'Edit' function for forum posts and such
  • Set WebClient.Builder.exchangeStrategies() without losing Spring Jackson configuration
  • SQL Server 2012 not showing unicode character in results
  • Stop an element moving with padding on hover
  • Ruby regex for matching simpliest Ruby's regexes
  • How to write seo friendly url's using htaccess?
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • Terminal run dalvikvm with am.jar
  • Change cell value based on cell color in google spreadsheet
  • Android: Unable to detect vertical plane
  • Why isn't stemDocument stemming?
  • How to handle div that is created dynamically in a table
  • Debug `Unexpected end of JSON input Error` on content script
  • Make checkout phone field optional for specific countries in WooCommerce
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007
  • How to use FirstOrDefault inside Include
  • ReferenceError: TextEncoder is not defined