Skip to content

Commit

Permalink
make package_json required to some APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Oct 31, 2024
1 parent 6af4ecb commit 8df8362
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-parrots-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ryanatkn/gro': minor
---

make `package_json` required to some APIs
7 changes: 5 additions & 2 deletions src/lib/gro.config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {gro_plugin_sveltekit_app} from './gro_plugin_sveltekit_app.js';
import {has_sveltekit_app, has_sveltekit_library} from './sveltekit_helpers.js';
import {gro_plugin_gen} from './gro_plugin_gen.js';
import {gro_plugin_moss, has_moss_dep} from './gro_plugin_moss.js';
import {load_package_json} from './package_json.js';

/**
* This is the default config that's passed to `gro.config.ts`
Expand All @@ -16,15 +17,17 @@ import {gro_plugin_moss, has_moss_dep} from './gro_plugin_moss.js';
* - if `src/lib/server/server.ts`, assumes a Node server
*/
const config: Create_Gro_Config = async (cfg) => {
const package_json = load_package_json(); // TODO gets wastefully loaded by some plugins, maybe put in plugin/task context? how does that interact with `map_package_json`?

const [
moss_plugin_result,
has_server_result,
has_sveltekit_library_result,
has_sveltekit_app_result,
] = await Promise.all([
has_moss_dep(),
has_moss_dep(package_json),
has_server(),
has_sveltekit_library(),
has_sveltekit_library(package_json),
has_sveltekit_app(),
]);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/gro_plugin_moss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {has_dep, type Package_Json} from './package_json.js';
export const MOSS_PACKAGE_DEP_NAME = '@ryanatkn/moss';

export const has_moss_dep = (
package_json?: Package_Json,
package_json: Package_Json,
dep_name = MOSS_PACKAGE_DEP_NAME,
): Result<object, {message: string}> => {
if (!has_dep(dep_name, package_json)) {
Expand Down
12 changes: 9 additions & 3 deletions src/lib/gro_plugin_sveltekit_library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ export const gro_plugin_sveltekit_library = ({
svelte_package_options,
svelte_package_cli = SVELTE_PACKAGE_CLI,
}: Gro_Plugin_Sveltekit_Library_Options = {}): Plugin => {
const package_json = load_package_json();

return {
name: 'gro_plugin_sveltekit_library',
setup: async ({dev, log, config}) => {
if (!dev) {
await run_svelte_package(svelte_package_options, svelte_package_cli, log, config.pm_cli);
await run_svelte_package(
package_json,
svelte_package_options,
svelte_package_cli,
log,
config.pm_cli,
);
}
},
adapt: async ({log, timings, config}) => {
const package_json = load_package_json();

// link the CLI binaries if they exist
if (package_json.bin) {
const timing_to_link = timings.start(`${config.pm_cli} link`);
Expand Down
15 changes: 8 additions & 7 deletions src/lib/publish.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export const task: Task<Args> = {
log.info(st('green', 'dry run!'));
}

const has_sveltekit_library_result = has_sveltekit_library();
const package_json = load_package_json();

const has_sveltekit_library_result = has_sveltekit_library(package_json);
if (!has_sveltekit_library_result.ok) {
throw new Task_Error(
'Failed to find SvelteKit library: ' + has_sveltekit_library_result.message,
Expand Down Expand Up @@ -109,11 +111,10 @@ export const task: Task<Args> = {
if (dry) {
log.info('dry run, skipping changeset version');
} else {
const package_json_before = load_package_json();
if (typeof package_json_before.version !== 'string') {
if (typeof package_json.version !== 'string') {
throw new Task_Error('Failed to find package.json version');
}
const parsed_repo_url = parse_repo_url(package_json_before);
const parsed_repo_url = parse_repo_url(package_json);
if (!parsed_repo_url) {
throw new Task_Error(
'package.json `repository` must contain a repo url (and GitHub only for now, sorry),' +
Expand All @@ -139,9 +140,9 @@ export const task: Task<Args> = {
await update_changelog(parsed_repo_url.owner, parsed_repo_url.repo, changelog, token, log);
}

const package_json_after = load_package_json();
version = package_json_after.version!;
if (package_json_before.version === version) {
const package_json_after_versioning = load_package_json();
version = package_json_after_versioning.version!;
if (package_json.version === version) {
// The version didn't change.
// For now this is the best detection we have for a no-op `changeset version`.
if (optional) {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/release.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {z} from 'zod';

import type {Task} from './task.js';
import {has_sveltekit_library, has_sveltekit_app} from './sveltekit_helpers.js';
import {load_package_json} from './package_json.js';

export const Args = z.object({}).strict();
export type Args = z.infer<typeof Args>;
Expand All @@ -10,7 +11,9 @@ export const task: Task<Args> = {
summary: 'publish and deploy',
Args,
run: async ({invoke_task}) => {
const publish = has_sveltekit_library().ok;
const package_json = load_package_json();

const publish = has_sveltekit_library(package_json).ok;
if (publish) {
await invoke_task('publish', {optional: true});
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/sveltekit_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const has_sveltekit_app = (): Result<object, {message: string}> => {
};

export const has_sveltekit_library = (
package_json?: Package_Json,
package_json: Package_Json,
sveltekit_config: Parsed_Sveltekit_Config = default_sveltekit_config,
dep_name = SVELTE_PACKAGE_DEP_NAME,
): Result<object, {message: string}> => {
Expand Down Expand Up @@ -149,12 +149,13 @@ export interface Svelte_Package_Options {
}

export const run_svelte_package = async (
package_json: Package_Json,
options: Svelte_Package_Options | undefined,
cli: string | Cli,
log: Logger,
pm_cli: string,
): Promise<void> => {
const has_sveltekit_library_result = has_sveltekit_library();
const has_sveltekit_library_result = has_sveltekit_library(package_json);
if (!has_sveltekit_library_result.ok) {
throw new Task_Error(
'Failed to find SvelteKit library: ' + has_sveltekit_library_result.message,
Expand Down

0 comments on commit 8df8362

Please sign in to comment.