def prepare(force=False, sudo=False):
    with cd_back():
        if not run("which vue", True) and not run("which yarn", True):
            fail("which npm",
                 silent=True,
                 msg="Please install yarn or at least npm")
            print(
                "Yarn not installed! Please install it first with your package-manager.\
                Trying to install it via sudo npm i -g yarn")
            fail("sudo npm i -g yarn")
        if not run(r"yarn global list|grep vue\/cli", True):
            if not sudo:
                run("mkdir -p ~/.yarn-global")
                run("yarn config set prefix ~/.yarn-global")
            fail(f"{'sudo ' if sudo else ''}yarn global add @vue/cli")
        if not run("yarn global list|grep vue-beautify", True):
            if not sudo:
                run("mkdir -p ~/.yarn-global")
                run("yarn config set prefix ~/.yarn-global")
            run(f"{'sudo ' if sudo else ''}yarn global add vue-beautify js-beautify"
                )
        set_yarn_path()
        fail(
            f"vue create -m yarn -n -p default frontend{' -f' if force else ''}"
        )
    with cd_back("frontend/"):
        run("yarn add vuelidate")
        run("yarn add vue-resource")
        replace_in_file(
            "src/main.js",
            "import Vue from 'vue'",
            """\nimport Vuelidate from 'vuelidate'\nVue.use(Vuelidate)\n""",
        )
        replace_in_file(
            "src/main.js",
            "import Vue from 'vue'",
            """\nimport VueResource from 'vue-resource'\nVue.use(VueResource)\n""",
        )
        replace_in_file(
            "package.json",
            "vue-cli-service build",
            r""" && (rm -rf static/frontend/ 2>/dev/null || true) && sed 's/=\\//=\\/static\\/frontend\\//g' dist/index.html > templates/frontend/index.html && mv dist static/frontend""",
        )
        run("touch __init__.py")
        run("mkdir -p templates/frontend")
        run("mkdir -p static/frontend")
        with overwrite(".eslintrc.json", force) as f:
            f.write(ESLINT_CONFIG)
        # with overwrite('webpack.config.js', force) as f:
        #     f.write(WEBPACK_CONFIG)
        with overwrite("templates/index.html", force) as f:
            f.write("""Please run ./manage.py build_frontend""")
        with overwrite("urls.py", force) as f:
            f.write(URLS)
    print(
        "don't forget to add 'frontend' to INSTALLED_APPS and include frontend.urls to your urlpatterns"
    )
    print("next you should run ./manage.py build_frontend")
    print(
        "or 'cd frontend && yarn build && cd .. && ./manage.py collectstatic'")
Exemplo n.º 2
0
    def handle(self, *args, **options):
        prepare(options["force"], options["sudo"])
        from rest_framework.viewsets import ModelViewSet

        for viewset in ModelViewSet.__subclasses__():
            name = viewset().get_serializer_class(
            ).Meta.model._meta.model_name.title()
            for GeneratorClass in [FormGenerator, ListGenerator]:
                generator = GeneratorClass(viewset)
                with overwrite(generator.filename) as f:
                    f.write(generator.render())