Repository: gradio-app/gradio
File: gradio/cli/commands/components/_create_utils.py (Python)

Code before:
    frontend = directory / "frontend"
    frontend.mkdir(exist_ok=True)

    p = Path(inspect.getfile(gradio)).parent

    component_source = p / "_frontend_code"
    if not component_source.exists():
        component_source.mkdir(exist_ok=True)
        _download_from_hub(component_source)

    def ignore(_src, names):
        ignored = []
        for n in names:
            if (
                n.startswith("CHANGELOG")
                or n.startswith("README.md")
                or ".test." in n
                or ".stories." in n
                or ".spec." in n
            ):
                ignored.append(n)
        return ignored

    # Replace once we figure out bug with svelte-package
    shutil.copytree(
        str(p / "_frontend_code" / "5.49.1" / component.js_dir),
        frontend,
        dirs_exist_ok=True,
        ignore=ignore,
    )
    source_package_json = json.loads(Path(frontend / "package.json").read_text())
    source_package_json["name"] = package_name
    source_package_json = _modify_js_deps(source_package_json, "dependencies", p)
    source_package_json = _modify_js_deps(source_package_json, "devDependencies", p)
    (frontend / "package.json").write_text(json.dumps(source_package_json, indent=2))
    shutil.copy(
        str(Path(__file__).parent / "files" / "gradio.config.js"),
        frontend / "gradio.config.js",
    )
    shutil.copy(
        str(Path(__file__).parent / "files" / "tsconfig.json"),
        frontend / "tsconfig.json",
    )


def _replace_old_class_name(old_class_name: str, new_class_name: str, content: str):
    pattern = rf"(?<=\b)(?<!\bimport\s)(?<!\.){re.escape(old_class_name)}(?=\b)"
    return re.sub(pattern, new_class_name, content)


def _strip_document_lines(content: str):

Review comment (bug):
Right now I'm hardcoding to the latest version of 5.x gradio. This is because there is a bug in svelte-package that means we can't package our js components and release to npm. So using the latest frontend files would cause an error at install time because any dependencies (say `@gradio/atoms`) can't be installed from npm

Code after:
    frontend = directory / "frontend"
    frontend.mkdir(exist_ok=True)

    p = Path(inspect.getfile(gradio)).parent

    component_source = p / "_frontend_code"
    if not component_source.exists():
        component_source.mkdir(exist_ok=True)
        _download_from_hub(component_source)

    def ignore(_src, names):
        ignored = []
        for n in names:
            if (
                n.startswith("CHANGELOG")
                or n.startswith("README.md")
                or ".test." in n
                or ".stories." in n
                or ".spec." in n
            ):
                ignored.append(n)
        return ignored

    # Replace once we figure out bug with svelte-package
    shutil.copytree(
        str(p / "_frontend_code" / gradio.__version__ / component.js_dir),
        frontend,
        dirs_exist_ok=True,
        ignore=ignore,
    )
    source_package_json = json.loads(Path(frontend / "package.json").read_text())
    source_package_json["name"] = package_name
    source_package_json = _modify_js_deps(source_package_json, "dependencies", p)
    source_package_json = _modify_js_deps(source_package_json, "devDependencies", p)
    (frontend / "package.json").write_text(json.dumps(source_package_json, indent=2))
    shutil.copy(
        str(Path(__file__).parent / "files" / "gradio.config.js"),
        frontend / "gradio.config.js",
    )
    shutil.copy(
        str(Path(__file__).parent / "files" / "tsconfig.json"),
        frontend / "tsconfig.json",
    )


def _replace_old_class_name(old_class_name: str, new_class_name: str, content: str):
    pattern = rf"(?<=\b)(?<!\bimport\s)(?<!\.){re.escape(old_class_name)}(?=\b)"
    return re.sub(pattern, new_class_name, content)


def _strip_document_lines(content: str):