Skip to content
Open
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"nodemon": "^3.1.14",
"ora": "9.4.0",
"outdent": "0.8.0",
"pkg-up": "3.1.0",
"prettier": "3.8.3",
"prompts": "^2.4.2",
"typescript": "5.9.3",
Expand Down
42 changes: 0 additions & 42 deletions pnpm-lock.yaml

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

36 changes: 36 additions & 0 deletions src/cli/commands/utils/find-package-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from 'fs/promises';

Check warning on line 1 in src/cli/commands/utils/find-package-json.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `node:fs/promises` over `fs/promises`.

See more on https://sonarcloud.io/project/issues?id=strapi_sdk-plugin&issues=AZ50J9lR5WQG7-EhUxS4&open=AZ50J9lR5WQG7-EhUxS4&pullRequest=169
import path from 'path';

Check warning on line 2 in src/cli/commands/utils/find-package-json.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `node:path` over `path`.

See more on https://sonarcloud.io/project/issues?id=strapi_sdk-plugin&issues=AZ50J9lR5WQG7-EhUxS5&open=AZ50J9lR5WQG7-EhUxS5&pullRequest=169

/**
* Find the closest package.json file by walking up from `cwd`.
*/
const findPackageJson = async (cwd: string): Promise<string | undefined> => {
let current = path.resolve(cwd);
const { root } = path.parse(current);

while (current !== root) {
const candidate = path.join(current, 'package.json');

try {
await fs.access(candidate);

return candidate;
} catch {
// Continue searching in the parent directory.
}

current = path.dirname(current);
}

const rootCandidate = path.join(root, 'package.json');

try {
await fs.access(rootCandidate);

return rootCandidate;
} catch {
return undefined;
}
};

export { findPackageJson };
5 changes: 3 additions & 2 deletions src/cli/commands/utils/pkg.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import chalk from 'chalk';
import fs from 'fs/promises';
import os from 'os';
import pkgUp from 'pkg-up';
import * as yup from 'yup';

import { findPackageJson } from './find-package-json';

import type { Logger } from './logger';

interface Export {
Expand Down Expand Up @@ -64,7 +65,7 @@ const packageJsonSchema = yup.object({
* the process will throw with an appropriate error message.
*/
const loadPkg = async ({ cwd, logger }: { cwd: string; logger: Logger }): Promise<object> => {
const pkgPath = await pkgUp({ cwd });
const pkgPath = await findPackageJson(cwd);

if (!pkgPath) {
throw new Error('Could not find a package.json in the current directory');
Expand Down
5 changes: 3 additions & 2 deletions src/cli/commands/utils/validation/pkg-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import chalk from 'chalk';
import fs from 'fs/promises';
import os from 'os';
import pkgUp from 'pkg-up';
import * as yup from 'yup';

import { findPackageJson } from '../find-package-json';

import type { Export } from './types';

const record = (value: unknown) =>
Expand Down Expand Up @@ -161,7 +162,7 @@ export const loadPkg = async ({
cwd: string;
logger: Logger;
}): Promise<object> => {
const pkgPath = await pkgUp({ cwd });
const pkgPath = await findPackageJson(cwd);

if (!pkgPath) {
throw new Error('Could not find a package.json in the current directory');
Expand Down
Loading