From a8cf48e40dfe41c9cb9e34ad1395262ee1e797ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Fri, 20 Jan 2023 13:13:57 +0100 Subject: [PATCH] feat: port ruleset loading logic from spectral-cli --- src/getRuleset.ts | 71 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/src/getRuleset.ts b/src/getRuleset.ts index dbcccfb..77c6920 100644 --- a/src/getRuleset.ts +++ b/src/getRuleset.ts @@ -1,14 +1,18 @@ import * as fs from 'fs'; import * as process from 'process'; +import { createRequire } from 'module'; import type { Optional } from '@stoplight/types'; -import { Ruleset } from '@stoplight/spectral-core'; +import { Ruleset, RulesetDefinition } from '@stoplight/spectral-core'; import { info, error } from '@actions/core'; +import { isError, isObject } from 'lodash'; import * as path from '@stoplight/path'; import { fetch } from '@stoplight/spectral-runtime'; -import type { IO } from '@stoplight/spectral-ruleset-bundler'; +import { migrateRuleset, isBasicRuleset } from '@stoplight/spectral-ruleset-migrator'; +import { bundleRuleset } from '@stoplight/spectral-ruleset-bundler'; +import { node } from '@stoplight/spectral-ruleset-bundler/presets/node'; import { builtins } from '@stoplight/spectral-ruleset-bundler/plugins/builtins'; import { commonjs } from '@stoplight/spectral-ruleset-bundler/plugins/commonjs'; -import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader'; +import { stdin } from '@stoplight/spectral-ruleset-bundler/plugins/stdin'; async function getDefaultRulesetFile(): Promise> { const cwd = process.cwd(); @@ -21,6 +25,10 @@ async function getDefaultRulesetFile(): Promise> { return; } +function isErrorWithCode(error: Error | (Error & { code: unknown })): error is Error & { code: string } { + return 'code' in error && typeof error.code === 'string'; +} + export async function getRuleset(rulesetFile: Optional): Promise { if (!rulesetFile) { rulesetFile = await getDefaultRulesetFile(); @@ -36,12 +44,63 @@ export async function getRuleset(rulesetFile: Optional): Promise req(req.resolve(id, { paths })); + + // eslint-disable-next-line @typescript-eslint/no-implied-eval + Function('module, require', source)(m, _require); + + if (!isObject(m.exports)) { + throw new Error('No valid export found'); + } + + return m.exports; +}