all repos — py-vite @ f45e3de024c6abb079ba4a537e0d03f86070d329

the original vite, written in python

vite/vite.py (view raw)

 1#! /usr/bin/env python3
 2
 3"""
 4Vite - A simple and minimal static site generator.
 5"""
 6
 7import sys
 8import argparse
 9import pathlib
10import os
11import importlib
12
13parser = argparse.ArgumentParser(description="""
14        A simple and minimal static site generator.
15        """)
16parser.add_argument('action', choices=['new', 'build'])
17# TODO: add help for each action
18parser.add_argument('path', nargs='*')
19
20if len(sys.argv) == 1:
21    parser.print_help()
22    sys.exit(1)
23
24try:
25    args = parser.parse_args()
26    project_path = args.path[0]
27except IndexError:
28    parser.print_help()
29    sys.exit(1)
30
31
32def create_project(path):
33    try:
34        abs_path = pathlib.Path(path).resolve()
35        cur_path = pathlib.Path('.').resolve()
36        os.makedirs(os.path.join(path, 'build'))
37        os.mkdir(os.path.join(path, 'pages'))
38        os.mkdir(os.path.join(path, 'templates'))
39        create_config(path)
40        os.symlink(os.path.join(cur_path, 'make.py'),
41                   os.path.join(abs_path, 'make.py'))
42        print('Created project directory at %s.' % (abs_path))
43    except FileExistsError:
44        print('Error: specified path exists')
45
46
47def create_config(path):
48    with open(path + '/config.py', 'w') as f:
49        f.write("""# config.py - Vite's configuration script
50
51title = ''
52author = ''
53header = ''
54footer = ''
55               """)
56
57
58def build_project(path):
59    try:
60        os.chdir(path)
61        importlib.import_module('make')
62    except FileNotFoundError as e:
63        print('Error: no such file or directory: %s' % (path))
64
65
66def main():
67    if args.action == 'new':
68        create_project(project_path)
69    elif args.action == 'build':
70        build_project(project_path)
71
72
73if __name__ == "__main__":
74    main()