# Path: lib/tsconfiglint/TsconfigLinter.py
# @catch_CancelCommand
# def check_tsconfig(view):
#     """ Tests view for being a tsconfig.json.
#         Reads the tsconfig.json and checks for errors.
#         Show Errors using Regions.
#         Set a error map [<(a,b), msg>] into views settings. """
# 
#     if not _is_valid_and_enabled(view):
#         return
# 
#     Debug('tsconfig', 'tsconfig modified, check')
# 
#     linter = TsconfigLinter(view)
# 
#     return linter
# 
# Path: lib/utils/debug.py
# def Debug(classification, text):
#     if 'all' in print_classifications or classification in print_classifications:
#         print("ArcticTypescript: %s: %s" % (classification.ljust(8), text))
#     if classification not in possible_classifications:
#         print("ArcticTypescript: debug: got unknown debug message classification: %s. " \
#             "Consider adding this to possible_classifications" % classification)
#     sys.stdout.flush()
# 
# Path: lib/utils/pathutils.py
# def get_expandglob_path():
#     """ Return path to expandglob.js """
#     return os.path.join(package_path, 'bin', 'expandglob.js')
# 
# def default_node_path(node_path, project=None):
#     if node_path == 'none':
#         Debug('notify', 'The setting node_path is set to "none". That is depreciated. Remove the setting.')
#         node_path = None
# 
#     if node_path == "" or node_path is None:
#         if sys.platform == "linux":
#             return "nodejs"
#         elif sys.platform == "nt":
#             return "node"
#         elif sys.platform == "darwin":
#             return "/usr/local/bin/node"
#         else:
#             return "node"
#     else:
#         return expand_variables(node_path, project)
# 
# Path: lib/utils/osutils.py
# def get_kwargs(stderr=True):
#     if os.name == 'nt':
#         startupinfo = subprocess.STARTUPINFO()
#         startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#         if stderr:
#             #errorlog = open(os.devnull, 'w')
#             return {'stderr':subprocess.DEVNULL, 'startupinfo': startupinfo}
#         return {'startupinfo': startupinfo}
#     else:
#         return {}
# 
# Path: lib/utils/disabling.py
# def set_tsglobexpansion_disabled():
#     """ Sets the disabled flag for tsglobexpansion """
#     if "*tsglob" not in plugin_disabled_for_folders:
#         plugin_disabled_for_folders.append("*tsglob")
# 
# def set_tsglobexpansion_enabled():
#     """ Clears the disabled flag for tsglobexpansion """
#     if "*tsglob" in plugin_disabled_for_folders:
#         plugin_disabled_for_folders.remove("*tsglob")
# 
# def is_tsglobexpansion_disabled():
#     """ Checks the disabled flag for tsglobexpansion """
#     return "*tsglob"  in plugin_disabled_for_folders


