winzik/tools/chrome/pack.cjs

73 lines
3.8 KiB
JavaScript

// The Chrome Web Store package: bin/jumphp chrome:pack <folder>
// node pack.cjs <folder> -> dist/<name>-<version>.zip (manifest.json at the zip's root), after chrome:check passes.
// Left out: tests/, scripts/, var/, dist/, store/, node_modules/, .git, *.md, eslint / package.json, source maps,
// and what .packignore lists (one per line: folder/, a/path, *.ext).
'use strict';
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const { shippedFiles } = require('./common.cjs');
const { check } = require('./check.cjs');
const crcTable = Array.from({ length: 256 }, (_, n) => {
let c = n;
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
return c >>> 0;
});
const crc32 = (buf) => {
let c = 0xffffffff;
for (let i = 0; i < buf.length; i++) c = crcTable[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
return (c ^ 0xffffffff) >>> 0;
};
function zip(root, files) {
const locals = [], central = [];
let offset = 0;
const d = new Date();
const time = (d.getHours() << 11) | (d.getMinutes() << 5) | (d.getSeconds() >> 1);
const date = ((d.getFullYear() - 1980) << 9) | ((d.getMonth() + 1) << 5) | d.getDate();
for (const rel of files) {
const data = fs.readFileSync(path.join(root, rel));
const deflated = zlib.deflateRawSync(data, { level: 9 });
const stored = deflated.length >= data.length;
const body = stored ? data : deflated;
const name = Buffer.from(rel, 'utf8');
const crc = crc32(data);
const local = Buffer.alloc(30);
local.writeUInt32LE(0x04034b50, 0); local.writeUInt16LE(20, 4); local.writeUInt16LE(0x0800, 6);
local.writeUInt16LE(stored ? 0 : 8, 8); local.writeUInt16LE(time, 10); local.writeUInt16LE(date, 12);
local.writeUInt32LE(crc, 14); local.writeUInt32LE(body.length, 18); local.writeUInt32LE(data.length, 22);
local.writeUInt16LE(name.length, 26); local.writeUInt16LE(0, 28);
locals.push(local, name, body);
const entry = Buffer.alloc(46);
entry.writeUInt32LE(0x02014b50, 0); entry.writeUInt16LE(20, 4); entry.writeUInt16LE(20, 6); entry.writeUInt16LE(0x0800, 8);
entry.writeUInt16LE(stored ? 0 : 8, 10); entry.writeUInt16LE(time, 12); entry.writeUInt16LE(date, 14);
entry.writeUInt32LE(crc, 16); entry.writeUInt32LE(body.length, 20); entry.writeUInt32LE(data.length, 24);
entry.writeUInt16LE(name.length, 28); entry.writeUInt32LE(0o100644 << 16 >>> 0, 38); entry.writeUInt32LE(offset, 42);
central.push(entry, name);
offset += 30 + name.length + body.length;
}
const centralBuf = Buffer.concat(central);
const end = Buffer.alloc(22);
end.writeUInt32LE(0x06054b50, 0); end.writeUInt16LE(files.length, 8); end.writeUInt16LE(files.length, 10);
end.writeUInt32LE(centralBuf.length, 12); end.writeUInt32LE(offset, 16);
return Buffer.concat([...locals, centralBuf, end]);
}
const root = path.resolve(process.argv[2] || '.');
const report = check(root);
for (const e of report.errors) console.log(`error ${e.message}`);
for (const w of report.warnings) console.log(`warning ${w.message}`);
if (!report.ok) {
console.log('Not packed: fix the errors above (bin/jumphp chrome:check).');
process.exit(1);
}
const slug = (report.name || path.basename(root)).toLowerCase().normalize('NFD').replace(/[̀-ͯ]/g, '').replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') || 'extension';
const files = shippedFiles(root);
const out = path.join(root, 'dist', `${slug}-${report.version}.zip`);
fs.mkdirSync(path.dirname(out), { recursive: true });
const buf = zip(root, files);
fs.writeFileSync(out, buf);
console.log(`${files.length} file(s): ${files.join(', ')}`);
console.log(`${(buf.length / 1024).toFixed(1)} KB -> dist/${path.basename(out)}`);
console.log('Upload it at https://chrome.google.com/webstore/devconsole (new item, or Package > Upload new package; bump the version first: bin/jumphp chrome:bump).');