MCPcopy Create free account
hub / github.com/NomicFoundation/hardhat / copyProjectFiles

Function copyProjectFiles

packages/hardhat/src/internal/cli/init/init.ts:537–604  ·  view source on GitHub ↗
(
  workspace: string,
  template: Template,
  force?: boolean,
)

Source from the content-addressed store, hash-verified

535 * @param force Whether to overwrite existing files in the workspace.
536 */
537export async function copyProjectFiles(
538 workspace: string,
539 template: Template,
540 force?: boolean,
541): Promise<void> {
542 // Find all the paths in the template that clash with an existing one in the
543 // workspace
544 const matchingRelativeWorkspacePaths = (
545 await Promise.all(
546 template.files.map(async (relativeTemplatePath) => {
547 const relativeWorkspacePath =
548 relativeTemplateToWorkspacePath(relativeTemplatePath);
549
550 const absoluteWorkspacePath = path.join(
551 workspace,
552 relativeWorkspacePath,
553 );
554
555 if (!(await exists(absoluteWorkspacePath))) {
556 return undefined;
557 }
558
559 // We ignore directories in this clash detection
560 if (await isDirectory(absoluteWorkspacePath)) {
561 return undefined;
562 }
563
564 return relativeWorkspacePath;
565 }),
566 )
567 ).filter((relativeWorkspacePath) => relativeWorkspacePath !== undefined);
568
569 const matchingRelativeWorkspacePathsSet = new Set(
570 matchingRelativeWorkspacePaths,
571 );
572
573 // Ask the user for permission to overwrite existing files if needed
574 if (matchingRelativeWorkspacePaths.length !== 0) {
575 if (force === undefined) {
576 force = await promptForForce(matchingRelativeWorkspacePaths);
577 }
578 }
579
580 // Copy the template files to the workspace
581 for (const relativeTemplatePath of template.files) {
582 const relativeWorkspacePath =
583 relativeTemplateToWorkspacePath(relativeTemplatePath);
584
585 if (
586 force === false &&
587 matchingRelativeWorkspacePathsSet.has(relativeWorkspacePath)
588 ) {
589 continue;
590 }
591
592 const absoluteTemplatePath = path.join(template.path, relativeTemplatePath);
593 const absoluteWorkspacePath = path.join(workspace, relativeWorkspacePath);
594

Callers 2

init.tsFile · 0.85
initHardhatFunction · 0.85

Calls 9

existsFunction · 0.90
isDirectoryFunction · 0.90
copyFunction · 0.90
promptForForceFunction · 0.85
installSkillsFunction · 0.85
createClaudeMdFunction · 0.85
createDotClaudeFunction · 0.85
filterMethod · 0.80

Tested by

no test coverage detected