diff --git a/.changeset/thirty-buttons-provide.md b/.changeset/thirty-buttons-provide.md new file mode 100644 index 0000000..b9f0fc4 --- /dev/null +++ b/.changeset/thirty-buttons-provide.md @@ -0,0 +1,5 @@ +--- +"astro-theme-provider": patch +--- + +Only emit package warnings on the first start diff --git a/package/src/index.ts b/package/src/index.ts index 1bfee67..f28ced5 100644 --- a/package/src/index.ts +++ b/package/src/index.ts @@ -140,7 +140,7 @@ export default function ( 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); @@ -167,8 +167,7 @@ export default function ( ThemeIntegrationsResolved: "", }; - - if (logLevel) { + if (logLevel && !isRestart) { // Warn about issues with theme's `package.json` warnThemePackage(themePackage, logger); } @@ -398,10 +397,10 @@ export default function ( }, "astro:config:done": ({ injectTypes }) => { injectTypes({ - filename: 'theme.d.ts', + filename: "theme.d.ts", content: themeTypesBuffer, }); - } + }, }, }; diff --git a/package/src/utils/package.ts b/package/src/utils/package.ts index c7eaaa5..dad397d 100644 --- a/package/src/utils/package.ts +++ b/package/src/utils/package.ts @@ -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", ); } }