Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JkYTqsvYu6AYr9yQ3Q4Nt6
57 lines
2.5 KiB
JavaScript
57 lines
2.5 KiB
JavaScript
// Shared by the Chrome extension tools (tools/chrome.sh): the files of an extension, what goes in the package.
|
||
'use strict';
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
// development files: never in the Chrome Web Store package (and not checked as extension code)
|
||
const DEV_DIRS = new Set(['.git', '.svn', 'node_modules', 'tests', 'test', 'var', 'dist', 'scripts', '.jumphp', 'IA', '.idea', '.vscode', '.github', 'store', 'coverage']);
|
||
const DEV_FILES = /^(\.gitignore|\.gitattributes|\.editorconfig|\.eslintrc.*|eslint\.config\.[cm]?js|package(-lock)?\.json|\.packignore|\.DS_Store|Thumbs\.db|.*\.md|.*\.map|.*\.log|.*\.zip|.*\.crx|.*\.pem)$/i;
|
||
|
||
/** The patterns of .packignore (one per line: a folder/ , a path, or *.ext), # for comments. */
|
||
function packIgnore(root) {
|
||
const file = path.join(root, '.packignore');
|
||
if (!fs.existsSync(file)) return [];
|
||
return fs.readFileSync(file, 'utf8').split('\n').map((l) => l.trim()).filter((l) => l && !l.startsWith('#'));
|
||
}
|
||
|
||
function ignoredBy(patterns, rel) {
|
||
return patterns.some((p) => {
|
||
if (p.startsWith('*.')) return rel.endsWith(p.slice(1));
|
||
const clean = p.replace(/^\/+/, '');
|
||
return clean.endsWith('/') ? rel.startsWith(clean) : rel === clean || rel.startsWith(clean + '/');
|
||
});
|
||
}
|
||
|
||
/** Every file of the extension that is shipped (relative paths, sorted). */
|
||
function shippedFiles(root) {
|
||
const patterns = packIgnore(root);
|
||
const out = [];
|
||
const walk = (dir, rel) => {
|
||
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const r = rel ? `${rel}/${entry.name}` : entry.name;
|
||
if (entry.isDirectory()) {
|
||
if ((!rel && DEV_DIRS.has(entry.name)) || entry.name === '.git' || entry.name === 'node_modules' || ignoredBy(patterns, r + '/')) continue;
|
||
walk(path.join(dir, entry.name), r);
|
||
} else if (entry.isFile() && !DEV_FILES.test(entry.name) && !ignoredBy(patterns, r)) {
|
||
out.push(r);
|
||
}
|
||
}
|
||
};
|
||
walk(root, '');
|
||
return out.sort();
|
||
}
|
||
|
||
function readJson(file) {
|
||
return JSON.parse(fs.readFileSync(file, 'utf8').replace(/^/, ''));
|
||
}
|
||
|
||
/** __MSG_key__ resolved with a locale's messages (the default locale), else the text as it is. */
|
||
function resolveMessage(value, messages) {
|
||
if (typeof value !== 'string') return value;
|
||
return value.replace(/__MSG_(\w+)__/g, (all, key) => {
|
||
const found = Object.entries(messages || {}).find(([k]) => k.toLowerCase() === key.toLowerCase());
|
||
return found ? String(found[1].message ?? '') : all;
|
||
});
|
||
}
|
||
|
||
module.exports = { shippedFiles, readJson, resolveMessage };
|