Skip to content

Commit

Permalink
feat: prepare run() for async use with ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau committed Jul 30, 2022
1 parent d77c4fe commit e904c03
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Create a script, for example `scripts/check-package.js`. Add it in `"scripts"` i

const { createCheckPackage } = require('check-package-dependencies');

createCheckPackage(/* '.' */)
await createCheckPackage(/* '.' */)
// Check that your package.json contains only exact versions of package, not range.
.checkExactVersions({
// When isLibrary is true, it doesnt check "dependencies" as they should mostly have a range, not an exact version
Expand Down Expand Up @@ -110,18 +110,20 @@ createCheckPackage(/* '.' */)
devDependencies: ['react-test-renderer'],
},
},
});
})
.run();
```

```js
'use script';

const { createCheckPackage } = require('check-package-dependencies');

createCheckPackage(/* '.' */)
await createCheckPackage(/* '.' */)
// Call .checkExactVersions(), checkDirectPeerDependencies(), checkDirectDuplicateDependencies()
// checkResolutionsVersionsMatch() and checkResolutionsHasExplanation()
.checkRecommended({});
.checkRecommended({})
.run();
```

If you use workspaces:
Expand All @@ -133,7 +135,7 @@ const {
createCheckPackageWithWorkspaces,
} = require('check-package-dependencies');

createCheckPackageWithWorkspaces()
await createCheckPackageWithWorkspaces()
// Call .checkExactVersions(), checkDirectPeerDependencies(), checkDirectDuplicateDependencies()
// checkResolutionsVersionsMatch() and checkResolutionsHasExplanation() for root package and workspaces packages, but also
// checks your workspaces packages doesn't have different versions than the ones in devDependencies of root packages.
Expand All @@ -147,5 +149,6 @@ createCheckPackageWithWorkspaces()
})
.for('packageName', (pkgCheck) => {
/* pkgCheck has the same API presented for single package */
});
})
.run();
```
22 changes: 22 additions & 0 deletions dist/index-node14.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index-node14.cjs.js.map

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions scripts/check-package.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createCheckPackage } from '../dist/index-node14.cjs.js';

createCheckPackage(undefined, { tryToAutoFix: true }).checkRecommended({
isLibrary: true,
onlyWarnsForInDependencies: {
'@babel/core': { duplicateDirectDependency: ['semver'] },
},
});
await createCheckPackage(undefined, { tryToAutoFix: true })
.checkRecommended({
isLibrary: true,
onlyWarnsForInDependencies: {
'@babel/core': { duplicateDirectDependency: ['semver'] },
},
})
.run();
19 changes: 19 additions & 0 deletions src/check-package-with-workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export interface CheckPackageWithWorkspacesRecommendedOptions {
}

export interface CheckPackageWithWorkspacesApi {
run: () => Promise<void>;

checkRecommended: (
options?: CheckPackageWithWorkspacesRecommendedOptions,
) => CheckPackageWithWorkspacesApi;
Expand Down Expand Up @@ -113,7 +115,24 @@ export function createCheckPackageWithWorkspaces(
}),
);

let runCalled = false;

process.on('beforeExit', () => {
if (!runCalled) {
console.warn('\nFor future compatibility, call .run()');
}
});

return {
async run() {
runCalled = true;
await Promise.all(
[...checksWorkspaces.values()].map((checksWorkspace) =>
checksWorkspace.run(),
),
);
},

checkRecommended({
isLibrary = () => false,
allowRangeVersionsInLibraries = true,
Expand Down
17 changes: 17 additions & 0 deletions src/check-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export interface CheckExactVersionsOptions {
}

export interface CheckPackageApi {
run: () => Promise<void>;

/** @internal */
pkg: PackageJson;
/** @internal */
Expand Down Expand Up @@ -197,7 +199,22 @@ export function createCheckPackage(
pkgDirname,
});

let runCalled = false;

process.on('beforeExit', () => {
if (!runCalled) {
console.warn(
'\nFor future compatibility, call .run() and await the result.',
);
}
});

return {
run() {
runCalled = true;
return Promise.resolve();
},

pkg,
pkgDirname,
pkgPathName,
Expand Down

0 comments on commit e904c03

Please sign in to comment.