Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
48 lines (45 loc) · 1.61 KB
/
Copy pathcli.ts
File metadata and controls
48 lines (45 loc) · 1.61 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
#!/usr/bin/env node
import { createHash } from 'node:crypto'
import { lstat, readdir, readFile } from 'node:fs/promises'
import { isAbsolute, relative, resolve } from 'node:path'
import { create } from 'tar'
import { validateManifest } from './validation.js'
const [command, directory = 'dist', output] = process.argv.slice(2)
if (command !== 'pack') {
console.error('Usage: kite-plugin pack [dist-directory] [output.tar.gz]')
process.exit(1)
}
const root = resolve(directory)
const manifest: unknown = JSON.parse(
await readFile(resolve(root, 'plugin.json'), 'utf8')
)
validateManifest(manifest)
for (const path of [manifest.entry, ...(manifest.styles ?? [])]) {
const resolved = resolve(root, path)
const local = relative(root, resolved)
if (
isAbsolute(path) ||
local.startsWith('../') ||
local === '..' ||
!(await lstat(resolved)).isFile()
) {
throw new Error(`Plugin asset must be a regular file inside dist: ${path}`)
}
}
const files = await readdir(root, { recursive: true })
for (const file of files) {
const stat = await lstat(resolve(root, file))
if (!stat.isFile() && !stat.isDirectory())
throw new Error(`Unsupported package entry: ${file}`)
}
const archive = resolve(output ?? `${manifest.id}-${manifest.version}.tar.gz`)
if (!relative(root, archive).startsWith('../'))
throw new Error('Archive output must be outside the dist directory')
await create(
{ cwd: root, file: archive, gzip: true, portable: true },
(await readdir(root)).sort()
)
const checksum = createHash('sha256')
.update(await readFile(archive))
.digest('hex')
console.log(`${archive}\nsha256: ${checksum}`)