Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): inline html templates in html formatter #2474

Merged
merged 8 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ commands:
steps:
- run:
name: Lint code
command: yarn lint.eslint
command: |
yarn prelint
yarn lint.eslint

lint-documentation:
steps:
Expand Down
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/test-harness/tests/
/packages/*/dist
/packages/*/CHANGELOG.md
packages/cli/src/formatters/html/templates.ts
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ node_modules
!.yarn/sdks
!.yarn/versions

packages/cli/src/formatters/html/templates.ts
packages/cli/binaries
/test-harness/tmp/
/test-harness/tests/
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
},
"scripts": {
"clean": "rimraf .cache packages/*/{dist,.cache}",
"prebuild": "yarn workspace @stoplight/spectral-ruleset-migrator prebuild",
"build": "yarn prebuild && tsc --build ./tsconfig.build.json && yarn postbuild",
"postbuild": "yarn workspace @stoplight/spectral-cli postbuild",
"lint": "yarn lint.prettier && yarn lint.eslint",
"prebuild": "yarn workspaces foreach run prebuild",
"build": "yarn prebuild && tsc --build ./tsconfig.build.json",
"prelint": "yarn workspaces foreach run prelint",
"lint": "yarn prelint && yarn lint.prettier && yarn lint.eslint",
"lint.fix": "yarn lint.prettier --write && yarn lint.eslint --fix",
"lint.eslint": "eslint --cache --cache-location .cache/.eslintcache --ext=.js,.mjs,.ts packages test-harness",
"lint.prettier": "prettier --ignore-path .eslintignore --ignore-unknown --check packages/core/src/ruleset/meta/*.json packages/rulesets/src/{asyncapi,oas}/schemas/*.json docs/**/*.md README.md",
"pretest": "yarn workspace @stoplight/spectral-ruleset-migrator pretest",
"pretest": "yarn workspaces foreach run pretest",
"test": "yarn pretest && yarn test.karma && yarn test.jest",
"pretest.harness": "ts-node -T test-harness/scripts/generate-tests.ts",
"test.harness": "yarn pretest.harness && jest -c test-harness/jest.config.mjs",
Expand Down
12 changes: 7 additions & 5 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"build.binary": "pkg . --output ./binaries/spectral",
"build.windows": "pkg . --targets windows --out-path ./binaries",
"build.nix": "pkg . --targets linux,macos,alpine --out-path ./binaries",
"postbuild": "copyfiles -u 1 \"./src/formatters/html/*.html\" \"./dist/\"",
"pretest": "yarn prebuild",
"prelint": "yarn prebuild",
"prebuild": "node scripts/bundle-html-templates.mjs",
"cli": "node -r ts-node/register/transpile-only -r tsconfig-paths/register src/index.ts",
"cli:debug": "node -r ts-node/register/transpile-only -r tsconfig-paths/register --inspect-brk src/index.ts",
"release": "semantic-release -e semantic-release-monorepo"
Expand All @@ -45,8 +47,7 @@
"@stoplight/types": "^13.6.0",
"chalk": "4.1.2",
"cliui": "7.0.4",
"eol": "0.9.1",
"fast-glob": "3.2.7",
"fast-glob": "~3.2.12",
"lodash": "~4.17.21",
"pony-cause": "^1.0.0",
"proxy-agent": "5.0.0",
Expand All @@ -60,7 +61,9 @@
"@types/es-aggregate-error": "^1.0.2",
"@types/xml2js": "^0.4.9",
"@types/yargs": "^17.0.8",
"copyfiles": "^2.4.1",
"ast-types": "^0.14.2",
"astring": "^1.8.4",
"eol": "0.9.1",
"es-aggregate-error": "^1.0.7",
"nock": "^13.1.3",
"node-html-parser": "^4.1.5",
Expand All @@ -74,7 +77,6 @@
],
"assets": [
"./dist/**/*.json",
"./dist/**/*.html",
"../*/dist/**/*.js.map",
"../*/src/**/*.ts"
]
Expand Down
25 changes: 25 additions & 0 deletions packages/cli/scripts/bundle-html-templates.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env node
import * as path from 'node:path';
import { promises as fs } from 'node:fs';
import { fileURLToPath } from 'node:url';
import * as astring from 'astring';
import { builders as b } from 'ast-types';

import eol from 'eol';
import fg from 'fast-glob';

const cwd = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');

