Skip to content

Commit f0c0b94

Browse files
committed
refactor: fix __dirname and package.json import issues (#2029)
1 parent 2de15e1 commit f0c0b94

File tree

21 files changed

+41
-52
lines changed

21 files changed

+41
-52
lines changed

.github/workflows/smoke-plugins.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ jobs:
6565
with:
6666
node-version: 20
6767
- run: bash ./__tests__/smoke-plugins/run-smoke.sh
68+
6869
run-smoke-plugins--npm--node-20--windows:
6970
needs: prepare-smoke-plugins
7071
runs-on: windows-latest

.github/workflows/smoke.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ jobs:
225225
226226
# Check for broken styles when building docs (related issue: https://github.com/Redocly/redocly-cli/issues/1073)
227227
echo "Checking docs for issues..."
228-
diff pre-built/redoc.html redoc-static.html
228+
diff pre-built/redoc.html redoc-static.html -u
229229
echo "✅ Docs built correctly."
230230
231231
# Check for broken $refs (or other issues) in the split files, especially on Windows (it will fail on a difference)
232232
echo "Checking split files for issues..."
233-
diff -r pre-split output/split
233+
diff -r pre-split output/split -u
234234
echo "✅ Files split correctly."

__tests__/smoke/pre-built/redoc.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311
" class="sc-dZnbom sc-eStNqG sc-ecLbSZ ewwVxe jZPdSZ dcgeal"><p>OK</p>
312312
</div></button></div><div><button class="sc-jZkJda iesDBN"><svg class="sc-cXrvCr cPuBeZ" version="1.1" viewBox="0 0 24 24" x="0" xmlns="http://www.w3.org/2000/svg" y="0" aria-hidden="true"><polygon points="17.3 8.3 12 13.6 6.7 8.3 5.3 9.7 12 16.4 18.7 9.7 "></polygon></svg><strong class="sc-dNrSaK eSFldv">400<!-- --> </strong><div html="&lt;p&gt;Bad request.&lt;/p&gt;
313313
" class="sc-dZnbom sc-eStNqG sc-ecLbSZ ewwVxe jZPdSZ dcgeal"><p>Bad request.</p>
314-
</div></button></div></div></div><div class="sc-jgmqJh sc-gIIpAQ egscYh brdjDi"><div class="sc-dTdpwD ePBxvF"><button class="sc-ligLZB bgPLGr"><span type="get" class="sc-eeA-dwE hMeoaw http-verb get">get</span><span class="sc-ieSqsC ioopTP">/hello</span><svg class="sc-cXrvCr dOdtJx" style="margin-right:-25px" version="1.1" viewBox="0 0 24 24" x="0" xmlns="http://www.w3.org/2000/svg" y="0" aria-hidden="true"><polygon points="17.3 8.3 12 13.6 6.7 8.3 5.3 9.7 12 16.4 18.7 9.7 "></polygon></svg></button><div aria-hidden="true" class="sc-dMGIQz kCxvuD"><div class="sc-hYvJMy sxQGy"><div html="" class="sc-dZnbom sc-eStNqG ewwVxe gxENBN"></div><div tabindex="0" role="button"><div class="sc-lbKftx iPVodL"><span>http://redocly-example.com</span>/hello</div></div></div></div></div><div><h3 class="sc-jOTjWM dhJZBt"> <!-- -->Response samples<!-- --> </h3><div class="sc-bwSFLc gTnidh" data-rttabs="true"><ul class="react-tabs__tab-list" role="tablist"><li class="tab-success react-tabs__tab--selected" role="tab" id="tab:R9pq:0" aria-selected="true" aria-disabled="false" aria-controls="panel:R9pq:0" tabindex="0" data-rttab="true">200</li><li class="tab-error" role="tab" id="tab:R9pq:1" aria-selected="false" aria-disabled="false" aria-controls="panel:R9pq:1" data-rttab="true">400</li></ul><div class="react-tabs__tab-panel react-tabs__tab-panel--selected" role="tabpanel" id="panel:R9pq:0" aria-labelledby="tab:R9pq:0"><div><div class="sc-brVvGI bVwklo"><span class="sc-fKfBHT gEDIwZ">Content type</span><div class="sc-buNqiq cnTPdi">application/json</div></div><div class="sc-eTAtz eQHTDF"><div class="sc-fowSIr gzsuAn"><div class="sc-ljIkKL jwxgAi"><button><div class="sc-fcNxSY kochun">Copy</div></button></div><div class="sc-dZnbom ewwVxe sc-gozoIq hnBcqa"><div class="redoc-json"><code><button class="collapser" aria-label="collapse"></button><span class="token punctuation">{</span><span class="ellipsis"></span><ul class="obj collapsible"><li><div class="hoverable "><span class="property token string">"message"</span>: <span class="token string">&quot;string&quot;</span></div></li></ul><span class="token punctuation">}</span></code></div></div></div></div></div></div><div class="react-tabs__tab-panel" role="tabpanel" id="panel:R9pq:1" aria-labelledby="tab:R9pq:1"></div></div></div></div></div></div></div><div class="sc-dJOOoR dKumiZ"></div></div></div>
314+
</div></button></div></div></div><div class="sc-jgmqJh sc-gIIpAQ egscYh brdjDi"><div class="sc-dTdpwD ePBxvF"><button class="sc-ligLZB bgPLGr"><span type="get" class="sc-eeA-dwE hMeoaw http-verb get">get</span><span class="sc-ieSqsC ioopTP">/hello</span><svg class="sc-cXrvCr dOdtJx" style="margin-right:-25px" version="1.1" viewBox="0 0 24 24" x="0" xmlns="http://www.w3.org/2000/svg" y="0" aria-hidden="true"><polygon points="17.3 8.3 12 13.6 6.7 8.3 5.3 9.7 12 16.4 18.7 9.7 "></polygon></svg></button><div aria-hidden="true" class="sc-dMGIQz kCxvuD"><div class="sc-hYvJMy sxQGy"><div html="" class="sc-dZnbom sc-eStNqG ewwVxe gxENBN"></div><div tabindex="0" role="button"><div class="sc-lbKftx iPVodL"><span>http://redocly-example.com</span>/hello</div></div></div></div></div><div><h3 class="sc-jOTjWM dhJZBt"> <!-- -->Response samples<!-- --> </h3><div class="sc-bwSFLc gTnidh" data-rttabs="true"><ul class="react-tabs__tab-list" role="tablist"><li class="tab-success react-tabs__tab--selected" role="tab" id="tab«R9pq»0" aria-selected="true" aria-disabled="false" aria-controls="panel«R9pq»0" tabindex="0" data-rttab="true">200</li><li class="tab-error" role="tab" id="tab«R9pq»1" aria-selected="false" aria-disabled="false" aria-controls="panel«R9pq»1" data-rttab="true">400</li></ul><div class="react-tabs__tab-panel react-tabs__tab-panel--selected" role="tabpanel" id="panel«R9pq»0" aria-labelledby="tab«R9pq»0"><div><div class="sc-brVvGI bVwklo"><span class="sc-fKfBHT gEDIwZ">Content type</span><div class="sc-buNqiq cnTPdi">application/json</div></div><div class="sc-eTAtz eQHTDF"><div class="sc-fowSIr gzsuAn"><div class="sc-ljIkKL jwxgAi"><button><div class="sc-fcNxSY kochun">Copy</div></button></div><div class="sc-dZnbom ewwVxe sc-gozoIq hnBcqa"><div class="redoc-json"><code><button class="collapser" aria-label="collapse"></button><span class="token punctuation">{</span><span class="ellipsis"></span><ul class="obj collapsible"><li><div class="hoverable "><span class="property token string">"message"</span>: <span class="token string">&quot;string&quot;</span></div></li></ul><span class="token punctuation">}</span></code></div></div></div></div></div></div><div class="react-tabs__tab-panel" role="tabpanel" id="panel«R9pq»1" aria-labelledby="tab«R9pq»1"></div></div></div></div></div></div></div><div class="sc-dJOOoR dKumiZ"></div></div></div>
315315
<script>
316316
const __redoc_state = {"menu":{"activeItemIdx":-1},"spec":{"data":{"openapi":"3.1.0","servers":[{"url":"http://redocly-example.com"}],"info":{"title":"Sample API","version":"1.0.0"},"paths":{"/hello":{"get":{"operationId":"getMessage","security":[],"summary":"Get a greeting message","responses":{"200":{"description":"OK","content":{"application/json":{"schema":{"$ref":"#/components/schemas/message-schema"}}}},"400":{"$ref":"#/components/responses/BadRequest"}}}}},"components":{"schemas":{"message-schema":{"type":"object","properties":{"message":{"type":"string"}}},"Error":{"type":"object","properties":{"type":{"type":"string","example":"object"},"title":{"type":"string","example":"Validation failed"}}}},"responses":{"BadRequest":{"description":"Bad request.","content":{"application/problem+json":{"schema":{"$ref":"#/components/schemas/Error"}}}}}}}},"searchIndex":{"store":["operation/getMessage"],"index":{"version":"2.3.9","fields":["title","description"],"fieldVectors":[["title/0",[0,0.288,1,0.288]],["description/0",[2,0.288]]],"invertedIndex":[["greet",{"_index":0,"title":{"0":{}},"description":{}}],["hello",{"_index":2,"title":{},"description":{"0":{}}}],["messag",{"_index":1,"title":{"0":{}},"description":{}}]],"pipeline":[]}},"options":{}};
317317

packages/cli/src/__tests__/commands/lint.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ describe('handleLint', () => {
5656
async (entrypoints) => entrypoints?.map((path: string) => ({ path })) ?? []
5757
);
5858

59-
vi.mock('../../utils/update-version-notifier.js', () => ({
60-
version: '1.0.0',
59+
vi.mock('../../utils/package.js', () => ({
60+
version: '2.0.0',
6161
}));
6262
});
6363

@@ -137,7 +137,7 @@ describe('handleLint', () => {
137137
format: 'stylish',
138138
maxProblems: 2,
139139
totals: { errors: 0 },
140-
version: '1.0.0',
140+
version: '2.0.0',
141141
});
142142
expect(getExecutionTime).toHaveBeenCalledWith(42);
143143
});

packages/cli/src/commands/build-docs/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import type { BuildDocsArgv } from './types.js';
1515
import type { CommandArgs } from '../../wrapper.js';
1616

17-
const packageJson = createRequire(import.meta.url)('../../../package.json');
17+
const packageJson = createRequire(import.meta.url ?? __dirname)('../../../package.json');
1818

1919
export const handlerBuildCommand = async ({
2020
argv,

packages/cli/src/commands/build-docs/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { fileURLToPath } from 'node:url';
1313
import type { Config } from '@redocly/openapi-core';
1414
import type { BuildDocsOptions } from './types.js';
1515

16-
const __dirname = dirname(fileURLToPath(import.meta.url));
16+
const __internalDirname = dirname(fileURLToPath(import.meta.url ?? __dirname));
1717

1818
export function getObjectOrJSON(
1919
openapiOptions: string | Record<string, unknown>,
@@ -77,7 +77,7 @@ export async function getPageHTML(
7777
? templateFileName
7878
: redocOptions?.htmlTemplate
7979
? resolve(configPath ? dirname(configPath) : '', redocOptions.htmlTemplate)
80-
: join(__dirname, './template.hbs');
80+
: join(__internalDirname, './template.hbs');
8181
const template = handlebars.compile(readFileSync(templateFileName).toString());
8282
return template({
8383
redocHTML: `

packages/cli/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ import { handleLint } from './commands/lint.js';
1515
import { handleBundle } from './commands/bundle.js';
1616
import { handleLogin, handleLogout } from './commands/auth.js';
1717
import { handlerBuildCommand } from './commands/build-docs/index.js';
18-
import {
19-
cacheLatestVersion,
20-
notifyUpdateCliVersion,
21-
version,
22-
} from './utils/update-version-notifier.js';
18+
import { cacheLatestVersion, notifyUpdateCliVersion } from './utils/update-version-notifier.js';
2319
import { commandWrapper } from './wrapper.js';
2420
import { previewProject } from './commands/preview-project/index.js';
2521
import { handleTranslations } from './commands/translations.js';
2622
import { handleEject } from './commands/eject.js';
2723
import { PRODUCT_PLANS } from './commands/preview-project/constants.js';
24+
import { version } from './utils/package.js';
2825

2926
import type { Arguments } from 'yargs';
3027
import type { OutputFormat, RuleSeverity } from '@redocly/openapi-core';

packages/cli/src/otel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Resource as OtelResource } from '@opentelemetry/resources';
33
import { NodeTracerProvider, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node';
44
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
55
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
6-
import { version } from './utils/update-version-notifier.js';
6+
import { version } from './utils/package.js';
77
import { DEFAULT_FETCH_TIMEOUT } from './utils/fetch-with-timeout.js';
88

99
import type { Analytics } from './utils/miscellaneous.js';

packages/cli/src/utils/assert-node-version.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import { createRequire } from 'node:module';
21
import * as process from 'node:process';
32
import * as semver from 'semver';
43
import { yellow } from 'colorette';
4+
import { engines } from './package.js';
55

66
try {
7-
const packageJson = createRequire(import.meta.url)('../../package.json');
8-
const { engines } = packageJson;
9-
const version = engines.node;
7+
const range = engines?.node;
108

11-
if (!semver.satisfies(process.version, version)) {
9+
if (typeof range === 'string' && !semver.satisfies(process.version, range)) {
1210
process.stderr.write(
1311
yellow(
14-
`\n⚠️ Warning: failed to satisfy expected node version. Expected: "${version}", Current "${process.version}"\n\n`
12+
`\n⚠️ Warning: failed to satisfy expected node version. Expected: "${range}", Current "${process.version}"\n\n`
1513
)
1614
);
1715
}

packages/cli/src/utils/miscellaneous.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
} from '@redocly/openapi-core';
2626
import { deprecatedRefDocsSchema } from '@redocly/config/lib/reference-docs-config-schema.js';
2727
import { outputExtensions } from '../types.js';
28-
import { version } from './update-version-notifier.js';
28+
import { version } from './package.js';
2929
import { getReuniteUrl } from '../reunite/api/index.js';
3030

3131
import type { Arguments } from 'yargs';

0 commit comments

Comments
 (0)