Skip to content

Commit

Permalink
feat: verify that the package.json fields point to correct path (#401)
Browse files Browse the repository at this point in the history
### Summary

After initial project setup, it's possible that the location of built
files may diverge from value of package.json fields due to configuration
changes. Previously it'd go unnoticed.

This adds additional checks after the files are built to ensure that
such mismatches cause a build failure.


https://github.com/callstack/react-native-builder-bob/assets/1174278/25d1c427-da3e-4204-b49e-6754c1a316de

### Test plan

- Tested it in React Navigation & React Native Paper repos
  • Loading branch information
satya164 authored Jun 28, 2023
1 parent 47d1370 commit 49ef758
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/react-native-builder-bob/src/targets/commonjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export default async function build({
output,
modules: 'commonjs',
report,
field: 'main',
});
}
1 change: 1 addition & 0 deletions packages/react-native-builder-bob/src/targets/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export default async function build({
output,
modules: false,
report,
field: 'module',
});
}
58 changes: 52 additions & 6 deletions packages/react-native-builder-bob/src/targets/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,62 @@ export default async function build({
report.success(
`Wrote definition files to ${kleur.blue(path.relative(root, output))}`
);

const packageJson = JSON.parse(
await fs.readFile(path.join(root, 'package.json'), 'utf-8')
);

if ('types' in packageJson) {
if (!packageJson.types.endsWith('.d.ts')) {
report.error(
`The ${kleur.blue('types')} field in ${kleur.blue(
'package.json'
)} doesn't point to a definition file. Verify the path points to the correct file under ${kleur.blue(
path.relative(root, output)
)}.`
);

throw new Error("Found incorrect path in 'types' field.");
}

const typesPath = path.join(root, packageJson.types);

if (!(await fs.pathExists(typesPath))) {
report.error(
`The ${kleur.blue('types')} field in ${kleur.blue(
'package.json'
)} points to a non-existent file: ${kleur.blue(
packageJson.types
)}.\nVerify the path points to the correct file under ${kleur.blue(
path.relative(root, output)
)}.`
);

throw new Error("Found incorrect path in 'types' field.");
}
} else {
report.warn(
`No ${kleur.blue('types')} field found in ${kleur.blue(
'package.json'
)}.\nConsider adding it so consumers can use the types.`
);
}
} else {
throw new Error('Failed to build definition files.');
}
} catch (e: any) {
if (e.stdout) {
report.error(
`Errors found when building definition files:\n${e.stdout.toString()}`
);
} catch (e: unknown) {
if (e != null && typeof e === 'object') {
if ('stdout' in e && e.stdout != null) {
report.error(
`Errors found when building definition files:\n${e.stdout.toString()}`
);
} else if ('message' in e && typeof e.message === 'string') {
report.error(e.message);
} else {
throw e;
}
} else {
report.error(e.message);
throw e;
}

throw new Error('Failed to build definition files.');
Expand Down
41 changes: 41 additions & 0 deletions packages/react-native-builder-bob/src/utils/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Options = Input & {
sourceMaps?: boolean;
copyFlow?: boolean;
modules: 'commonjs' | false;
field: 'main' | 'module';
};

export default async function compile({
Expand All @@ -24,6 +25,7 @@ export default async function compile({
copyFlow,
sourceMaps = true,
report,
field,
}: Options) {
const files = glob.sync('**/*', {
cwd: source,
Expand Down Expand Up @@ -120,4 +122,43 @@ export default async function compile({
);

report.success(`Wrote files to ${kleur.blue(path.relative(root, output))}`);

const packageJson = JSON.parse(
await fs.readFile(path.join(root, 'package.json'), 'utf-8')
);

if (field in packageJson) {
try {
require.resolve(path.join(root, packageJson[field]));
} catch (e: unknown) {
if (
e != null &&
typeof e === 'object' &&
'code' in e &&
e.code === 'MODULE_NOT_FOUND'
) {
report.error(
`The ${kleur.blue(field)} field in ${kleur.blue(
'package.json'
)} points to a non-existent file: ${kleur.blue(
packageJson[field]
)}.\nVerify the path points to the correct file under ${kleur.blue(
path.relative(root, output)
)}.`
);

throw new Error(`Found incorrect path in '${field}' field.`);
}

throw e;
}
} else {
report.warn(
`No ${kleur.blue(field)} field found in ${kleur.blue(
'package.json'
)}. Add it to your ${kleur.blue(
'package.json'
)} so that consumers of your package can use it.`
);
}
}
11 changes: 10 additions & 1 deletion packages/react-native-builder-bob/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import kleur from 'kleur';
const logger =
(type: string, color: Function) =>
(...messages: unknown[]) => {
console.log(color(kleur.bold(type)), ...messages);
console.log(
color(kleur.bold(type)),
...messages.map((message) => {
if (typeof message === 'string') {
return message.split('\n').join(`\n `);
} else {
return message;
}
})
);
};

export const info = logger('ℹ', kleur.blue);
Expand Down

0 comments on commit 49ef758

Please sign in to comment.