import os
import glob
import json
from subprocess import Popen, PIPE, TimeoutExpired
from .TsconfigLinter import check_tsconfig
from ..utils.debug import Debug
from ..utils.pathutils import get_expandglob_path, default_node_path
from ..utils.osutils import get_kwargs
from ..utils.disabling import set_tsglobexpansion_disabled, set_tsglobexpansion_enabled, is_tsglobexpansion_disabled
        This operates on the file contents, so the file should have been saved
        before.
        Returns immediately if not linted or linter is None
        Returns True if the filesGlob has been expanded
        Returns False if there was a linter error, so no expansion has been done
    """

    # Expanding?

    if is_tsglobexpansion_disabled():
        return False

    if linter is None or not linter or not linter.linted:
        return False

    if len(linter.harderrors) > 0:
        return False

    if len(linter.softerrors) != linter.numerrors:
        return False

    if linter.content == "":
        return False

    if "filesGlob" not in linter.tsconfig:
        return True # Should reopen project, so return True here

    # Expand!
    project_dir = os.path.dirname(linter.view.file_name())
    file_list = _expand_globs_with_javascript(project_dir, linter)

# Path: lib/tsconfiglint/TsconfigLinter.py
# @catch_CancelCommand
# def check_tsconfig(view):
#     """ Tests view for being a tsconfig.json.
#         Reads the tsconfig.json and checks for errors.
#         Show Errors using Regions.
#         Set a error map [<(a,b), msg>] into views settings. """
# 
#     if not _is_valid_and_enabled(view):
#         return
# 
#     Debug('tsconfig', 'tsconfig modified, check')
# 
#     linter = TsconfigLinter(view)
# 
#     return linter
# 
# Path: lib/utils/debug.py
# def Debug(classification, text):
#     if 'all' in print_classifications or classification in print_classifications:
#         print("ArcticTypescript: %s: %s" % (classification.ljust(8), text))
#     if classification not in possible_classifications:
#         print("ArcticTypescript: debug: got unknown debug message classification: %s. " \
#             "Consider adding this to possible_classifications" % classification)
#     sys.stdout.flush()
# 
# Path: lib/utils/pathutils.py
# def get_expandglob_path():
#     """ Return path to expandglob.js """
#     return os.path.join(package_path, 'bin', 'expandglob.js')
# 
# def default_node_path(node_path, project=None):
#     if node_path == 'none':
#         Debug('notify', 'The setting node_path is set to "none". That is depreciated. Remove the setting.')
#         node_path = None
# 
#     if node_path == "" or node_path is None:
#         if sys.platform == "linux":
#             return "nodejs"
#         elif sys.platform == "nt":
#             return "node"
#         elif sys.platform == "darwin":
#             return "/usr/local/bin/node"
#         else:
#             return "node"
#     else:
#         return expand_variables(node_path, project)
# 
# Path: lib/utils/osutils.py
# def get_kwargs(stderr=True):
#     if os.name == 'nt':
#         startupinfo = subprocess.STARTUPINFO()
#         startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#         if stderr:
#             #errorlog = open(os.devnull, 'w')
#             return {'stderr':subprocess.DEVNULL, 'startupinfo': startupinfo}
#         return {'startupinfo': startupinfo}
#     else:
#         return {}
# 
# Path: lib/utils/disabling.py
# def set_tsglobexpansion_disabled():
#     """ Sets the disabled flag for tsglobexpansion """
#     if "*tsglob" not in plugin_disabled_for_folders:
#         plugin_disabled_for_folders.append("*tsglob")
# 
# def set_tsglobexpansion_enabled():
#     """ Clears the disabled flag for tsglobexpansion """
#     if "*tsglob" in plugin_disabled_for_folders:
#         plugin_disabled_for_folders.remove("*tsglob")
# 
# def is_tsglobexpansion_disabled():
#     """ Checks the disabled flag for tsglobexpansion """
#     return "*tsglob"  in plugin_disabled_for_folders


# Path: lib/tsconfiglint/tsconfigglobexpand.py
import os
import glob
import json
from subprocess import Popen, PIPE, TimeoutExpired
from .TsconfigLinter import check_tsconfig
from ..utils.debug import Debug
from ..utils.pathutils import get_expandglob_path, default_node_path
from ..utils.osutils import get_kwargs
from ..utils.disabling import set_tsglobexpansion_disabled, set_tsglobexpansion_enabled, is_tsglobexpansion_disabled

        This operates on the file contents, so the file should have been saved
        before.
        Returns immediately if not linted or linter is None
        Returns True if the filesGlob has been expanded
        Returns False if there was a linter error, so no expansion has been done
    """

    # Expanding?

    if is_tsglobexpansion_disabled():
        return False

    if linter is None or not linter or not linter.linted:
        return False

    if len(linter.harderrors) > 0:
        return False

    if len(linter.softerrors) != linter.numerrors:
        return False

    if linter.content == "":
        return False

    if "filesGlob" not in linter.tsconfig:
        return True # Should reopen project, so return True here

    # Expand!
    project_dir = os.path.dirname(linter.view.file_name())
    file_list = _expand_globs_with_javascript(project_dir, linter)

    Debug("tsconfig.json", "fileGlobs expaned")