Skip to content

Commit 93efe43

Browse files
Merge pull request #44 from SDOS2020/avi
move extra functions to helpers.py
2 parents 9437192 + cce8b10 commit 93efe43

File tree

3 files changed

+82
-51
lines changed

3 files changed

+82
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
1+
# This workflow will installs Python dependencies, and lints the codebase
22
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
33

4-
name: Python package
4+
name: Python Linter
55

66
on:
77
push:
@@ -15,24 +15,20 @@ jobs:
1515
strategy:
1616
matrix:
1717
python-version: [3.6, 3.7, 3.8, 3.9]
18-
1918
steps:
2019
- uses: actions/checkout@v2
21-
- name: Set up Python ${{ matrix.python-version }}
20+
- name: Set up Python ${{ matrix.python-version }} 🛎️
2221
uses: actions/setup-python@v2
2322
with:
2423
python-version: ${{ matrix.python-version }}
25-
- name: Install dependencies
24+
- name: Install dependencies ✔️
2625
run: |
2726
python -m pip install --upgrade pip
2827
pip install flake8 pytest
2928
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
30-
- name: Lint with flake8
29+
- name: Lint with flake8
3130
run: |
3231
# stop the build if there are Python syntax errors or undefined names
3332
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
3433
# exit-zero treats all errors as warnings.
3534
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=79 --statistics
36-
# - name: Test with pytest
37-
# run: |
38-
# pytest

reactonite/helpers.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import json
2+
import os
3+
4+
5+
def create_dir(path):
6+
"""Creates directory at the given path if it doesn't exist.
7+
8+
Parameters
9+
----------
10+
path : str
11+
Path to directory which needs to be created.
12+
13+
Raises
14+
------
15+
RuntimeError
16+
Raised if directory can't be created.
17+
"""
18+
19+
if os.path.isdir(path):
20+
print("{} already exists. Skipping.".format(path))
21+
return
22+
23+
os.makedirs(path)
24+
if not os.path.isdir(path):
25+
raise RuntimeError('Folder can not be created at ' + str(path))
26+
27+
28+
def create_file(path):
29+
"""Creates the file at the given path if it doesn't exist.
30+
31+
Parameters
32+
----------
33+
path : str
34+
Path to file which needs to be created.
35+
36+
Raises
37+
------
38+
RuntimeError
39+
Raised if file can't be created.
40+
"""
41+
42+
open(path, 'w').close()
43+
if not os.path.isfile(path):
44+
raise RuntimeError('File can not be created at ' + str(path))
45+
46+
47+
def write_to_json_file(path, content):
48+
"""Writes content to a json file at the given path. Raises
49+
exception if file not exists.
50+
51+
Parameters
52+
----------
53+
path : str
54+
Path to file where content will be dumped.
55+
content : dict
56+
Dictonary to be dumped into the file.
57+
58+
Raises
59+
------
60+
FileNotFoundError
61+
Raised if file doesn't exist.
62+
"""
63+
64+
if not os.path.isfile(path):
65+
raise FileNotFoundError('File can not be reached at ' + str(path))
66+
67+
with open(path, 'w') as outfile:
68+
json.dump(content, outfile, indent=4, sort_keys=True)

reactonite/main.py

+9-42
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,12 @@
55
import click
66

77
from reactonite.config import DEFAULTS
8+
from reactonite.helpers import create_dir, create_file, write_to_json_file
89
from reactonite.node_wrapper import NodeWrapper
910
from reactonite.transpiler import Transpiler
1011
from reactonite.watcher import ReactoniteWatcher
1112

1213

13-
def create_dir(path):
14-
"""Creates directory at the given path if it doesn't exist.
15-
16-
Parameters
17-
----------
18-
path : str
19-
Path to directory which needs to be created.
20-
21-
Raises
22-
------
23-
RuntimeError
24-
Raised if directory can't be created.
25-
"""
26-
27-
if os.path.isdir(path):
28-
print("{} already exists. Skipping.".format(path))
29-
return
30-
31-
os.makedirs(path)
32-
if not os.path.isdir(path):
33-
raise RuntimeError('Folder can not be created at ' + str(path))
34-
35-
36-
def create_file(path):
37-
"""Creates the file at the given path if it doesn't exist.
38-
39-
Parameters
40-
----------
41-
path : str
42-
Path to file which needs to be created.
43-
44-
Raises
45-
------
46-
RuntimeError
47-
Raised if file can't be created.
48-
"""
49-
50-
open(path, 'w').close()
51-
if not os.path.isfile(path):
52-
raise RuntimeError('File can not be created at ' + str(path))
53-
54-
5514
@click.group()
5615
def cli():
5716
"""Entry point for reactonite cli."""
@@ -112,6 +71,10 @@ def create_project(project_name):
11271
html_file_path = os.path.join(src_dir, DEFAULTS.HTML_FILE_PATH)
11372

11473
config_file_path = os.path.join(project_dir, DEFAULTS.CONFIG_FILE_PATH)
74+
config_settings = {
75+
"src_dir": DEFAULTS.SRC_DIR,
76+
"dest_dir": DEFAULTS.DEST_DIR
77+
}
11578

11679
# Create project directory
11780
create_dir(project_dir)
@@ -123,6 +86,10 @@ def create_project(project_name):
12386

12487
# Create template config.json in project dir
12588
create_file(config_file_path)
89+
write_to_json_file(
90+
config_file_path,
91+
content=config_settings
92+
)
12693

12794
# Create react app
12895
npm = NodeWrapper(project_name, working_dir=project_dir)

0 commit comments

Comments
 (0)