Skip to content

Commit

Permalink
Ts part 14 (#576)
Browse files Browse the repository at this point in the history
* Update esbuild.config.js

* Add @types/node

* Reporter

* Update tsconfig.json

* Tests

* Define more types

* Add build to github workflows

* Update tsconfig.json
  • Loading branch information
tclindner authored Feb 27, 2022
1 parent a8509f0 commit 78853eb
Show file tree
Hide file tree
Showing 45 changed files with 687 additions and 430 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ jobs:
with:
node-version: ${{ matrix.node }}
- run: npm ci --no-progress
- run: npm run build
- run: npm run test:ci
1 change: 1 addition & 0 deletions .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
node-version: '14'
registry-url: 'https://registry.npmjs.org'
- run: npm ci --no-progress --production
- run: npm run build
- run: npm version --no-push --no-git-tag-version --yes ${{ github.event.release.tag_name }}
- run: npm publish --tag next
env:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
node-version: '14'
registry-url: 'https://registry.npmjs.org'
- run: npm ci --no-progress --production
- run: npm run build
- run: npm version --no-push --no-git-tag-version --yes ${{ github.event.release.tag_name }}
- run: npm publish --tag latest
env:
Expand Down
60 changes: 45 additions & 15 deletions esbuild.config.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-extraneous-dependencies
const esbuild = require('esbuild');
/* eslint-disable @typescript-eslint/no-var-requires, import/no-extraneous-dependencies */

const esbuild = require('esbuild');
// Automatically exclude all node_modules from the bundled version
// eslint-disable-next-line import/no-extraneous-dependencies, @typescript-eslint/no-var-requires
const {nodeExternalsPlugin} = require('esbuild-node-externals');
const {readdirSync} = require('fs');
const path = require('path');

const rulesDirectory = path.join(__dirname, 'src', 'rules');
const bundle = true;
const minify = true;
const platform = 'node';
const sourcemap = true;
const target = 'node12';
const plugins = [nodeExternalsPlugin()];

readdirSync(rulesDirectory).forEach((file) => {
const ruleFilePath = path.join(rulesDirectory, file);
const beginIndex = 0;
const endIndex = -3;
const ruleFileNameWithoutExtension = file.slice(beginIndex, endIndex);

esbuild
.build({
entryPoints: [ruleFilePath],
outfile: `dist/rules/${ruleFileNameWithoutExtension}.js`,
bundle,
minify,
platform,
sourcemap: false,
target,
plugins,
})
// eslint-disable-next-line unicorn/no-process-exit
.catch(() => process.exit(1));
});

esbuild
.build({
entryPoints: ['./src/api.ts'],
outfile: 'dist/api.js',
bundle: true,
minify: true,
platform: 'node',
sourcemap: true,
target: 'node14',
plugins: [nodeExternalsPlugin()],
bundle,
minify,
platform,
sourcemap,
target,
plugins,
})
// eslint-disable-next-line unicorn/no-process-exit
.catch(() => process.exit(1));
Expand All @@ -23,12 +53,12 @@ esbuild
.build({
entryPoints: ['./src/cli.ts'],
outfile: 'dist/cli.js',
bundle: true,
minify: true,
platform: 'node',
sourcemap: true,
target: 'node14',
plugins: [nodeExternalsPlugin()],
bundle,
minify,
platform,
sourcemap,
target,
plugins,
})
// eslint-disable-next-line unicorn/no-process-exit
.catch(() => process.exit(1));
9 changes: 4 additions & 5 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
"main": "dist/api.js",
"types": "index.d.ts",
"scripts": {
"build": "node esbuild.config.js",
"build": "npm run esbuild && npm run tsc",
"esbuild": "node esbuild.config.js",
"eslint": "eslint . --format=node_modules/eslint-formatter-pretty",
"npmpackagejsonlint": "node src/cli.js ./package.json",
"lint": "npm run eslint && npm run npmpackagejsonlint",
"test": "jest",
"test:ci": "jest --runInBand"
"test:ci": "jest --runInBand",
"tsc": "tsc --project tsconfig.json"
},
"dependencies": {
"ajv": "^8.10.0",
Expand All @@ -58,6 +60,7 @@
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/node": "^17.0.21",
"@typescript-eslint/eslint-plugin": "^5.12.1",
"esbuild": "^0.14.23",
"esbuild-node-externals": "^1.4.1",
Expand Down
32 changes: 20 additions & 12 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {validateRules} from './config/ConfigValidator';
import {transform} from './config/cosmicConfigTransformer';
import {applyExtendsIfSpecified} from './config/applyExtendsIfSpecified';
import {applyOverrides} from './config/applyOverrides';
import {Rules} from './rules';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const debug = require('debug')('npm-package-json-lint:Config');
Expand All @@ -14,30 +15,37 @@ const noRules = 0;
* @class
*/
export class Config {
/**
* The user passed config object.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any;

/**
* The current working directory.
*/
cwd: string;

/**
* The user passed configFile path.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
configFile: any;

/**
* The base directory that config should be pulled from.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
configBaseDirectory: any;

/**
* Rules object
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rules: any;

/**
* Constructor
*
* @param {string} cwd The current working directory.
* @param {Object} config The user passed config object.
* @param {string} configFile The user passed configFile path.
* @param {string} configBaseDirectory The base directory that config should be pulled from.
* @param {Object} rules Rules object
*/
constructor(cwd, config, configFile, configBaseDirectory, rules) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(cwd: string, config: any, configFile: any, configBaseDirectory: any, rules: Rules) {
if (config) {
this.config = applyExtendsIfSpecified(config, 'PassedConfig');
}
Expand All @@ -51,12 +59,12 @@ export class Config {
/**
* Gets the config for a file.
*
* @param {string} filePath File path of the file being linted.
* @param filePath File path of the file being linted.
* @returns {Object} A config object.
* @memberof Config
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
getConfigForFile(filePath) {
getConfigForFile(filePath: string) {
debug(`Getting config for ${filePath}`);
const filePathToSearch = filePath;

Expand Down
53 changes: 19 additions & 34 deletions src/NpmPackageJsonLint.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import isPlainObj from 'is-plain-obj';
import slash from 'slash';
import {PackageJson} from 'type-fest';
import {Config} from './Config';
import {Rules} from './Rules';
import {executeOnPackageJsonFiles, executeOnPackageJsonObject} from './linter/linter';
import {Rules} from './rules';
import {executeOnPackageJsonFiles, executeOnPackageJsonObject, OverallLintingResult} from './linter/linter';
import {getFileList} from './utils/getFileList';
import {getIgnorer} from './utils/getIgnorer';
import {Severity} from './types/severity';
import {PackageJsonFileLintingResult} from './types/package-json-linting-result';
import {LintIssue} from './lint-issue';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const debug = require('debug')('npm-package-json-lint:NpmPackageJsonLint');
Expand All @@ -17,29 +20,27 @@ const noIssues = 0;
/**
* Checks if the given issue is an error issue.
*
* @param {LintIssue} issue npm-package-json-lint issue
* @returns {boolean} True if error, false if warning.
* @param issue A {@link LintIssue} object
* @returns True if error, false if warning.
* @private
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const isIssueAnError = (issue) => issue.severity === Severity.Error;
const isIssueAnError = (issue: LintIssue): boolean => issue.severity === Severity.Error;

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const isPackageJsonObjectValid = (packageJsonObject) => isPlainObj(packageJsonObject);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isPackageJsonObjectValid = (packageJsonObject: PackageJson | any): boolean => isPlainObj(packageJsonObject);

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const areRequiredOptionsValid = (packageJsonObject, patterns) =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const areRequiredOptionsValid = (packageJsonObject: PackageJson | any, patterns: string[]): boolean =>
(!patterns && !isPackageJsonObjectValid(packageJsonObject)) ||
(patterns && (packageJsonObject || isPackageJsonObjectValid(packageJsonObject)));

/**
* Filters results to only include errors.
*
* @param {LintResult[]} results The results to filter.
* @returns {LintResult[]} The filtered results.
* @param results The results to filter.
* @returns The filtered results.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const getErrorResults = (results) => {
const getErrorResults = (results: PackageJsonFileLintingResult[]): PackageJsonFileLintingResult[] => {
const filtered = [];

results.forEach((result) => {
Expand All @@ -60,16 +61,6 @@ const getErrorResults = (results) => {
return filtered;
};

/**
* CLIEngine configuration object
*
* @typedef {Object} NpmPackageJsonLint
* @property {string} configFile The configuration file to use.
* @property {string} cwd The value to use for the current working directory.
* @property {boolean} useConfigFiles False disables use of .npmpackagejsonlintrc.json files, npmpackagejsonlint.config.js files, and npmPackageJsonLintConfig object in package.json file.
* @property {Object<string,*>} rules An object of rules to use.
*/

export interface NpmPackageJsonLintOptions {
cwd?: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -88,10 +79,6 @@ export interface NpmPackageJsonLintOptions {
fix?: boolean;
}

/**
* Public CLIEngine class
* @class
*/
export class NpmPackageJsonLint {
cwd: string;

Expand All @@ -117,7 +104,7 @@ export class NpmPackageJsonLint {

/**
* constructor
* @param {NpmPackageJsonLint} options The options for the CLIEngine.
* @param options An instance of the {@link NpmPackageJsonLintOptions} options object.
* @constructor
*/
constructor(options: NpmPackageJsonLintOptions) {
Expand Down Expand Up @@ -154,11 +141,9 @@ export class NpmPackageJsonLint {
/**
* Runs the linter using the config specified in the constructor
*
* @returns {LinterResult} The results {@link LinterResult} from linting a collection of package.json files.
* @memberof NpmPackageJsonLint
* @returns The results {@link OverallLintingResult} from linting a collection of package.json files.
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
lint() {
lint(): OverallLintingResult {
debug('Starting lint');

if (areRequiredOptionsValid(this.packageJsonObject, this.patterns)) {
Expand All @@ -168,7 +153,7 @@ export class NpmPackageJsonLint {
}

const ignorer = getIgnorer(this.cwd, this.ignorePath);
let linterOutput;
let linterOutput: OverallLintingResult;

if (this.patterns) {
debug('Linting using patterns');
Expand Down
Loading

0 comments on commit 78853eb

Please sign in to comment.