Skip to content

Commit

Permalink
Only emit warnings on first start (#186)
Browse files Browse the repository at this point in the history
* Only warnThemePackage on not restart

* Tweak warning messages

* Tweak changelog description

---------

Co-authored-by: Bryce Russell <[email protected]>
  • Loading branch information
Yuhanawa and BryceRussell authored Dec 14, 2024
1 parent 7cb29e4 commit aa826a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-buttons-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-theme-provider": patch
---

Only emit package warnings on the first start
9 changes: 4 additions & 5 deletions package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default function <ThemeName extends string, Schema extends z.ZodTypeAny>(
if (existsSync(seedEntrypoint)) extendDb({ seedEntrypoint });
},
"astro:config:setup": (params) => {
const { config, logger, injectRoute, addMiddleware } = params;
const { config, logger, isRestart, injectRoute, addMiddleware } = params;

const projectRoot = resolveDirectory("./", config.root);

Expand All @@ -167,8 +167,7 @@ export default function <ThemeName extends string, Schema extends z.ZodTypeAny>(
ThemeIntegrationsResolved: "",
};


if (logLevel) {
if (logLevel && !isRestart) {
// Warn about issues with theme's `package.json`
warnThemePackage(themePackage, logger);
}
Expand Down Expand Up @@ -398,10 +397,10 @@ export default function <ThemeName extends string, Schema extends z.ZodTypeAny>(
},
"astro:config:done": ({ injectTypes }) => {
injectTypes({
filename: 'theme.d.ts',
filename: "theme.d.ts",
content: themeTypesBuffer,
});
}
},
},
};

Expand Down
73 changes: 43 additions & 30 deletions package/src/utils/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,46 +40,59 @@ export function warnThemePackage(pkg: PackageJSON, logger: HookParameters<"astro

// If package is not private, warn theme author about issues with package
if (!isPrivate) {
let hasIssues = false;

const warn = (condition: boolean, message: string) => {
if (condition) {
hasIssues = true;
logger.warn(message);
}
};

// Warn theme author if `astro-integration` keyword does not exist inside 'package.json'
if (!keywords.includes("astro-integration")) {
logger.warn(
`Add the 'astro-integration' keyword to your theme's 'package.json'!\tAstro uses this value to support the command 'astro add ${name}'\n\n\t"keywords": [ "astro-integration" ],\n`,
);
}
warn(
!keywords.includes("astro-integration"),
`Add the 'astro-integration' keyword to your theme's 'package.json':\n\n\t"keywords": [ "astro-integration" ],\n\nAstro uses this value to support the command 'astro add ${name}'\n`,
);

// Warn theme author if no 'description' property exists inside 'package.json'
if (!description) {
logger.warn(
`Add a 'description' to your theme's 'package.json'!\tAstro uses this value to populate the integrations page https://astro.build/integrations/\n\n\t"description": "My awesome Astro theme!",\n`,
);
}
warn(
!description,
`Add a 'description' to your theme's 'package.json':\n\n\t"description": "My awesome Astro theme!",\n\nAstro uses this value to populate the integrations page https://astro.build/integrations/\n`,
);

// Warn theme author if no 'homepage' property exists inside 'package.json'
if (!homepage) {
logger.warn(
`Add a 'homepage' to your theme's 'package.json'!\tAstro uses this value to populate the integrations page https://astro.build/integrations/\n\n\t"homepage": "https://github.com/UserName/theme-playground",\n`,
);
}
warn(
!homepage,
`Add a 'homepage' to your theme's 'package.json':\n\n\t"homepage": "https://github.com/UserName/theme-playground",\n\nAstro uses this value to populate the integrations page https://astro.build/integrations/\n`,
);

// Warn theme author if no 'repository' property exists inside 'package.json'
if (!repository) {
logger.warn(
`Add a 'repository' to your theme's 'package.json'!\tAstro uses this value to populate the integrations page https://astro.build/integrations/\n\n\t"repository": ${JSON.stringify(
{
type: "git",
url: `https://github.com/UserName/${name}`,
directory: "package",
},
null,
4,
).replace(/\n/g, "\n\t")},\n`,
);
}
warn(
!repository,
`Add a 'repository' to your theme's 'package.json':\n\n\t"repository": ${JSON.stringify(
{
type: "git",
url: `https://github.com/UserName/${name}`,
directory: "package",
},
null,
4,
).replaceAll(
"\n",
"\n\t",
)}\n\nAstro uses this value to populate the integrations page https://astro.build/integrations/\n`,
);

// Warn theme author if package does not have a README
if (!existsSync(resolveFilepath(pkg.path, "README.md", false))) {
warn(
!existsSync(resolveFilepath(pkg.path, "README.md", false)),
`Add a 'README.md' to the root of your theme's package!\n\nNPM uses this file to populate the package page https://www.npmjs.com/package/${name}\n`,
);

if (hasIssues) {
logger.warn(
`Add a 'README.md' to the root of your theme's package!\tNPM uses this file to populate the package page https://www.npmjs.com/package/${name}\n`,
"Is this a private package?\n\n\t'private': true\n\nSet private as true in your theme's 'package.json' to suppress these warnings\n",
);
}
}
Expand Down

0 comments on commit aa826a7

Please sign in to comment.