diff --git a/plugins/post_build.py b/plugins/post_build.py index cfcd372..cffc47f 100644 --- a/plugins/post_build.py +++ b/plugins/post_build.py @@ -1,14 +1,21 @@ from pelican import signals import os +from plugins.utils import run_command OUTPUT_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'output') def post_build(*args, **kwargs): - os.system("mv {0}/assets/robots.txt {0}".format(OUTPUT_PATH)) - os.system("cp -R {0}/assets/* {0}/static".format(OUTPUT_PATH)) - os.system("rm -rf {0}/assets".format(OUTPUT_PATH)) + run_command('Copying Robots.txt', [ + 'mv', os.path.join(OUTPUT_PATH, 'assets', 'robots.txt'), OUTPUT_PATH + ]) + run_command('Copying Assets', [ + 'cp', '-R', os.path.join(OUTPUT_PATH, 'assets', '*'), os.path.join(OUTPUT_PATH, 'static') + ], True) + run_command('Remove Old Assets', [ + 'rm', '-rf', os.path.join(OUTPUT_PATH, 'assets') + ]) def register(): diff --git a/plugins/static_build.py b/plugins/static_build.py index 4e7e339..8606595 100644 --- a/plugins/static_build.py +++ b/plugins/static_build.py @@ -1,6 +1,6 @@ from pelican import signals import os -import subprocess +from plugins.utils import node_bin, run_command import logging logger = logging.getLogger(__file__) @@ -8,30 +8,6 @@ logger = logging.getLogger(__file__) NODE_PRODUCTION = os.environ.get('NODE_ENV') == 'production' -def flatten_list(array): - res = [] - for el in array: - if isinstance(el, (list, tuple)): - res.extend(flatten_list(el)) - continue - res.append(el) - return res - - -def run_command(detail, args, use_system=False): - logger.info(detail + '...') - if use_system: - exit_code = os.system(' '.join(flatten_list(args))) - if exit_code: - exit(exit_code) - else: - subprocess.run(flatten_list(args), check=True) - - -def node_bin(exec): - return os.path.join('node_modules', '.bin', exec) - - def static_build(*args, **kwargs): if NODE_PRODUCTION: logger.info('Building Production...') diff --git a/plugins/utils.py b/plugins/utils.py new file mode 100644 index 0000000..11e3c6f --- /dev/null +++ b/plugins/utils.py @@ -0,0 +1,28 @@ +import subprocess +import logging +import os + + +logger = logging.getLogger(__file__) + + +def flatten_list(array): + res = [] + for el in array: + if isinstance(el, (list, tuple)): + res.extend(flatten_list(el)) + continue + res.append(el) + return res + + +def run_command(detail, args, wrap=False): + if wrap: + run_command('', ['bash', '-c', ' '.join(flatten_list(args))]) + else: + logger.info(detail + '...') + subprocess.run(flatten_list(args), check=True) + + +def node_bin(exec): + return os.path.join('node_modules', '.bin', exec)