Skip to content

Commit 14eff6f

Browse files
committed
chore: improve deno cache script
1 parent 3bf2f62 commit 14eff6f

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

packages/apps-engine/scripts/deno-cache.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,30 @@ const fs = require('fs');
33
const path = require('path');
44

55
const SHELL_ERR_CMD_NOT_FOUND = 127;
6+
const { CI } = process.env;
67

78
/**
89
* Matches 'deno 2.3.1' or 'Deno 2.7.11-alpha3.24' or even 'some deno and-anything in between 1.43.5' (as long as everything is in the same line)
910
* and extracts the correct version string from those ('2.3.1', '2.7.11' and '1.43.5' respectively).
1011
*
1112
* Doesn't match 'denoing 2.3.1' or 'deno2.3.1' or 'mydeno 2.7.11alpha3.24' or 'deno\n1.43.5'
13+
*
14+
* The expression gets a bit complicated because the word boundary assertion (\b) identifies the dash (-) as a valid word boundary,
15+
* but that is not the case for use, as we don't want to match "make-deno" for instance. So, for correctness, we use a negative lookbehind
16+
* assertion ("(?<!)") BEFORE words to make sure our match is not preceded by a word character (\w), a dash (-) or a dot (.), e.g. it won't
17+
* match "mydeno", "my-deno", "my.deno", etc. The negative lookbehind also allows us to match "deno" in the start of the input, something that
18+
* simply using the expression itself wouldn't match. In most other cases, these would be replaced by simply "\b"
19+
*
20+
* The exact expression tries find the first line to match a sequence as follows:
21+
* - A character "D" or "d" that is not preceded by a word character, dash or dot (negative lookbehind)
22+
* - Followed by the literal sequence "eno"
23+
* - Followed by any character that is not a word character, dash or dot
24+
* - Followed by a sequence of zero or more occurrences of any character (non multi line)
25+
* - Followed by a sequence of one or more numbers, then a dot, then one or more numbers, then a dot, then one or more numbers ("version" capture group)
26+
* that is NOT preceded by a word character, a dash, or dot (negative lookbehind)
27+
* - Followed by a word boundary (here we're less picky with the "\b" assertion, as we're out of the capture group)
1228
*/
13-
const extractDenoVersion = (input) => /\bdeno\b.*\b(?<version>\d+\.\d+\.\d+)(?<prerelease>-[\w.-]+)?\b/i.exec(input)?.groups?.version;
29+
const extractDenoVersion = (input) => /(?<![\w-.])[Dd]eno[^\w-.].*\b(?<![\w-.])(?<version>\d+\.\d+\.\d+)\b/.exec(input)?.groups?.version;
1430

1531
try {
1632
const toolVersionsPath = path.resolve(__dirname, '..', '..', '..', '.tool-versions');
@@ -29,7 +45,14 @@ try {
2945
}
3046

3147
if (installedVersion !== denoToolVersion) {
32-
throw new Error(`Incorrect Deno version. Required '${denoToolVersion}', found '${installedVersion}'`);
48+
const message = `Incorrect Deno version. Required '${denoToolVersion}', found '${installedVersion}'.${!CI && " The server will likely work, but it may cause your deno.lock to change - do not to commit it. Make sure your Deno version matches the required one so you don't see this message again."}`;
49+
50+
if (CI) {
51+
throw new Error(message);
52+
}
53+
54+
// We don't need to fail if a dev environment doesn't have a matching Deno version, just the warning is enough
55+
console.warn(message);
3356
}
3457
} catch (e) {
3558
if (e.status === SHELL_ERR_CMD_NOT_FOUND) {
@@ -43,7 +66,7 @@ try {
4366
),
4467
);
4568
} else {
46-
console.error(e);
69+
console.error(e, e.message, e.message?.startsWith('Error: Incorrect deno version.'));
4770
}
4871

4972
process.exit(1);
@@ -53,7 +76,10 @@ const rootPath = path.join(__dirname, '..');
5376
const denoRuntimePath = path.join(rootPath, 'deno-runtime');
5477
const DENO_DIR = process.env.DENO_DIR ?? path.join(rootPath, '.deno-cache');
5578

56-
childProcess.execSync('deno install --frozen --entrypoint main.ts', {
79+
// In CI envs, break if lockfile changes; in dev envs, don't update the lockfile
80+
const commandLine = CI ? 'deno install --frozen --entrypoint main.ts' : 'deno install --entrypoint main.ts';
81+
82+
childProcess.execSync(commandLine, {
5783
cwd: denoRuntimePath,
5884
env: {
5985
DENO_DIR,

0 commit comments

Comments
 (0)