pc.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """Pynecone CLI to create, run, and deploy apps."""
  2. import os
  3. import requests
  4. import typer
  5. from pynecone import constants, utils
  6. from pynecone.compiler import templates
  7. # Create the app.
  8. cli = typer.Typer()
  9. @cli.command()
  10. def version():
  11. """Get the Pynecone version."""
  12. utils.console.print(constants.VERSION)
  13. @cli.command()
  14. def init():
  15. """Initialize a new Pynecone app."""
  16. app_name = utils.get_default_app_name()
  17. with utils.console.status(f"[bold]Initializing {app_name}") as status:
  18. # Only create the app directory if it doesn't exist.
  19. if not os.path.exists(constants.CONFIG_FILE):
  20. # Create a configuration file.
  21. with open(constants.CONFIG_FILE, "w") as f:
  22. f.write(templates.PCCONFIG.format(app_name=app_name))
  23. utils.console.log(f"Initialize the app directory.")
  24. # Initialize the app directory.
  25. utils.cp(constants.APP_TEMPLATE_DIR, app_name)
  26. utils.mv(
  27. os.path.join(app_name, constants.APP_TEMPLATE_FILE),
  28. os.path.join(app_name, app_name + constants.PY_EXT),
  29. )
  30. utils.cp(constants.ASSETS_TEMPLATE_DIR, constants.APP_ASSETS_DIR)
  31. # Install bun if it isn't already installed.
  32. if not os.path.exists(utils.get_bun_path()):
  33. utils.console.log(f"Installing bun...")
  34. os.system(constants.INSTALL_BUN)
  35. # Initialize the web directory.
  36. utils.console.log(f"Initializing the web directory.")
  37. utils.rm(os.path.join(constants.WEB_TEMPLATE_DIR, constants.NODE_MODULES))
  38. utils.rm(os.path.join(constants.WEB_TEMPLATE_DIR, constants.PACKAGE_LOCK))
  39. utils.cp(constants.WEB_TEMPLATE_DIR, constants.WEB_DIR)
  40. utils.console.log(f"[bold green]Finished Initializing: {app_name}")
  41. @cli.command()
  42. def run(
  43. env: constants.Env = constants.Env.DEV,
  44. frontend: bool = True,
  45. backend: bool = True,
  46. ):
  47. """Run the app.
  48. Args:
  49. env: The environment to run the app in.
  50. frontend: Whether to run the frontend.
  51. backend: Whether to run the backend.
  52. """
  53. utils.console.rule("[bold]Starting Pynecone App")
  54. app = utils.get_app()
  55. frontend_cmd = backend_cmd = None
  56. if env == constants.Env.DEV:
  57. frontend_cmd, backend_cmd = utils.run_frontend, utils.run_backend
  58. if env == constants.Env.PROD:
  59. frontend_cmd, backend_cmd = utils.run_frontend_prod, utils.run_backend_prod
  60. assert frontend_cmd and backend_cmd, "Invalid env"
  61. if frontend:
  62. frontend_cmd(app)
  63. if backend:
  64. backend_cmd(app)
  65. @cli.command()
  66. def deploy(dry_run: bool = False):
  67. """Deploy the app to the hosting service.
  68. Args:
  69. dry_run: Whether to run a dry run.
  70. """
  71. # Get the app config.
  72. config = utils.get_config()
  73. config.api_url = utils.get_production_backend_url()
  74. # Check if the deploy url is set.
  75. if config.deploy_url is None:
  76. typer.echo("This feature is coming soon!")
  77. return
  78. # Compile the app in production mode.
  79. typer.echo("Compiling production app")
  80. app = utils.get_app()
  81. utils.export_app(app)
  82. # Exit early if this is a dry run.
  83. if dry_run:
  84. return
  85. # Deploy the app.
  86. data = {"userId": config.username, "projectId": config.app_name}
  87. original_response = requests.get(config.deploy_url, params=data)
  88. response = original_response.json()
  89. print("response", response)
  90. frontend = response["frontend_resources_url"]
  91. backend = response["backend_resources_url"]
  92. # Upload the frontend and backend.
  93. with open(constants.FRONTEND_ZIP, "rb") as f:
  94. response = requests.put(frontend, data=f)
  95. with open(constants.BACKEND_ZIP, "rb") as f:
  96. response = requests.put(backend, data=f)
  97. main = cli
  98. if __name__ == "__main__":
  99. main()