-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathplaywright-cli.js
More file actions
executable file
·115 lines (99 loc) · 3.18 KB
/
Copy pathplaywright-cli.js
File metadata and controls
executable file
·115 lines (99 loc) · 3.18 KB
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
#!/usr/bin/env node
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// @ts-check
const fs = require('fs');
const path = require('path');
const { program } = require('playwright-core/lib/tools/cli-client/program');
const coreBundle = require('playwright-core/lib/coreBundle');
const { tools, registry } = coreBundle;
const { checkInstalledSkills, frame } = require('./skillCheck');
const packageJson = require('./package.json');
const ONE_DAY_MS = 24 * 60 * 60 * 1000;
main();
async function main() {
const command = process.argv.slice(2).find(arg => !arg.startsWith('-'));
if (command !== 'install')
checkInstalledSkills();
await notifyAboutUpdate().catch(() => {});
program({ embedderVersion: packageJson.version });
}
async function notifyAboutUpdate() {
if (process.env.NO_UPDATE_NOTIFIER || process.env.CI)
return;
const cache = readCache();
const stale = !cache || (Date.now() - cache.lastCheck) > ONE_DAY_MS;
if (!stale)
return;
const latest = await fetchLatestVersion();
if (!latest)
return;
writeCache({ lastCheck: Date.now(), latestVersion: latest });
if (tools.compareSemver(latest, packageJson.version) > 0)
printNotice(packageJson.version, latest);
}
async function fetchLatestVersion() {
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 1500);
try {
const res = await fetch(`https://registry.npmjs.org/${packageJson.name}/latest`, { signal: controller.signal });
if (!res.ok)
return undefined;
const json = await res.json();
return typeof json.version === 'string' ? json.version : undefined;
} finally {
clearTimeout(timeout);
}
} catch {
return undefined;
}
}
/**
*
* @param {string} current
* @param {string} latest
*/
function printNotice(current, latest) {
process.stderr.write('\n' + frame([
`Update available for ${packageJson.name}: ${current} → ${latest}`,
`Run \`npm install -g ${packageJson.name}@latest\` (global) or`,
`\`npm install --save-dev ${packageJson.name}@latest\` (local) to update.`,
]) + '\n');
}
function cacheFile() {
return path.join(registry.defaultRegistryDirectory, 'cli-update-check.json');
}
function readCache() {
try {
const data = JSON.parse(fs.readFileSync(cacheFile(), 'utf8'));
if (typeof data.lastCheck === 'number' && typeof data.latestVersion === 'string')
return data;
} catch {
}
return undefined;
}
/**
* @param {*} data
*/
function writeCache(data) {
try {
const file = cacheFile();
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, JSON.stringify(data));
} catch {
}
}