File size: 1,705 Bytes
ae01f49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2024, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################

import os
from contextlib import contextmanager
from subprocess import call
from pgadmin.utils import u_encode, fs_encoding, file_quote


# enum-like for tracking whether we have
class JsState:
    NONE = 0
    OLD = 1
    NEW = 2


class JavascriptBundler:
    """Builds Javascript bundle files by delegating to webpack"""

    def __init__(self):
        self.js_state = JsState.NONE

    def bundle(self):
        try:
            try_building_js()
            self.js_state = JsState.NEW
        except OSError:
            webdir_path()
            generated_js_dir = os.path.join(
                webdir_path(), 'pgadmin', 'static', 'js', 'generated')
            if os.path.exists(generated_js_dir) and \
                    os.listdir(generated_js_dir):
                self.js_state = JsState.OLD
            else:
                self.js_state = JsState.NONE

    def report(self):
        return self.js_state


@contextmanager
def pushd(new_dir):
    previous_dir = os.getcwd()
    os.chdir(new_dir)
    yield
    os.chdir(previous_dir)


def webdir_path():
    dirname = os.path.dirname
    this_path = os.path.realpath(u_encode(__file__, fs_encoding))
    return dirname(dirname(dirname(dirname(this_path))))


def try_building_js():
    with pushd(webdir_path()):
        if call(['yarn', 'run', 'bundle:dev']) != 0:
            raise OSError('Error executing bundling the application')