<commit_before>/* This file is a part of @mdn/browser-compat-data
 * See LICENSE file for more information. */

import { BrowserName } from '../../types/types.js';
import { InternalSupportStatement } from '../../types/index.js';

import fs from 'node:fs/promises';
import path from 'node:path';

import esMain from 'es-main';
import stringify from 'fast-json-stable-stringify';

import mirrorSupport from './mirror.js';
import compileTS from '../generate-types.js';
import { walk } from '../../utils/index.js';

const packageJson = JSON.parse(
  await fs.readFile(new URL('../../package.json', import.meta.url), 'utf-8'),
);

const directory = './build/';

const verbatimFiles = ['LICENSE', 'README.md'];

export function generateMeta() {
  return { version: packageJson.version };
}

export function applyMirroring(data) {
  const response = Object.assign({}, data);
  const walker = walk(undefined, response);

  for (const feature of walker) {
    if (!feature.compat) {
      continue;
    }
    for (const [browser, supportData] of Object.entries(
      feature.compat.support as InternalSupportStatement,
    )) {
      if (supportData === 'mirror') {
        (feature.data as any).__compat.support[browser] = mirrorSupport(
          browser as BrowserName,
          feature.compat.support,
        );
      }
    }
  }

  return response;
}

// Returns an object containing the prepared BCD data
export async function createDataBundle() {
  const { default: bcd } = await import('../../index.js');

  return {
    ...applyMirroring(bcd),
    __meta: generateMeta(),
  };
}

/* c8 ignore start */

// Returns a promise for writing the data to JSON file
async function writeData() {
  const dest = path.resolve(directory, 'data.json');
  const data = await createDataBundle();
  await fs.writeFile(dest, stringify(data));
}

async function writeWrapper() {
  const dest = path.resolve(directory, 'legacynode.mjs');
  const content = `// A small wrapper to allow ESM imports on older NodeJS versions that don't support import assertions
import fs from 'node:fs';
const bcd = JSON.parse(fs.readFileSync(new URL('./data.json', import.meta.url)));
export default bcd;
`;
  await fs.writeFile(dest, content);
}

async function writeTypeScript() {
  const dest = path.resolve(directory, 'index.ts');
  const content = `/* This file is a part of @mdn/browser-compat-data
 * See LICENSE file for more information. */

import { CompatData } from "./types";

import bcd from "./data.json";

export default bcd as CompatData;
export * from "./types";`;
  await fs.writeFile(dest, content);

  await compileTS(path.resolve(directory, 'types.d.ts'));
}<commit_msg>Default TS export: cast to "any" first (#16707)<commit_after>/* This file is a part of @mdn/browser-compat-data
 * See LICENSE file for more information. */

import { BrowserName } from '../../types/types.js';
import { InternalSupportStatement } from '../../types/index.js';

import fs from 'node:fs/promises';
import path from 'node:path';

import esMain from 'es-main';
import stringify from 'fast-json-stable-stringify';

import mirrorSupport from './mirror.js';
import compileTS from '../generate-types.js';
import { walk } from '../../utils/index.js';

const packageJson = JSON.parse(
  await fs.readFile(new URL('../../package.json', import.meta.url), 'utf-8'),
);

const directory = './build/';

const verbatimFiles = ['LICENSE', 'README.md'];

export function generateMeta() {
  return { version: packageJson.version };
}

export function applyMirroring(data) {
  const response = Object.assign({}, data);
  const walker = walk(undefined, response);

  for (const feature of walker) {
    if (!feature.compat) {
      continue;
    }
    for (const [browser, supportData] of Object.entries(
      feature.compat.support as InternalSupportStatement,
    )) {
      if (supportData === 'mirror') {
        (feature.data as any).__compat.support[browser] = mirrorSupport(
          browser as BrowserName,
          feature.compat.support,
        );
      }
    }
  }

  return response;
}

// Returns an object containing the prepared BCD data
export async function createDataBundle() {
  const { default: bcd } = await import('../../index.js');

  return {
    ...applyMirroring(bcd),
    __meta: generateMeta(),
  };
}

/* c8 ignore start */

// Returns a promise for writing the data to JSON file
async function writeData() {
  const dest = path.resolve(directory, 'data.json');
  const data = await createDataBundle();
  await fs.writeFile(dest, stringify(data));
}

async function writeWrapper() {
  const dest = path.resolve(directory, 'legacynode.mjs');
  const content = `// A small wrapper to allow ESM imports on older NodeJS versions that don't support import assertions
import fs from 'node:fs';
const bcd = JSON.parse(fs.readFileSync(new URL('./data.json', import.meta.url)));
export default bcd;
`;
  await fs.writeFile(dest, content);
}

async function writeTypeScript() {
  const dest = path.resolve(directory, 'index.ts');
  const content = `/* This file is a part of @mdn/browser-compat-data
 * See LICENSE file for more information. */

import { CompatData } from "./types";

import bcd from "./data.json";

// XXX The cast to "any" mitigates a TS definition issue. This is very bad practice and
// should be fixed as soon as possible.
export default bcd as any as CompatData;
export * from "./types";`;
  await fs.writeFile(dest, content);

  await compileTS(path.resolve(directory, 'types.d.ts'));
}
{'fraction_non_alphanumeric': 0.14111212397447584, 'fraction_numerical': 0.0016408386508659982, 'mean_word_length': 3.58312447786132, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7427297', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1620, 'n_words': 520}