{"size": 3749, "ext": "js", "max_stars_repo_path": "bin/templates/cordova/parser.js", "max_stars_repo_name": "rharriso/cordova-electron", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "bin/templates/cordova/parser.js", "max_issues_repo_name": "rharriso/cordova-electron", "max_issues_repo_issues_event_min_datetime": "2019-05-31T08:07:37.000Z", "max_issues_repo_issues_event_max_datetime": "2019-05-31T08:07:37.000Z", "max_forks_repo_path": "bin/templates/cordova/parser.js", "max_forks_repo_name": "GedasGa/cordova-electron", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 35.7047619048, "max_line_length": 121, "alphanum_fraction": 0.6529741264}
/*
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
*/

const fs = require('fs-extra');
const path = require('path');
const CordovaError = require('cordova-common').CordovaError;
const events = require('cordova-common').events;
const FileUpdater = require('cordova-common').FileUpdater;

function dirExists (dir) {
    return fs.existsSync(dir) && fs.statSync(dir).isDirectory();
}

class Parser {
    constructor (project) {
        if (!dirExists(project) || !dirExists(path.join(project, 'cordova'))) {
            throw new CordovaError(`The provided path "${project}" is not a valid electron project.`);
        }

        this.path = project;
    }

    update_from_config () {
        return Promise.resolve();
    }

    www_dir () {
        return path.join(this.path, 'www');
    }

    // Used for creating platform_www in projects created by older versions.
    // cordovajs_path (libDir) {
    //     return path.resolve(path.join(libDir, 'cordova-lib', 'cordova.js'));
    // }

    // cordovajs_src_path (libDir) {
    //     return path.resolve(path.join(libDir, 'cordova-js-src'));
    // }

    // Replace the www dir with contents of platform_www and app www.
    update_www (cordovaProject, opts) {
        const platform_www = path.join(this.path, 'platform_www');
        const my_www = this.www_dir();
        // add cordova www and platform_www to sourceDirs
        const sourceDirs = [
            path.relative(cordovaProject.root, cordovaProject.locations.www),
            path.relative(cordovaProject.root, platform_www)
        ];

        // If project contains 'merges' for our platform, use them as another overrides
        const merges_path = path.join(cordovaProject.root, 'merges', 'electron');
        if (fs.existsSync(merges_path)) {
            events.emit('verbose', 'Found "merges/electron" folder. Copying its contents into the electron project.');
            // add merges/electron to sourceDirs
            sourceDirs.push(path.join('merges', 'electron'));
        }

        // targetDir points to electron/www
        const targetDir = path.relative(cordovaProject.root, my_www);
        events.emit('verbose', `Merging and updating files from [${sourceDirs.join(', ')}] to ${targetDir}`);
        FileUpdater.mergeAndUpdateDir(sourceDirs, targetDir, { rootDir: cordovaProject.root }, logFileOp);
    }

    // update_overrides () {}

    config_xml () {
        return path.join(this.path, 'config.xml');
    }

    update_project (cfg) {
        return this.update_from_config()
            .then(
                // Copy munged config.xml to platform www dir
                () => fs.copySync(path.join(this.www_dir(), '..', 'config.xml'), path.join(this.www_dir(), 'config.xml'))
            );
    }
}

module.exports = function (project) {
    return new Parser(project);
};

/**
 * Logs all file operations via the verbose event stream, indented.
 */
function logFileOp (message) {
    events.emit('verbose', `  ${message}`);
}
