all repos — py-vite @ b93bd0a4acb34cc47cf1e5f990cc7b81ade8c2d7

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