Repository: pyscript/pyscript
File: pyscriptjs/src/components/pyconfig.ts (TypeScript)

Code before:
        let inlineConfig: object = {};

        // load config from src and inline
        if (this.hasAttribute('src') && this.innerHTML!=='')
        {
            srcConfig = this.extractFromSrc();
            inlineConfig = this.extractFromInline();
            // first make config from src whole if it is partial
            srcConfig = mergeConfig(srcConfig, this.getDefaultConfig());
            // then merge inline config and config from src
            loadedValues = mergeConfig(inlineConfig, srcConfig);
            logger.info('config set from src attribute and inline both, merging', JSON.stringify(loadedValues));
        }
        // load config from src only
        else if (this.hasAttribute('src'))
        {
            srcConfig = this.extractFromSrc();
            logger.info('config set from src attribute, merging with default', srcConfig);
            loadedValues = mergeConfig(srcConfig, this.getDefaultConfig());
        }
        // load config from inline
        else if (this.innerHTML!=='')
        {
            inlineConfig = this.extractFromInline();
            logger.info('config set from inline, merging with default', inlineConfig);
            loadedValues = mergeConfig(inlineConfig, this.getDefaultConfig());
        }
        // load from default if still undefined
        if (Object.keys(loadedValues).length === 0) {
            logger.info('no config set, loading default', defaultConfig);
            loadedValues = this.getDefaultConfig();
        }
        // eslint-disable-next-line
        // @ts-ignore
        this.values = loadedValues;

        appConfig.set(this.values);
        logger.info('config set:', this.values);

        addInitializer(this.loadEnv);
        addInitializer(this.loadPaths);
        this.loadRuntimes();
    }

    log(msg: string) {
        const newLog = document.createElement('p');
        newLog.innerText = msg;
        this.details.appendChild(newLog);
    }

    close() {

Review comment (refactor):
What are the conditions adding here? Feels like we could avoid all these conditions and just have functions return empty configs if they are not defined. So all the code above is reduced to:

```
srcConfig = this.extractFromSrc();
inlineConfig = this.extractFromInline();
// first make config from src whole if it is partial
srcConfig = mergeConfig(srcConfig, this.getDefaultConfig());
// then merge inline config and config from src
loadedValues = mergeConfig(inlineConfig, srcConfig);
```



Code after:
    injectMetadata() {
        this.values.pyscript = {
            "version": version,
            "time": new Date().toISOString()
        };
    }

    connectedCallback() {
        let srcConfig = this.extractFromSrc();
        const inlineConfig = this.extractFromInline();
        // first make config from src whole if it is partial
        srcConfig = mergeConfig(srcConfig, defaultConfig);
        // then merge inline config and config from src
        this.values = mergeConfig(inlineConfig, srcConfig);
        this.injectMetadata();

        appConfig.set(this.values);
        logger.info('config set:', this.values);

        addInitializer(this.loadPackages);
        addInitializer(this.loadPaths);
        this.loadRuntimes();
    }

    log(msg: string) {
        const newLog = document.createElement('p');
        newLog.innerText = msg;
        this.details.appendChild(newLog);
    }

    close() {
        this.remove();
    }

    loadPackages = async () => {
        const env = appConfig_.packages;
        logger.info("Loading env: ", env);
        await runtimeSpec.installPackage(env);
    }

    loadPaths = async () => {
        const paths = appConfig_.paths;
        logger.info("Paths to load: ", paths)
        for (const singleFile of paths) {
            logger.info(`  loading path: ${singleFile}`);
            try {
                await runtimeSpec.loadFromFile(singleFile);
            } catch (e) {
                //Should we still export full error contents to console?
                handleFetchError(<Error>e, singleFile);
            }