@dataunlocker/defender
Version:
DataUnlocker core module that protects your web app's analytics.
26 lines (25 loc) • 1.12 kB
JavaScript
import { mkdir, readFile, writeFile } from 'fs/promises';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
import { fetchModule, info, log } from './utils/index.js';
const startedAt = Date.now();
const homeDir = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const isInstalledAsModule = /[\\|/]node_modules[\\|/]/.test(homeDir);
if (!isInstalledAsModule) {
info(`DataUnlocker data won't be fetched on this install (the package is not in node_modules).`);
process.exit(0);
}
const pkg = JSON.parse((await readFile(resolve(dirname(fileURLToPath(import.meta.url)), '../package.json'))).toString());
console.info(`💜 DataUnlocker v${pkg.version}\n`);
info("Fetching DataUnlocker's module...");
const { files, endpoint } = await fetchModule();
info(`Selected endpoint: ${endpoint}`);
for (let { name, content } of files) {
const file = resolve(homeDir, name);
log(`Writing file ${file}...`);
const dir = dirname(file);
await mkdir(dir, { recursive: true });
await writeFile(file, content);
}
log(`Done in ${Date.now() - startedAt}ms!`);