winzik/tools/chrome/new.cjs

32 lines
1.4 KiB
JavaScript

// A new Chrome extension from skeleton-chrome/: node new.cjs <skeleton> <target> <name>
// Copies the files, puts the name in the locales, the README and AGENTS.md.
'use strict';
const fs = require('fs');
const path = require('path');
const [skeleton, target, rawName] = process.argv.slice(2);
const name = (rawName || path.basename(target)).trim().slice(0, 75);
const shortName = name.length <= 12 ? name : name.split(/\s+/)[0].slice(0, 12);
const id = path.basename(target).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') || 'extension';
const json = (s) => JSON.stringify(s).slice(1, -1);
const TEXT = /\.(json|md|js|cjs|mjs|html|css|txt)$|^\.gitignore$|^\.packignore$/;
fs.mkdirSync(target, { recursive: true });
const copy = (from, to) => {
for (const entry of fs.readdirSync(from, { withFileTypes: true })) {
const src = path.join(from, entry.name), dst = path.join(to, entry.name);
if (entry.isDirectory()) { fs.mkdirSync(dst, { recursive: true }); copy(src, dst); continue; }
if (TEXT.test(entry.name)) {
const isJson = entry.name.endsWith('.json');
const text = fs.readFileSync(src, 'utf8')
.replaceAll('{{NAME}}', isJson ? json(name) : name)
.replaceAll('{{SHORT_NAME}}', isJson ? json(shortName) : shortName)
.replaceAll('{{ID}}', id);
fs.writeFileSync(dst, text);
} else {
fs.copyFileSync(src, dst);
}
}
};
copy(skeleton, target);
console.log(`${name}: ${target}`);