Home / Function/ updatePackageJsonScripts() — astro Function Reference

updatePackageJsonScripts() — astro Function Reference

Architecture documentation for the updatePackageJsonScripts() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  f3e6969a_3f04_43b5_609b_fdfd7fa60748["updatePackageJsonScripts()"]
  9151bb3d_ee1e_da42_752a_45a9db1dd918["index.ts"]
  f3e6969a_3f04_43b5_609b_fdfd7fa60748 -->|defined in| 9151bb3d_ee1e_da42_752a_45a9db1dd918
  e251add5_ea46_2280_c246_1b5a023acc3b["add()"]
  e251add5_ea46_2280_c246_1b5a023acc3b -->|calls| f3e6969a_3f04_43b5_609b_fdfd7fa60748
  6bba8e7c_bf8b_374e_8b24_b01c2bdb5bd4["getDiffContent()"]
  f3e6969a_3f04_43b5_609b_fdfd7fa60748 -->|calls| 6bba8e7c_bf8b_374e_8b24_b01c2bdb5bd4
  2ee262d1_8614_9c2d_75fd_5bb9c01ac63c["askToContinue()"]
  f3e6969a_3f04_43b5_609b_fdfd7fa60748 -->|calls| 2ee262d1_8614_9c2d_75fd_5bb9c01ac63c
  style f3e6969a_3f04_43b5_609b_fdfd7fa60748 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/cli/add/index.ts lines 719–779

async function updatePackageJsonScripts({
	configURL,
	flags,
	logger,
	scripts,
}: {
	configURL: URL;
	flags: Flags;
	logger: Logger;
	scripts: Record<string, string>;
}): Promise<UpdateResult> {
	const pkgURL = new URL('./package.json', configURL);
	if (!existsSync(pkgURL)) {
		logger.debug('add', 'No package.json found, skipping scripts update');
		return UpdateResult.none;
	}

	const pkgPath = fileURLToPath(pkgURL);
	const input = await fs.readFile(pkgPath, { encoding: 'utf-8' });
	const pkgJson = JSON.parse(input);

	pkgJson.scripts ??= {};
	let hasChanges = false;
	for (const [name, command] of Object.entries(scripts)) {
		if (!(name in pkgJson.scripts)) {
			pkgJson.scripts[name] = command;
			hasChanges = true;
		}
	}

	if (!hasChanges) {
		return UpdateResult.none;
	}

	const output = JSON.stringify(pkgJson, null, 2);
	const diff = getDiffContent(input, output);

	if (!diff) {
		return UpdateResult.none;
	}

	const message = `\n${boxen(diff, {
		margin: 0.5,
		padding: 0.5,
		borderStyle: 'round',
		title: 'package.json',
	})}\n`;

	logger.info(
		'SKIP_FORMAT',
		`\n  ${magenta('Astro will add the following scripts to your package.json:')}\n${message}`,
	);

	if (await askToContinue({ flags, logger })) {
		await fs.writeFile(pkgPath, output, { encoding: 'utf-8' });
		logger.debug('add', 'Updated package.json scripts');
		return UpdateResult.updated;
	} else {
		return UpdateResult.cancelled;
	}
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does updatePackageJsonScripts() do?
updatePackageJsonScripts() is a function in the astro codebase, defined in packages/astro/src/cli/add/index.ts.
Where is updatePackageJsonScripts() defined?
updatePackageJsonScripts() is defined in packages/astro/src/cli/add/index.ts at line 719.
What does updatePackageJsonScripts() call?
updatePackageJsonScripts() calls 2 function(s): askToContinue, getDiffContent.
What calls updatePackageJsonScripts()?
updatePackageJsonScripts() is called by 1 function(s): add.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free