Skip to content

Commit be5d1ea

Browse files
authored
Features/17 heroku support (#25)
1 parent 6528e3d commit be5d1ea

File tree

9 files changed

+98
-16
lines changed

9 files changed

+98
-16
lines changed

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ frontend
2828
1. Static assets such as images, fonts and other files can be put at `vendors`
2929

3030
```bash
31+
$ cd frontend
32+
3133
# install dependency packages
3234
$ npm install
3335
# build js, scss files

docs/run_npm_command_at_root.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Run NPM command at root directory
2+
3+
By default, the `package.json` will be placed at the `frontend` directory, which means, you need to run `npm` command under the `frontend` directory.
4+
5+
If you do not like this and want to run `npm` command at the root directory.
6+
7+
Please set `run_npm_command_at_root` to `y` when you create the `webpack` project.
8+
9+
Below files will be placed at the root directory instead of the `frontend` directory.
10+
11+
```
12+
.babelrc
13+
.browserslistrc
14+
.eslintrc
15+
.nvmrc
16+
.stylelintrc.json
17+
package-lock.json
18+
package.json
19+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ nav:
1717
- Setup With Django: setup_with_django.md
1818
- Setup With Flask: setup_with_flask.md
1919
- Frontend workflow: frontend.md
20+
- Run NPM command at root directory: run_npm_command_at_root.md
2021
- Tailwind CSS: setup_with_tailwind.md
2122
- Import React: react.md
2223
- Import Vue: vue.md

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ python = "^3.6"
2424
cookiecutter = "^1.7.0"
2525

2626
[build-system]
27-
requires = ["poetry-core>=1.0.0"]
27+
requires = ["setuptools", "poetry_core>=1.0"]
2828
build-backend = "poetry.core.masonry.api"
2929

3030
[tool.black]
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"project_slug": "frontend"
2+
"project_slug": "frontend",
3+
"run_npm_command_at_root": "n"
34
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os
2+
import os.path
3+
import shutil
4+
5+
TERMINATOR = "\x1b[0m"
6+
WARNING = "\x1b[1;33m [WARNING]: "
7+
INFO = "\x1b[1;33m [INFO]: "
8+
HINT = "\x1b[3;33m"
9+
SUCCESS = "\x1b[1;32m [SUCCESS]: "
10+
11+
DENY_LIST = [".gitignore"]
12+
ALLOW_LIST = ["package.json", "package-lock.json"]
13+
14+
15+
def print_success_msg(msg):
16+
print(SUCCESS + msg + TERMINATOR)
17+
18+
19+
def get_frontend_config_files():
20+
frontend_path = os.getcwd()
21+
22+
for f in os.listdir(frontend_path):
23+
if f.startswith(".") and f not in DENY_LIST:
24+
full_path = os.path.join(frontend_path, f)
25+
yield os.path.dirname(full_path), os.path.basename(full_path)
26+
27+
for f in ALLOW_LIST:
28+
full_path = os.path.join(frontend_path, f)
29+
yield os.path.dirname(full_path), os.path.basename(full_path)
30+
31+
32+
def copy_frontend_config_files():
33+
for dirname, filename in get_frontend_config_files():
34+
old_full_path = os.path.join(dirname, filename)
35+
root_dir = os.path.dirname(dirname)
36+
new_full_path = os.path.join(root_dir, filename)
37+
shutil.copyfile(old_full_path, new_full_path)
38+
39+
os.remove(old_full_path)
40+
41+
42+
def update_webpack_path():
43+
"""
44+
Update webpack config file path in package.json
45+
"""
46+
file_path = "package.json"
47+
with open(file_path, "r+") as f:
48+
file_contents = f.read().replace(
49+
"--config webpack/", "--config {{ cookiecutter.project_slug }}/webpack/"
50+
)
51+
f.seek(0)
52+
f.write(file_contents)
53+
f.truncate()
54+
55+
56+
def main():
57+
if "{{ cookiecutter.run_npm_command_at_root }}".lower() == "y":
58+
update_webpack_path()
59+
copy_frontend_config_files()
60+
61+
print_success_msg(
62+
f"Frontend app '{{ cookiecutter.project_slug }}' "
63+
f"has been created. "
64+
f"To know more, check https://python-webpack-boilerplate.rtfd.io/en/latest/frontend/"
65+
)
66+
67+
68+
if __name__ == "__main__":
69+
main()

webpack_boilerplate/frontend_template/{{cookiecutter.project_slug}}/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webpack_boilerplate/frontend_template/{{cookiecutter.project_slug}}/webpack/webpack.common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const WebpackAssetsManifest = require("webpack-assets-manifest");
66

77
const getEntryObject = () => {
88
const entries = {};
9-
glob.sync("src/application/*.js").forEach((path) => {
9+
glob.sync(Path.join(__dirname, "../src/application/*.js")).forEach((path) => {
1010
const name = Path.basename(path, ".js");
11-
entries[name] = Path.resolve(__dirname, `../${path}`);
11+
entries[name] = path;
1212
});
1313
return entries;
1414
};

webpack_boilerplate/management/commands/webpack_init.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,4 @@ def handle(self, *args, **options):
88
from cookiecutter.main import cookiecutter
99

1010
pkg_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
11-
app_path = cookiecutter(pkg_path, directory="frontend_template")
12-
13-
app_name = os.path.basename(app_path)
14-
15-
self.stdout.write(
16-
self.style.SUCCESS(
17-
f"Frontend app '{app_name}' "
18-
f"has been successfully created. "
19-
f"To know more, check https://python-webpack-boilerplate.rtfd.io/en/latest/frontend/"
20-
)
21-
)
11+
cookiecutter(pkg_path, directory="frontend_template")

0 commit comments

Comments
 (0)