Skip to content

Commit

Permalink
Allow overriding node version in package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
maorleger committed Jan 6, 2021
1 parent c23bbe5 commit 28984d1
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 21 deletions.
11 changes: 9 additions & 2 deletions common/config/rush/pnpm-lock.yaml

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

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Requires support for all Node LTS version.

Currently, this requires `engine` in `package.json` to be set to `">=8.0.0"`.
Currently, this requires `engines` in `package.json` to contain an entry for `node` set to `">=8.0.0"` unless a `nodeVersionOverride` value is present.

This rule is fixable using the `--fix` option.

Expand All @@ -12,23 +12,33 @@ This rule is fixable using the `--fix` option.

```json
{
"engine": ">=8.0.0"
"engines": {
"node": ">=8.0.0"
}
}
```

### Bad

````json
```json
{
"engine": ">=6.0.0"
"engine": {
"node": ">=8.0.0"
}
}
```'
```

```json
{
"engine": ">=10.0.0"
"engine": ">=6.0.0"
}
````
```

```json
{
"engine": ">=10.0.0"
}
```

```json
{}
Expand All @@ -38,6 +48,24 @@ This rule is fixable using the `--fix` option.

Only if the rule breaks.

## Options

This rule as an object option:

- `"nodeVersionOverride"`: allow providing a custom supported node version if an external dependency enforces it

### nodeVersionOverride

Example of **correct** code for this rule with the `{ "nodeVersionOverride": ">=10.0.0" }` option:

```json
{
"engines": {
"node": ">=10.0.0"
}
}
```

## [Source](https://azure.github.io/azure-sdk/typescript_implementation.html#ts-package-json-engine-is-present)

Also encompasses [ts-node-support](https://azure.github.io/azure-sdk/typescript_design.html#ts-node-support), as the rules are similar enough to not exist separately for linting purposes.
2 changes: 2 additions & 0 deletions common/tools/eslint-plugin-azure-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@
"@types/estree": "^0.0.45",
"eslint-config-prettier": "^7.0.0",
"glob": "^7.1.2",
"json-schema": "^0.3.0",
"typescript": "4.1.2",
"tslib": "^2.0.0"
},
"devDependencies": {
"@types/chai": "^4.1.6",
"@types/glob": "^7.1.1",
"@types/json-schema": "^7.0.6",
"@types/mocha": "^7.0.2",
"@types/node": "^8.0.0",
"@typescript-eslint/eslint-plugin": "^4.9.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
import { Rule } from "eslint";
import { getRuleMetaData, getVerifiers, stripPath } from "../utils";

/**
* definition of LTS Node versions
* * needs updating as definitions change
*/
const LTS = ">=8.0.0";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -17,19 +23,27 @@ export = {
meta: getRuleMetaData(
"ts-package-json-engine-is-present",
"force Node support for all LTS versions",
"code"
"code",
[
{
type: "object",
properties: {
nodeVersionOverride: {
type: "string",
default: LTS,
description: "Allows specifying a different node version than the current default"
}
}
}
]
),
create: (context: Rule.RuleContext): Rule.RuleListener => {
/**
* definition of LTS Node versions
* * needs updating as definitions change
*/
const LTS = ">=8.0.0";
const options = context.options[0] || {};

const verifiers = getVerifiers(context, {
outer: "engines",
inner: "node",
expected: LTS
expected: options.nodeVersionOverride || LTS
});
return stripPath(context.getFilename()) === "package.json"
? ({
Expand Down
6 changes: 4 additions & 2 deletions common/tools/eslint-plugin-azure-sdk/src/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
*/

import { Rule } from "eslint";
import { JSONSchema4 } from "json-schema";

export const getRuleMetaData = (
ruleName: string,
ruleDescription: string,
fix?: "code" | "whitespace"
fix?: "code" | "whitespace",
schema?: JSONSchema4 | JSONSchema4[]
): Rule.RuleMetaData => {
const required = {
type: "suggestion",
Expand All @@ -21,7 +23,7 @@ export const getRuleMetaData = (
recommended: true,
url: `https://github.com/Azure/azure-sdk-for-js/tree/master/common/tools/eslint-plugin-azure-sdk/docs/rules/${ruleName}.md`
},
schema: []
schema: schema || []
};
return (fix !== undefined ? { ...required, fixable: fix } : required) as Rule.RuleMetaData;
};
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ ruleTester.run("ts-package-json-engine-is-present", rule, {
// incorrect format but in a file we don't care about
code: '{"engines": { "node": ">=6.0.0" }}',
filename: "not_package.json"
},
{
// different than the default but with an override
code: '{"engines": { "node": ">=8.5.0" }}',
filename: "package.json",
options: [
{
nodeVersionOverride: ">=8.5.0"
}
]
}
],
invalid: [
Expand Down Expand Up @@ -322,6 +332,22 @@ ruleTester.run("ts-package-json-engine-is-present", rule, {
}
],
output: examplePackageGood
},
{
// override was provided but the version does not match
code: '{"engines": { "node": ">=8.0.0" }}',
filename: "package.json",
errors: [
{
message: "engines.node is set to >=8.0.0 when it should be set to >=6.5.0"
}
],
options: [
{
nodeVersionOverride: ">=6.5.0"
}
],
output: '{"engines": { "node": ">=6.5.0" }}'
}
]
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
],
"rules": {
"no-underscore-dangle": ["error", { "allowAfterThis": true }],
"node/no-unsupported-features/es-syntax": ["error", { "ignores": ["modules"] }]
"node/no-unsupported-features/es-syntax": ["error", { "ignores": ["modules"] }],
// OpenTelemetry requires a minimum node version of 8.5.0
// https://github.com/open-telemetry/opentelemetry-js#node-support
"@azure/azure-sdk/ts-package-json-engine-is-present": [
"error",
{ "nodeVersionOverride": ">=8.5.0" }
]
}
}
2 changes: 1 addition & 1 deletion sdk/monitor/opentelemetry-exporter-azure-monitor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This exporter package assumes your application is [already instrumented](https:/

You must have an [Azure subscription](https://azure.microsoft.com/free/) and a
[Application Insights workspace](https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview/) to use this package.
If you are using this package in a Node.js application, then use Node.js 8.x or higher.
If you are using this package in a Node.js application, then use Node.js 8.5.0 or higher.

### Distributed Tracing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"docs": "typedoc --excludePrivate --excludeNotExported --excludeExternals --mode file --out ./dist/docs ./src"
},
"engines": {
"node": ">=8.0.0"
"node": ">=8.5.0"
},
"files": [
"dist-esm/src/",
Expand Down

0 comments on commit 28984d1

Please sign in to comment.