Skip to content

Commit

Permalink
check node engine is provided in package.json (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l authored Apr 24, 2023
1 parent 0a5ff75 commit 20117cd
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 183 deletions.
14 changes: 10 additions & 4 deletions .changeset/bob-the-bundler-247-dependencies.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
---
"bob-the-bundler": patch
'bob-the-bundler': patch
---

dependencies updates:
- Removed dependency [`@vercel/ncc@^0.36.0` ↗︎](https://www.npmjs.com/package/@vercel/ncc/v/0.36.0) (from `dependencies`)
- Removed dependency [`dependency-graph@^0.11.0` ↗︎](https://www.npmjs.com/package/dependency-graph/v/0.11.0) (from `dependencies`)
- Removed dependency [`tsup@^6.5.0` ↗︎](https://www.npmjs.com/package/tsup/v/6.5.0) (from `dependencies`)

- Removed dependency [`@vercel/ncc@^0.36.0` ↗︎](https://www.npmjs.com/package/@vercel/ncc/v/0.36.0)
(from `dependencies`)
- Removed dependency
[`dependency-graph@^0.11.0` ↗︎](https://www.npmjs.com/package/dependency-graph/v/0.11.0) (from
`dependencies`)
- Removed dependency [`tsup@^6.5.0` ↗︎](https://www.npmjs.com/package/tsup/v/6.5.0) (from
`dependencies`)
5 changes: 5 additions & 0 deletions .changeset/mean-kings-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'bob-the-bundler': major
---

Require `engines.node` entry with `bob check` command.
17 changes: 17 additions & 0 deletions src/commands/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const ExportsMapModel = zod.record(
]),
);

const EnginesModel = zod.record(zod.string(), zod.string());

const BinModel = zod.record(zod.string());

export const checkCommand = createCommand<{}, {}>(api => {
Expand Down Expand Up @@ -93,6 +95,9 @@ export const checkCommand = createCommand<{}, {}>(api => {
skipExports: new Set<string>(config?.check?.skip ?? []),
includesCommonJS: config?.commonjs ?? true,
});
await checkEngines({
packageJSON: distPackageJSON,
});
} catch (err) {
api.reporter.error(`Integrity check of '${packageJSON.name}' failed.`);
api.reporter.log(err);
Expand Down Expand Up @@ -316,6 +321,18 @@ async function checkExportsMapIntegrity(args: {
}
}

async function checkEngines(args: {
packageJSON: {
name: string;
engines: unknown;
};
}) {
const engines = EnginesModel.safeParse(args.packageJSON.engines);
if (engines.success === false || engines.data['node'] === undefined) {
throw new Error('Please specify the node engine version in your package.json.');
}
}

const timeout = `;setTimeout(() => { throw new Error("The Node.js process hangs. There is probably some side-effects. All exports should be free of side effects.") }, 500).unref()`;

function runRequireJSFileCommand(args: { cwd: string; path: string }): ExecaChildProcess {
Expand Down
68 changes: 45 additions & 23 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
import zod from 'zod';

const BobConfigModel = zod.optional(
zod.union([
zod.literal(false),
zod.object({
commonjs: zod.optional(zod.literal(false), {
description: 'Omit CommonJS output.',
}),
build: zod.union([
zod.literal(false),
zod.optional(
zod.object({
copy: zod.optional(zod.array(zod.string())),
}),
zod.union(
[
zod.literal(false),
zod.object({
commonjs: zod.optional(zod.literal(false), {
description: 'Omit CommonJS output.',
}),
build: zod.union(
[
zod.literal(false),
zod.optional(
zod.object({
copy: zod.optional(zod.array(zod.string()), {
description:
'Specify a list of files that should be copied the the output directory.',
}),
}),
),
],
{
description:
'Build configuration. Set to false for skipping the build of this package.',
},
),
check: zod.optional(
zod.union([
zod.literal(false),
zod.object({
skip: zod.optional(zod.array(zod.string()), {
description:
'Skip certain files from being checked. E.g. modules with side-effects.',
}),
}),
]),
{
description:
'Check whether the built packages comply with the standards. (ESM & CJS compatible and loadable and Node.js engines specified)',
},
),
]),
check: zod.optional(
zod.union([
zod.literal(false),
zod.object({
skip: zod.optional(zod.array(zod.string())),
}),
]),
),
}),
]),
}),
],
{
description:
'Bob configuration. Set this value to false in order to disable running bob on this package.',
},
),
);

export type BobConfig = zod.TypeOf<typeof BobConfigModel>;
Expand Down
3 changes: 3 additions & 0 deletions test/__fixtures__/simple-esm-only/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "simple-esm-only",
"type": "module",
"engines": {
"node": ">= 14.0.0"
},
"main": "dist/esm/index.js",
"module": "dist/esm/index.js",
"exports": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "a",
"type": "module",
"engines": {
"node": ">= 14.0.0"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "b",
"type": "module",
"engines": {
"node": ">= 14.0.0"
},
"bin": {
"bbb": "dist/cjs/log-the-world.js"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "c",
"engines": {
"node": ">= 14.0.0"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
Expand Down
3 changes: 3 additions & 0 deletions test/__fixtures__/simple-monorepo/packages/a/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "a",
"type": "module",
"engines": {
"node": ">= 14.0.0"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
Expand Down
3 changes: 3 additions & 0 deletions test/__fixtures__/simple-monorepo/packages/b/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "b",
"type": "module",
"engines": {
"node": ">= 14.0.0"
},
"bin": {
"bbb": "dist/cjs/log-the-world.js"
},
Expand Down
3 changes: 3 additions & 0 deletions test/__fixtures__/simple-monorepo/packages/c/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "c",
"engines": {
"node": ">= 14.0.0"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
Expand Down
3 changes: 3 additions & 0 deletions test/__fixtures__/simple-types-only/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "simple-types-only",
"type": "module",
"engines": {
"node": ">= 14.0.0"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
Expand Down
3 changes: 3 additions & 0 deletions test/__fixtures__/simple/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "simple",
"type": "module",
"engines": {
"node": ">= 12.0.0"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
Expand Down
3 changes: 3 additions & 0 deletions test/__fixtures__/tsconfig-build-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "tsconfig-build-json",
"type": "module",
"engines": {
"node": ">= 14.0.0"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
Expand Down
Loading

0 comments on commit 20117cd

Please sign in to comment.