Skip to content

Commit

Permalink
task: bring back rules file (#289)
Browse files Browse the repository at this point in the history
* bring back config/rules file generation

- adds `postinstall` script for `create-config.ts`
- adjust build process to account for new script
- adjusts how `logger.ts` grabs the custom rules

* Create small-forks-run.md

* Update run-tests.yml

* ci?

* Update postinstall.js

* Update postinstall.js
  • Loading branch information
zibs authored Dec 13, 2024
1 parent b833240 commit 4fb5051
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/small-forks-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@react-native-ama/internal': patch
'@react-native-ama/core': patch
---

fix config/rules file generation
16 changes: 9 additions & 7 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
"version": "1.1.1",
"description": "Accessible Mobile App Library for React Native",
"react-native": "src/index",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"types": "dist/src/index.d.ts",
"main": "dist/src/index.js",
"exports": {
".": [
{
"imports": "./dist/index.js",
"types": "./dist/index.d.ts"
"imports": "./dist/src/index.js",
"types": "./dist/src/index.d.ts"
},
"./dist/index.js"
"./dist/src/index.js"
]
},
"files": [
"src",
"dist"
"dist",
"dist/scripts"
],
"scripts": {
"build": "rm -rf dist && tsc -p ./tsconfig.build.json",
"typecheck": "tsc --noEmit",
"test": "jest"
"test": "jest",
"postinstall": "node ./src/postinstall.js"
},
"dependencies": {
"@react-native-ama/internal": "~1.1.1"
Expand Down
20 changes: 20 additions & 0 deletions packages/core/scripts/create-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fs from 'fs';
import path from 'path';

const projectRoot = path.resolve(process.cwd(), '..', '..', '..');

const configFileName = 'ama.rules.json';
const configFilePath = path.join(projectRoot, configFileName);
const defaultConfig = {
rules: {},
accessibilityLabelExceptions: [],
};

if (!fs.existsSync(configFilePath)) {
try {
fs.writeFileSync(configFilePath, JSON.stringify(defaultConfig, null, 2));
console.log(`[AMA]: Rules file created at ${configFilePath}`);
} catch (error) {
console.error('[AMA]: Error creating rules file:', error);
}
}
18 changes: 18 additions & 0 deletions packages/core/src/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const path = require('path');

// INIT_CWD points to where `npm install` or `yarn install` was invoked
const initCwd = process.env.INIT_CWD || '';
const packageRoot = __dirname;

// Detect if this is running as part of the monorepo installation
const isMonorepo = initCwd.includes(path.join(packageRoot, '..', '..', '..'));

// If running in the monorepo context, skip the postinstall script
if (isMonorepo) {
console.log('[AMA] Skipping postinstall: Running within monorepo');
process.exit(0);
}

// If installed as a standalone dependency, run the postinstall logic
console.log('[AMA] Running postinstall: User installation');
require('./../dist/scripts/create-config.js');
2 changes: 1 addition & 1 deletion packages/core/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"include": ["src/**/*.ts"],
"include": ["src/**/*.ts", "scripts/**.ts"],
"exclude": ["src/**/*.test.ts"],
"compilerOptions": {
"target": "ES2016",
Expand Down
20 changes: 15 additions & 5 deletions packages/internal/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ import {
RULES_HELP,
} from './logger.rules';

const overrideRules: OverrideRule = require('./../ama.rules.json');
const defaultRules: OverrideRule = require('./../ama.rules.json');
let projectRules = defaultRules;
try {
// look upwards to user's project root
// e.g we are here:
// root/node_modules/@react-native-ama/internal/dist/utils/logger.ts
const userDefinedRules: OverrideRule = require('./../../../../../ama.rules.json');
projectRules = Object.assign(projectRules, userDefinedRules);
} catch (error) {
// noop
}

type OverrideRule = {
rules: Record<
Expand All @@ -32,7 +42,7 @@ export type LogParams = {
export const getRuleAction = __DEV__
? (rule: Rule): RuleAction => {
const customRule = canRuleBeOverridden?.(rule)
? overrideRules?.rules?.[rule]
? projectRules?.rules?.[rule]
: undefined;

return customRule || LOGGER_RULES![rule];
Expand Down Expand Up @@ -70,7 +80,7 @@ export const logFailure = __DEV__
export const getContrastCheckerMaxDepth = __DEV__
? () => {
return (
overrideRules?.rules?.CONTRAST_CHECKER_MAX_DEPTH ||
projectRules?.rules?.CONTRAST_CHECKER_MAX_DEPTH ||
CONTRAST_CHECKER_MAX_DEPTH
);
}
Expand All @@ -79,15 +89,15 @@ export const getContrastCheckerMaxDepth = __DEV__
export const shouldIgnoreContrastCheckForDisabledElement = __DEV__
? () => {
return (
overrideRules?.rules?.IGNORE_CONTRAST_FOR_DISABLED_ELEMENTS ||
projectRules?.rules?.IGNORE_CONTRAST_FOR_DISABLED_ELEMENTS ||
IGNORE_CONTRAST_FOR_DISABLED_ELEMENTS
);
}
: null;

export const isAccessibilityLabelAllowed = __DEV__
? (accessibilityLabel: string) => {
return overrideRules?.accessibilityLabelExceptions?.includes(
return projectRules?.accessibilityLabelExceptions?.includes(
accessibilityLabel,
);
}
Expand Down

0 comments on commit 4fb5051

Please sign in to comment.