#! /usr/bin/env python3

import os
import re

PREPARED_FILES_REGEX = re.compile(r'(.d.ts)|(.js)$')
DIST_DIR = './dist'


def log_removal(fdesc):
    print('removing {0}'.format(fdesc))


def remove_prepared_files():
    """ Remove files generated for publishing to npm """
    for fdesc in os.listdir('.'):
        if PREPARED_FILES_REGEX.search(fdesc):
            log_removal(fdesc)
            os.remove(fdesc)


def remove_dist_files():
    """ Remove files generated by compilation process """
    for (dirpath, dirnames, filenames) in os.walk(DIST_DIR, topdown=False):
        for filename in filenames:
            fdesc = os.path.join(dirpath, filename)
            log_removal(fdesc)
            os.remove(fdesc)
        for dirname in dirnames:
            fdesc = os.path.join(dirpath, dirname)
            log_removal(fdesc)
            os.rmdir(fdesc)


if __name__ == '__main__':
    remove_prepared_files()
    remove_dist_files()

