deno.land / x / lume@v2.1.4 / core / utils / deno_config.ts

deno_config.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { brightGreen, gray } from "../../deps/colors.ts";import { dirname, extname, join } from "../../deps/path.ts";import { parse } from "../../deps/jsonc.ts";import { isUrl } from "../utils/path.ts";
/** Import map file */export interface ImportMap { imports: Record<string, string>; scopes?: Record<string, Record<string, string>>;}
/** Basic options for deno.json file */export interface DenoConfig extends Partial<ImportMap> { importMap?: string; tasks?: Record<string, string>; compilerOptions?: { jsx?: "jsx" | "react-jsx" | "precompile"; jsxImportSource?: string; types?: string[]; }; [key: string]: unknown;}
export interface DenoConfigResult { file: string; config: DenoConfig; importMap?: ImportMap;}
/** Detect and returns the Deno configuration */export async function readDenoConfig(): Promise<DenoConfigResult | undefined> { for (const file of ["deno.json", "deno.jsonc"]) { try { const content = await Deno.readTextFile(file); const config = parse(content) as DenoConfig; let importMap: ImportMap | undefined;
if (config.importMap) { importMap = isUrl(config.importMap) ? await (await fetch(config.importMap)).json() : await JSON.parse(await Deno.readTextFile(config.importMap)); } else if (config.imports) { importMap = { imports: config.imports, scopes: config.scopes, }; } return { file, config, importMap }; } catch (err) { if (err instanceof Deno.errors.NotFound) { continue; }
throw err; } }}
/** Write the Deno configuration file */export async function writeDenoConfig(options: DenoConfigResult) { const { file, config, importMap } = options;
if (importMap && !config.importMap) { config.imports = importMap.imports; config.scopes = importMap.scopes; }
if (config.importMap) { const importMapFile = join(dirname(file), config.importMap); await Deno.writeTextFile( importMapFile, JSON.stringify(importMap, null, 2) + "\n", ); console.log(brightGreen("Import map file saved:"), importMapFile); }
if (extname(file) === ".jsonc") { const save = confirm( "Saving the deno.jsonc file will overwrite the comments. Continue?", );
if (!save) { console.log( "You have to update your deno.jsonc file manually with the following content:", ); console.log(gray(JSON.stringify(config, null, 2))); console.log("Use deno.json to update it automatically without asking."); return; } } await Deno.writeTextFile(file, JSON.stringify(config, null, 2) + "\n"); console.log("Deno configuration file saved:", gray(file));}
/** Update the Lume import map and tasks in the Deno configuration file */export function updateLumeVersion(url: URL, denoConfig: DenoConfigResult) { denoConfig.importMap ??= { imports: {} };
const { config, importMap } = denoConfig;
const oldUrl = importMap.imports["lume/"]; const newUrl = new URL("./", url).href; importMap.imports["lume/"] = newUrl;
for (const [specifier, url] of Object.entries(importMap.imports)) { if (url.startsWith(oldUrl)) { importMap.imports[specifier] = url.replace(oldUrl, newUrl); } }
// Configure lume tasks const tasks = config.tasks || {}; if (!tasks.lume || !tasks.lume.includes(`echo "import 'lume/cli.ts'"`)) { tasks.lume = `echo "import 'lume/cli.ts'" | deno run -A -`; tasks.build = "deno task lume"; tasks.serve = "deno task lume -s"; } config.tasks = tasks;
// Configure the compiler options const compilerOptions = config.compilerOptions || {}; compilerOptions.types = compilerOptions.types || []; if (!compilerOptions.types.includes("lume/types.ts")) { compilerOptions.types.push("lume/types.ts"); } config.compilerOptions = compilerOptions;}
lume

Version Info

Tagged at
7 months ago