Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage Deno dependencies in one place

Tags:

deno

In Deno it is possible to version the dependencies in the import statement, and there's no package.json like with npm.

But how to managed it's URLs and versions in a single place?

I will have multiple files and dependencies that will be declared all over my system.

For example:

dateUtils.ts

import { parseDate } from 'https://deno.land/[email protected]/datetime/mod.ts';

const DEFAULT_MASK = "dd-mm-yyyy";

export function parse(date: string): Date {
    return parseDate(date, DEFAULT_MASK);
};

service.ts

import { v4 } from "https://deno.land/std/uuid/mod.ts";

export function process(request: any) {
    const { token } = request;
    const isValid = v4.validate(token);

    console.log(`Token validity: ${isValid}`)

};

app.ts

import { parse } from "./dateUtil.ts";
import * as service from "./service.ts";

const date = parse("20-05-2020");
console.log(date);

const request = {
    token: "408d30e5-1e90-45e3-b299-6520feee6d76"
}
service.process(request)
like image 594
Evandro Pomatti Avatar asked May 20 '20 16:05

Evandro Pomatti


2 Answers

To avoid typing https://deno.land/std/uuid/mod.ts everywhere you can use import maps

// import_map.json

{
   "imports": {
      "http/": "https://deno.land/std/http/"
   }
}
// server.ts
import { serve } from "http/server.ts";

const body = new TextEncoder().encode("Hello World\n");
for await (const req of serve(":8000")) {
  req.respond({ body });
}
deno run --importmap=import_map.json --allow-net server.ts

import maps are currently unstable, but you can use them behind --unstable flag.


Aside from that it's common to have a deps.ts file where you'll re-export all your dependencies.

// deps.ts
export * as path from "https://deno.land/[email protected]/path/mod.ts";
// other exports .. from
// some other file
import { path } from './deps.ts' // instead of "https://deno.land/[email protected]/path/mod.ts"
path.basename('/bar/test.txt');
like image 98
Marcos Casagrande Avatar answered Oct 20 '22 15:10

Marcos Casagrande


In deno there is a convention for consolidating imports into deps.ts.

export { parseDate } from 'https://deno.land/[email protected]/datetime/mod.ts';
export { v4 } from "https://deno.land/std/uuid/mod.ts";

Exports can then be imported in other modules/scripts on your application:

import { v4 } from "./deps.ts";
import { parseDate } from './deps.ts';

const myUUID = v4.generate();
parseDate("20-05-2020", "dd-mm-yyyy")

When using this approach one should consider integrity checking & lock files:

// Add a new dependency to "src/deps.ts", used somewhere else.
export { xyz } from "https://unpkg.com/[email protected]/lib.ts";
# Create/update the lock file "lock.json".
deno cache --lock=lock.json --lock-write src/deps.ts

# Include it when committing to source control.
git add -u lock.json
git commit -m "feat: Add support for xyz using xyz-lib"
git push
like image 35
Evandro Pomatti Avatar answered Oct 20 '22 17:10

Evandro Pomatti