Skip to content

Commit

Permalink
Fixup and optimize code (mdn#21554)
Browse files Browse the repository at this point in the history
  • Loading branch information
queengooborg authored Dec 9, 2023
1 parent 4cecf6c commit 64053aa
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 191 deletions.
2 changes: 1 addition & 1 deletion scripts/fix/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { processData } from '../../test/linter/test-links.js';
* @param {string} filename The name of the file to fix
*/
const fixLinks = (filename: string): void => {
const errors = processData(filename);
const original = fs.readFileSync(filename, 'utf-8').trim();
const errors = processData(original);
let data = original;

if (IS_WINDOWS) {
Expand Down
17 changes: 9 additions & 8 deletions scripts/release/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@ export const applyMirroring = (feature: WalkOutput): void => {
export const createDataBundle = async (): Promise<CompatData> => {
const { default: bcd } = await import('../../index.js');

const data = Object.assign({}, bcd);
const walker = walk(undefined, data);
const walker = walk(undefined, bcd);

for (const feature of walker) {
applyMirroring(feature);
}

return {
...data,
...bcd,
__meta: generateMeta(),
};
};
Expand Down Expand Up @@ -209,11 +208,13 @@ const main = async () => {
// Crate a new directory
await fs.mkdir(targetdir);

await writeManifest();
await writeData();
await writeWrapper();
await writeTypeScript();
await copyFiles();
await Promise.all([
writeManifest(),
writeData(),
writeWrapper(),
writeTypeScript(),
copyFiles(),
]);

console.log('Data bundle is ready');
};
Expand Down
33 changes: 15 additions & 18 deletions scripts/release/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ const commitAndPR = async (
thisVersion: string,
wait: boolean,
): Promise<void> => {
exec('git switch main');
exec('git switch -c release');

if (wait) {
console.log('');
console.log(
Expand All @@ -39,14 +36,16 @@ const commitAndPR = async (
console.log('');
}

exec('git add package.json package-lock.json RELEASE_NOTES.md');
exec(
`git commit -m "Release ${thisVersion}" -m "" -m "This release was generated by the project's release script."`,
);
exec('git push --set-upstream origin release');
exec('gh pr create --fill');
exec('git switch main');
exec('git branch -d release');
exec(`
git switch main
git switch -c release
git add package.json package-lock.json RELEASE_NOTES.md
git commit -m "Release ${thisVersion}" -m "" -m "This release was generated by the project's release script."
git push --set-upstream origin release
gh pr create --fill
git switch main
git branch -d release
`);
};

/**
Expand All @@ -64,14 +63,12 @@ const main = async () => {
console.log(
chalk`{blue Checking merged PRs to determine semver bump level...}`,
);
let versionBump: 'major' | 'minor' | 'patch' = 'patch';
const semverBumpPulls = getSemverBumpPulls(lastVersionDate);

if (semverBumpPulls.major.length) {
versionBump = 'major';
} else if (semverBumpPulls.minor.length) {
versionBump = 'minor';
}
const versionBump: 'major' | 'minor' | 'patch' = semverBumpPulls.major.length
? 'major'
: semverBumpPulls.minor.length
? 'minor'
: 'patch';

// Perform version bump
exec(`npm version --no-git-tag-version ${versionBump}`);
Expand Down
7 changes: 5 additions & 2 deletions scripts/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import esMain from 'es-main';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

import bcd from '../index.js';
import bcdData from '../index.js';
import {
BrowserName,
CompatStatement,
Expand Down Expand Up @@ -125,6 +125,7 @@ const iterateData = (data, browsers: BrowserName[], stats: VersionStats) => {
const getStats = (
folder: string,
allBrowsers: boolean,
bcd = bcdData,
): VersionStats | null => {
/** @constant {string[]} */
const browsers: BrowserName[] = allBrowsers
Expand Down Expand Up @@ -154,7 +155,9 @@ const getStats = (
} else if (bcd[folder]) {
iterateData(bcd[folder], browsers, stats);
} else {
console.error(chalk`{red.bold Folder "${folder}/" doesn't exist!}`);
if (process.env.NODE_ENV !== 'test') {
console.error(chalk`{red.bold Folder "${folder}/" doesn't exist!}`);
}
return null;
}
} else {
Expand Down
43 changes: 30 additions & 13 deletions test/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,38 @@ import extend from '../scripts/lib/extend.js';
import pluralize from '../scripts/lib/pluralize.js';
import { walk } from '../utils/index.js';

import linters from './linter/index.js';
import { LinterMessage, LinterMessageLevel, LinterPath } from './utils.js';
import * as linterModules from './linter/index.js';
import {
Linters,
LinterMessage,
LinterMessageLevel,
LinterPath,
} from './utils.js';

const dirname = fileURLToPath(new URL('.', import.meta.url));

const linters = new Linters(Object.values(linterModules));
/**
* Normalize and categorize file path
* @param {string} file The file path
* @returns {LinterPath} The normalized and categorized file path
*/
const normalizeAndCategorizeFilePath = (file: string): LinterPath => {
const filePath: LinterPath = {
full: path.relative(process.cwd(), file),
category: '',
};
if (path.sep === '\\') {
// Normalize file paths for Windows users
filePath.full = filePath.full.replace(/\\/g, '/');
}
if (filePath.full.includes('/')) {
filePath.category = filePath.full.split('/')[0];
}

return filePath;
};

/**
* Recursively load
* @param {string[]} files The files to test
Expand All @@ -45,17 +72,7 @@ const loadAndCheckFiles = async (...files: string[]): Promise<DataType> => {
}

if (fsStats.isFile() && path.extname(file) === '.json') {
const filePath: LinterPath = {
full: path.relative(process.cwd(), file),
category: '',
};
if (path.sep === '\\') {
// Normalize file paths for Windows users
filePath.full = filePath.full.replace(/\\/g, '/');
}
if (filePath.full.includes('/')) {
filePath.category = filePath.full.split('/')[0];
}
const filePath = normalizeAndCategorizeFilePath(file);

try {
const rawFileData = (await fs.readFile(file, 'utf-8')).trim();
Expand Down
56 changes: 17 additions & 39 deletions test/linter/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,20 @@
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */

import { Linters } from '../utils.js';

import testBrowsersData from './test-browsers-data.js';
import testBrowsersPresence from './test-browsers-presence.js';
import testConsistency from './test-consistency.js';
import testDescriptions from './test-descriptions.js';
import testFilename from './test-filename.js';
import testLinks from './test-links.js';
import testMirror from './test-mirror.js';
import testMultipleStatements from './test-multiple-statements.js';
import testNotes from './test-notes.js';
import testObsolete from './test-obsolete.js';
import testPrefix from './test-prefix.js';
import testSchema from './test-schema.js';
import testSpecURLs from './test-spec-urls.js';
import testStatus from './test-status.js';
import testStyle from './test-style.js';
import testTags from './test-tags.js';
import testVersions from './test-versions.js';

export default new Linters([
testBrowsersData,
testBrowsersPresence,
testConsistency,
testDescriptions,
testFilename,
testLinks,
testMirror,
testMultipleStatements,
testNotes,
testObsolete,
testPrefix,
testSchema,
testSpecURLs,
testStatus,
testStyle,
testTags,
testVersions,
]);
export { default as testBrowsersData } from './test-browsers-data.js';
export { default as testBrowsersPresence } from './test-browsers-presence.js';
export { default as testConsistency } from './test-consistency.js';
export { default as testDescriptions } from './test-descriptions.js';
export { default as testFilename } from './test-filename.js';
export { default as testLinks } from './test-links.js';
export { default as testMirror } from './test-mirror.js';
export { default as testMultipleStatements } from './test-multiple-statements.js';
export { default as testNotes } from './test-notes.js';
export { default as testObsolete } from './test-obsolete.js';
export { default as testPrefix } from './test-prefix.js';
export { default as testSchema } from './test-schema.js';
export { default as testSpecURLs } from './test-spec-urls.js';
export { default as testStatus } from './test-status.js';
export { default as testStyle } from './test-style.js';
export { default as testTags } from './test-tags.js';
export { default as testVersions } from './test-versions.js';
Loading

0 comments on commit 64053aa

Please sign in to comment.