-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathhammer.mjs
82 lines (81 loc) · 3.92 KB
/
hammer.mjs
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
import * as Http from 'node:http'
import * as Path from 'node:path'
import * as Fs from 'node:fs'
// -------------------------------------------------------------------------------
// Clean
// -------------------------------------------------------------------------------
export async function clean() {
await folder('target').delete()
}
// -------------------------------------------------------------------------------
// Format
// -------------------------------------------------------------------------------
export async function format() {
await shell('prettier --no-semi --single-quote --print-width 240 --trailing-comma all --write src test example')
}
// -------------------------------------------------------------------------------
// Start
// -------------------------------------------------------------------------------
export async function start(target = 'target/example') {
const worker = shell(`hammer watch src/proxy/service/worker.mts --dist ${target}`)
const start = shell(`hammer serve example/index.html --dist ${target}`)
const drift = shell(`drift wait 100 url http://localhost:5000`)
await Promise.all([worker, start, drift])
}
// -------------------------------------------------------------------------------
// Chrome
// -------------------------------------------------------------------------------
async function chrome_warmup() {
// Chrome will not have been run in CI environment due to fresh install. It's been
// noted that initialization of Chrome seems to involve running a latent optimization
// process on the user directory which takes some time to complete. The following just
// runs chrome, waits 8 seconds and exits.
await shell(`drift wait 8000 close`)
}
// -------------------------------------------------------------------------------
// Test
// -------------------------------------------------------------------------------
export async function test_serve(target = 'target/test') {
await shell(`hammer serve test/index.html --dist ${target}`)
}
function test_server(target = 'target/test', port = 5010) {
return Http.createServer((req, res) => {
const [path, extname] = [Path.join(target, req.url), Path.extname(req.url)]
if(Fs.existsSync(path) && Fs.statSync(path).isFile()) {
if(extname === '.js') res.writeHead(200, { 'Content-Type': 'application/javascript' })
res.end(Fs.readFileSync(path))
} else {
res.end('<html><head>NotFound</head></html>')
}
}).listen(port)
}
export async function test(filter = '', target = 'target/test') {
await chrome_warmup()
const server = test_server(target, 5010)
await shell(`hammer build src/proxy/service/worker.mts --dist ${target}`)
await shell(`hammer build test/index.mts --dist ${target} --platform node`)
await shell(`drift url http://localhost:5010 wait 1000 run ./${target}/index.mjs args "${filter}"`)
server.close()
}
// -------------------------------------------------------------------------------
// Build
// -------------------------------------------------------------------------------
export async function build(target = 'target/build') {
await clean()
await shell(`tsc -p src/tsconfig.json --outDir ${target} --declaration`)
await shell(`hammer build src/proxy/service/worker.mts --dist ${target}`)
await folder(target).add('package.json')
await folder(target).add('readme.md')
await folder(target).add('license')
await shell(`cd ${target} && npm pack`)
}
// -------------------------------------------------------------
// Publish
// -------------------------------------------------------------
export async function publish(otp, target = 'target/build') {
const { version } = JSON.parse(Fs.readFileSync('package.json', 'utf8'))
if(version.includes('-dev')) throw Error(`package version should not include -dev specifier`)
await shell(`cd ${target} && npm publish sinclair-smoke-${version}.tgz --access=public --otp ${otp}`)
await shell(`git tag ${version}`)
await shell(`git push origin ${version}`)
}