fg('src/formatters/html/*.html', { cwd, absolute: true })
.then(files =>
Promise.all(
files.map(async file => ({ file: path.basename(file), content: eol.lf(await fs.readFile(file, 'utf8')) })),
),
)
.then(async items => {
const root = b.exportDefaultDeclaration(
b.objectExpression(items.map(({ file, content }) => b.property('init', b.literal(file), b.literal(content)))),
);

await fs.writeFile(path.join(cwd, 'src/formatters/html/templates.ts'), astring.generate(root));
});
2 changes: 1 addition & 1 deletion packages/cli/src/formatters/__tests__/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DiagnosticSeverity } from '@stoplight/types';
import { parse } from 'node-html-parser';
import { html } from '../html';

const mixedErrors = require('./__fixtures__/mixed-errors.json');
import mixedErrors from './__fixtures__/mixed-errors.json';

describe('HTML formatter', () => {
test('should display proper severity levels', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/formatters/__tests__/junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Parser } from 'xml2js';
import { junit } from '../junit';
import { DiagnosticSeverity } from '@stoplight/types';

const oas3SchemaErrors = require('./__fixtures__/oas3-schema-errors.json');
const mixedErrors = require('./__fixtures__/mixed-errors.json');
const specialXmlStrings = require('./__fixtures__/errors-with-special-xml-strings.json');
import oas3SchemaErrors from './__fixtures__/oas3-schema-errors.json';
import mixedErrors from './__fixtures__/mixed-errors.json';
import specialXmlStrings from './__fixtures__/errors-with-special-xml-strings.json';

describe('JUnit formatter', () => {
let parse: Parser['parseStringPromise'];
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/formatters/__tests__/pretty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { DiagnosticSeverity } from '@stoplight/types';
import chalk from 'chalk';
import { pretty } from '../pretty';

const oas3SchemaErrors = require('./__fixtures__/oas3-schema-errors.json');
const mixedErrors = require('./__fixtures__/mixed-errors.json');
import oas3SchemaErrors from './__fixtures__/oas3-schema-errors.json';
import mixedErrors from './__fixtures__/mixed-errors.json';

function setColumnWidth(width: number, func: CallableFunction): void {
const og = process.stdout.columns;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/formatters/__tests__/stylish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { DiagnosticSeverity } from '@stoplight/types';
import chalk from 'chalk';
import { stylish } from '../stylish';

const oas3SchemaErrors = require('./__fixtures__/oas3-schema-errors.json');
const mixedErrors = require('./__fixtures__/mixed-errors.json');
import oas3SchemaErrors from './__fixtures__/oas3-schema-errors.json';
import mixedErrors from './__fixtures__/mixed-errors.json';

describe('Stylish formatter', () => {
test('should prefer message for oas-schema errors', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/formatters/__tests__/teamcity.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { teamcity } from '../teamcity';

const mixedErrors = require('./__fixtures__/mixed-errors.json');
import mixedErrors from './__fixtures__/mixed-errors.json';

describe('Teamcity formatter', () => {
test('should format messages', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/formatters/__tests__/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { text } from '../text';

const mixedErrors = require('./__fixtures__/mixed-errors.json');
import mixedErrors from './__fixtures__/mixed-errors.json';

describe('Text formatter', () => {
test('should format messages', () => {
Expand Down
12 changes: 5 additions & 7 deletions packages/cli/src/formatters/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,20 @@
* @fileoverview HTML reporter
* @author Julian Laval
*/
import * as path from '@stoplight/path';
import { Dictionary } from '@stoplight/types';
import * as eol from 'eol';
import * as fs from 'fs';
import { template } from 'lodash';
import template from 'lodash/template';
import type { IRuleResult } from '@stoplight/spectral-core';
import { Formatter } from '../types';
import { getHighestSeverity, getSeverityName, getSummary, getSummaryForSource, groupBySource } from '../utils';
import templates from './templates';

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------

const pageTemplate = template(eol.lf(fs.readFileSync(path.join(__dirname, 'html-template-page.html'), 'utf8')));
const messageTemplate = template(eol.lf(fs.readFileSync(path.join(__dirname, 'html-template-message.html'), 'utf8')));
const resultTemplate = template(eol.lf(fs.readFileSync(path.join(__dirname, 'html-template-result.html'), 'utf8')));
const pageTemplate = template(templates['html-template-page.html']);
const messageTemplate = template(templates['html-template-message.html']);
const resultTemplate = template(templates['html-template-result.html']);

function renderMessages(messages: IRuleResult[], parentIndex: number): string {
return messages
Expand Down
Loading