Skip to content

Commit 6c01975

Browse files
committed
fix: package validation
1 parent c8760f4 commit 6c01975

File tree

4 files changed

+51
-32
lines changed

4 files changed

+51
-32
lines changed

src/cli/commands/plugin/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const action = async ({ ...opts }: BuildCLIOptions, _cmd: unknown, { logger, cwd
1919
process.env.NODE_ENV = 'production';
2020

2121
const pkg = await loadPkg({ cwd, logger });
22-
const pkgJson = await validatePkg({ pkg });
22+
const pkgJson = await validatePkg({ pkg, logger });
2323

2424
if (!pkgJson.exports['./strapi-admin'] && !pkgJson.exports['./strapi-server']) {
2525
throw new Error(

src/cli/commands/plugin/link-watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const action = async (_opts: ActionOptions, _cmd: unknown, { cwd, logger }: CLIC
2525
}
2626

2727
const pkg = await loadPkg({ cwd, logger });
28-
const pkgJson = await validatePkg({ pkg });
28+
const pkgJson = await validatePkg({ pkg, logger });
2929

3030
logger.info(
3131
outdent`

src/cli/commands/plugin/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ActionOptions = WatchCLIOptions;
1616
const action = async (opts: ActionOptions, _cmd: unknown, { cwd, logger }: CLIContext) => {
1717
try {
1818
const pkg = await loadPkg({ cwd, logger });
19-
const pkgJson = await validatePkg({ pkg });
19+
const pkgJson = await validatePkg({ pkg, logger });
2020

2121
if (!pkgJson.exports['./strapi-admin'] && !pkgJson.exports['./strapi-server']) {
2222
throw new Error(

src/cli/commands/utils/pkg.ts

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,49 @@ interface Export {
1515
default: string;
1616
}
1717

18-
const packageJsonSchema = yup.object({
19-
name: yup.string().required(),
20-
exports: yup.lazy((value) =>
21-
yup
22-
.object(
23-
typeof value === 'object'
24-
? Object.entries(value).reduce((acc, [key, keyValue]) => {
25-
if (typeof keyValue === 'object') {
26-
acc[key] = yup
27-
.object({
18+
const createPackageJsonSchema = (logger: Logger) =>
19+
yup.object({
20+
name: yup.string().required(),
21+
exports: yup.lazy((value) =>
22+
yup
23+
.object(
24+
typeof value === 'object'
25+
? Object.entries(value).reduce((acc, [key, keyValue]) => {
26+
if (typeof keyValue === 'object') {
27+
acc[key] = yup.object({
2828
types: yup.string().optional(),
2929
source: yup.string().required(),
3030
module: yup.string().optional(),
3131
import: yup.string().required(),
3232
require: yup.string().required(),
3333
default: yup.string().required(),
34-
})
35-
.noUnknown(true);
36-
} else {
37-
acc[key] = yup
38-
.string()
39-
.matches(/^\.\/.*\.(json|d\.ts)$/)
40-
.required();
41-
}
42-
43-
return acc;
44-
}, {} as Record<string, yup.SchemaOf<string> | yup.SchemaOf<Export>>)
45-
: undefined
46-
)
47-
.optional()
48-
),
49-
});
34+
});
35+
} else {
36+
acc[key] = yup
37+
.string()
38+
.test(
39+
'warn-if-not-matching',
40+
'Warning: The file does not match the expected pattern (./*.json or ./*.d.ts)',
41+
(innervalue) => {
42+
const isValid = /^\.\/.*\.(json|d\.ts)$/.test(innervalue || '');
43+
if (!isValid) {
44+
logger.warn(
45+
`Warning: '${key}' in 'exports' does not match the expected pattern.`
46+
);
47+
}
48+
return true; // Always return true to pass validation
49+
}
50+
)
51+
.required();
52+
}
53+
54+
return acc;
55+
}, {} as Record<string, yup.SchemaOf<string> | yup.SchemaOf<Export>>)
56+
: undefined
57+
)
58+
.optional()
59+
),
60+
});
5061

5162
/**
5263
* @description being a task to load the package.json starting from the current working directory
@@ -69,13 +80,21 @@ const loadPkg = async ({ cwd, logger }: { cwd: string; logger: Logger }): Promis
6980
return pkg;
7081
};
7182

72-
type PackageJson = yup.Asserts<typeof packageJsonSchema>;
83+
type PackageJson = yup.Asserts<ReturnType<typeof createPackageJsonSchema>>;
7384

7485
/**
7586
* @description validate the package.json against a standardised schema using `yup`.
7687
* If the validation fails, the process will throw with an appropriate error message.
7788
*/
78-
const validatePkg = async ({ pkg }: { pkg: object }): Promise<PackageJson> => {
89+
const validatePkg = async ({
90+
pkg,
91+
logger,
92+
}: {
93+
pkg: object;
94+
logger: Logger;
95+
}): Promise<PackageJson> => {
96+
const packageJsonSchema = createPackageJsonSchema(logger);
97+
7998
try {
8099
const validatedPkg = await packageJsonSchema.validate(pkg, {
81100
strict: true,
@@ -114,7 +133,7 @@ const validatePkg = async ({ pkg }: { pkg: object }): Promise<PackageJson> => {
114133
throw new Error(
115134
`'${err.path}' in 'package.json' must be of type '${chalk.magenta(
116135
err.params.type
117-
)}' (recieved '${chalk.magenta(typeof err.params.value)}')`
136+
)}' (received '${chalk.magenta(typeof err.params.value)}')`
118137
);
119138
}
120139
}

0 commit comments

Comments
 (0)