summarylogtreecommitdiffstats
path: root/wxvpkg_pack.js
blob: 6918c5269eaad0c516285d6e73963946168a808f (plain)
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
#!/usr/bin/env node

// https://gist.github.com/chemzqm/9f2334ca201dc2fbc363fdd757aa2ed4
const path = require('path')
const fs = require('fs')
const { execSync } = require('child_process')

const args = process.argv.slice(2);
const from = args[0]
const to = args[1]

let file = to
console.log(file)
if (fs.existsSync(file)) {
  execSync(`rm -rf ${file}`)
}

let fd = fs.openSync(file, 'w')
let dest = from

function writeSync(buf, start) {
  fs.writeSync(fd, buf, 0, buf.length, start)
}

function writeInt32(number, start) {
  let buf = Buffer.alloc(4)
  buf.writeInt32BE(number, 0)
  writeSync(buf, start)
}

let files = fs.readdirSync(dest)
let totalCount = files.length
let buf = Buffer.alloc(4)
buf.writeInt32BE(totalCount, 0)
writeSync(buf, 14)
let start = 18
// 12 + /name.length
let dataOffset = start
for (let file of files) {
  let name = `/${file}`
  let buf = Buffer.from(name, 'utf8')
  dataOffset = dataOffset + 12 + buf.length
}

for (let file of files) {
  let nb = Buffer.from(`/${file}`, 'utf8')
  // write filename byte length
  writeInt32(nb.length, start)
  start += 4
  // write filename
  writeSync(nb, start)
  start += nb.length
  // write offset
  writeInt32(dataOffset, start)
  start += 4
  // write length
  let contentBuf = fs.readFileSync(path.join(dest, file))
  writeInt32(contentBuf.length, start)
  start += 4
  // write content
  writeSync(contentBuf, dataOffset)
  dataOffset += contentBuf.length
}

fs.closeSync(fd)