<commit_before>import os
import json
import shutil
import commentjson
from django.core.management.base import BaseCommand
from django.apps import apps as django_apps
from fiduswriter.settings import PROJECT_PATH


def deep_merge_dicts(old_dict, merge_dict):
    for key in merge_dict:
        if key in old_dict:
            if (
                isinstance(old_dict[key], dict) and
                isinstance(merge_dict[key], dict)
            ):
                deep_merge_dicts(old_dict[key], merge_dict[key])
            else:
                old_dict[key] = merge_dict[key]
        else:
            old_dict[key] = merge_dict[key]


class Command(BaseCommand):
    help = 'Join package.json files from apps into common package.json'

    def handle(self, *args, **options):
        shutil.os.chdir(PROJECT_PATH)
        package = {}
        configs = django_apps.get_app_configs()
        for config in configs:
            package_path = os.path.join(config.path, 'package.json')
            try:
                with open(package_path) as data_file:
                    data = commentjson.load(data_file)
            except IOError:
                continue
            deep_merge_dicts(package, data)
        with open('package.json', 'w') as outfile:
            json.dump(package, outfile)<commit_msg>allow plugin additions in package.json scripts section<commit_after>import os
import json
import shutil
import commentjson
from django.core.management.base import BaseCommand
from django.apps import apps as django_apps
from fiduswriter.settings import PROJECT_PATH


def deep_merge_dicts(old_dict, merge_dict, scripts = False):
    for key in merge_dict:
        if key in old_dict:
            if (
                isinstance(old_dict[key], dict) and
                isinstance(merge_dict[key], dict)
            ):
                if key == 'scripts':
                    deep_merge_dicts(old_dict[key], merge_dict[key], True)
                else:
                    deep_merge_dicts(old_dict[key], merge_dict[key])
            else:
                # In the scripts section, allow adding to hooks such as
                # "preinstall" and "postinstall"
                if scripts and key in old_dict:
                    old_dict[key] += ' & %s' % merge_dict[key]
                else:
                    old_dict[key] = merge_dict[key]
        else:
            old_dict[key] = merge_dict[key]


class Command(BaseCommand):
    help = 'Join package.json files from apps into common package.json'

    def handle(self, *args, **options):
        shutil.os.chdir(PROJECT_PATH)
        package = {}
        configs = django_apps.get_app_configs()
        for config in configs:
            package_path = os.path.join(config.path, 'package.json')
            try:
                with open(package_path) as data_file:
                    data = commentjson.load(data_file)
            except IOError:
                continue
            deep_merge_dicts(package, data)
        with open('package.json', 'w') as outfile:
            json.dump(package, outfile)
{'fraction_non_alphanumeric': 0.08289986996098829, 'fraction_numerical': 0.0, 'mean_word_length': 2.0107632093933465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3010012', 'n_tokens_mistral': 873, 'n_tokens_neox': 848, 'n_words': 221}