Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { fileURLToPath } from 'node:url';

import betterAjvErrors from 'better-ajv-errors';
import esMain from 'es-main';
import stringify from 'fast-json-stable-stringify';
import { compareVersions } from 'compare-versions';
import { marked } from 'marked';

Expand All @@ -20,6 +19,7 @@ import { createAjv } from '../lib/ajv.js';
import { walk } from '../../utils/index.js';
import { WalkOutput } from '../../utils/walk.js';
import bcd from '../../index.js';
import stringifyAndOrderProperties from '../lib/stringify-and-order-properties.js';

import mirrorSupport from './mirror.js';

Expand Down Expand Up @@ -261,7 +261,7 @@ const writeData = async () => {
const dest = new URL('data.json', targetdir);
const data = await createDataBundle();
validate(data);
await fs.writeFile(dest, stringify(data));
await fs.writeFile(dest, stringifyAndOrderProperties(data, null));
logWrite(dest, 'data');
};

Expand Down
58 changes: 32 additions & 26 deletions scripts/lib/stringify-and-order-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const doOrder = <T>(value: T, order: string[]): T => {
*/
export const stringifyReleases = (
releases: Record<string, ReleaseStatement>,
indent: any,
): string => {
const indentStep = ' '; // Constant with the indent step that sortStringify will use

Expand All @@ -90,7 +91,7 @@ export const stringifyReleases = (
let result = '';
for (let i = 0; i < sortedKeys.length; i++) {
const k = sortedKeys[i];
const v = JSON.stringify(releases[k], null, 2).replace(/\n/g, '\n ');
const v = JSON.stringify(releases[k], null, indent).replace(/\n/g, '\n ');

// Add it to the result
result += `${indentStep}"${k}": ${v}`;
Expand All @@ -100,10 +101,14 @@ export const stringifyReleases = (
result += ',';
}

// We always need a carriage return
result += '\n';
// We always need a carriage return unless we're squashing the format
if (indent) {
result += '\n';
}
}
return `*#*#{\n${result}}#*#*`; // Close the brace and return the string
return `*#*#{${indent ? '\n' : ''}${result}}#*#*`
.replace(/"/g, '*"*')
.replace(/\\n/g, '*\\n*'); // Close the brace and return the string
};

/**
Expand All @@ -115,7 +120,7 @@ export const stringifyReleases = (
* @param value The value of the key
* @returns The new value
*/
export const orderProperties = (key: string, value: any): any => {
export const orderProperties = (key: string, value: any, indent: any): any => {
if (value instanceof Object) {
// Order properties for data
if ('__compat' in value) {
Expand Down Expand Up @@ -154,23 +159,24 @@ export const orderProperties = (key: string, value: any): any => {

// Order properties for browsers
if ('browsers' in value) {
const browser = Object.keys(value.browsers)[0];
for (const browser in value.browsers) {
value.browsers[browser] = doOrder(
value.browsers[browser] as BrowserStatement,
propOrder.browsers.browser,
);

value.browsers[browser] = doOrder(
value.browsers[browser] as BrowserStatement,
propOrder.browsers.browser,
);
for (const r of Object.keys(value.browsers[browser].releases)) {
value.browsers[browser].releases[r] = doOrder(
value.browsers[browser].releases[r] as ReleaseStatement,
propOrder.browsers.release,
);
}

for (const r of Object.keys(value.browsers[browser].releases)) {
value.browsers[browser].releases[r] = doOrder(
value.browsers[browser].releases[r] as ReleaseStatement,
propOrder.browsers.release,
value.browsers[browser].releases = stringifyReleases(
value.browsers[browser].releases,
indent,
);
}

value.browsers[browser].releases = stringifyReleases(
value.browsers[browser].releases,
);
}
}
return value;
Expand All @@ -181,22 +187,22 @@ export const orderProperties = (key: string, value: any): any => {
* @param rawdata The object to stringify
* @returns The stringified object
*/
const stringifyAndOrderProperties = (rawdata: any): string => {
const stringifyAndOrderProperties = (rawdata: any, indent: any = 2): string => {
if (rawdata instanceof Object) {
rawdata = JSON.stringify(rawdata);
}
const data = JSON.parse(rawdata, orderProperties);
const data = JSON.parse(rawdata, (k, v) => orderProperties(k, v, indent));

if ('browsers' in data) {
// Browser data needs to be stringified in a special way due to the release data
return JSON.stringify(data, null, 2)
.replace('"*#*#', '')
.replace('#*#*"', '')
.replace(/\\n/g, '\n ')
.replace(/\\"/g, '"');
return JSON.stringify(data, null, indent)
.replace(/"\*#\*#/g, '')
.replace(/#\*#\*"/g, '')
.replace(/\*\\n\*/g, '\n')
.replace(/\*\\"\*/g, '"');
}

return JSON.stringify(data, null, 2);
return JSON.stringify(data, null, indent);
};

export default stringifyAndOrderProperties;
Loading