<commit_before>/**
 * Copyright 2013-2018 the original author or authors from the JHipster project.
 *
 * This file is part of the JHipster project, see https://www.jhipster.tech/
 * for more information.
 *
 * Licensed 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 crypto = require('crypto');
const chalk = require('chalk');

const constants = require('../generator-constants');

module.exports = {
    askForModuleName,
    askForServerSideOpts,
    askForOptionalItems,
    askFori18n
};

function askForModuleName() {
    if (this.baseName) return;

    this.askModuleName(this);
}

function askForServerSideOpts(meta) {
    if (!meta && this.existingProject) return;

    const applicationType = this.applicationType;
    let defaultPort = applicationType === 'gateway' ? '8080' : '8081';
    if (applicationType === 'uaa') {
        defaultPort = '9999';
    }
    const prompts = [
        {
            when: response => (applicationType === 'gateway' || applicationType === 'microservice' || applicationType === 'uaa'),
            type: 'input',
            name: 'serverPort',
            validate: input => (/^([0-9]*)$/.test(input) ? true : 'This is not a valid port number.'),
            message: 'As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts.',
            default: defaultPort
        },
        {
            type: 'input',
            name: 'packageName',
            validate: input => (/^([a-z_]{1}[a-z0-9_]*(\.[a-z_]{1}[a-z0-9_]*)*)$/.test(input) ?
                true : 'The package name you have provided is not a valid Java package name.'),
            message: 'What is your default Java package name?',
            default: 'com.mycompany.myapp',
            store: true
        },
        {
            when: response => applicationType === 'gateway' || applicationType === 'microservice' || applicationType === 'uaa',
            type: 'list',
            name: 'serviceDiscoveryType',
            message: 'Which service discovery server do you want to use?',
            choices: [
                {
                    value: 'eureka',
                    name: 'JHipster Registry (uses Eureka, provides Spring Cloud Config support and monitoring dashboards)'
                },
                {
                    value: 'consul',
                    name: 'Consul'
                },
                {
                    value: false,
                    name: 'No service discovery'
                }
            ],
            default: 'eureka'
        },
        {
            when: response => applicationType === 'monolith',
            type: 'list',
            name: 'serviceDiscoveryType',
            message: 'Do you want to use the JHipster Registry to configure, monitor and scale your application?',
            choices: [
                {
                    value: false,
                    name: 'No'
                },
                {
                    value: 'eureka',
                    name: 'Yes'
                }
            ],
            default: false
        },
        {
            when: response => (
                (applicationType === 'monolith' && response.serviceDiscoveryType !== 'eureka') ||
                ['gateway', 'microservice'].includes(applicationType)
            ),
            type: 'list',
            name: 'authenticationType',
            message: `Which ${chalk.yellow('*type*')} of authentication would you like to use?`,
            choices: (response) => {
                const opts = [
                    {
                        value: 'jwt',
                        name: 'JWT authentication (stateless, with a token)'
                    },
                    {
                        value: 'oauth2',
                        name: 'OAuth 2.0 / OIDC Authentication (stateful, works with Keycloak and Okta)'
                    }
                ];
                if (applicationType === 'monolith' && response.serviceDiscoveryType !== 'eureka') {
                    opts.push({
                        value: 'session',
                        name: 'HTTP Session Authentication (stateful, default Spring Security mechanism)'
                    });
                } else if (['gateway', 'microservice'].includes(applicationType)) {
                    opts.push({
                        value: 'uaa',
                        name: 'Authentication with JHipster UAA server (the server must be generated separately)'
                    });
                }
                return opts;
            },
            default: 0
        },
        {
            when: response => ((applicationType === 'gateway' || applicationType === 'microservice') && response.authenticationType === 'uaa'),
            type: 'input',
            name: 'uaaBaseName',
            message: 'What is the folder path of your UAA application?',
            default: '../uaa',
            validate: (input) => {
                const uaaAppData = this.getUaaAppName(input);

                if (uaaAppData && uaaAppData.baseName && uaaAppData.applicationType === 'uaa') {
                    return true;
                }
                return `Could not find a valid JHipster UAA server in path "${input}"`;
            }
        },
        {
            type: 'list',
            name: 'databaseType',
            message: `Which ${chalk.yellow('*type*')} of database would you like to use?`,
            choices: (response) => {
                const opts = [
                    {
                        value: 'sql',
                        name: 'SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL)'
                    },
                    {
                        value: 'mongodb',
                        name: 'MongoDB'
                    },
                    {
                        value: 'cassandra',
                        name: 'Cassandra'
                    },
                    {
                        value: 'couchbase',
                        name: '[BETA] Couchbase'
                    }
                ];
                if (
                    applicationType === 'microservice' || ((response.authenticationType === 'uaa' ||
                    response.authenticationType === 'oauth2') && applicationType === 'gateway')
                ) {
                    opts.push({
                        value: 'no',
                        name: 'No database'
                    });
                }
                return opts;
            },
            default: 0
        },
        {
            when: response => response.databaseType === 'sql',
            type: 'list',
            name: 'prodDatabaseType',
            message: `Which ${chalk.yellow('*production*')} database would you like to use?`,
            choices: constants.SQL_DB_OPTIONS,
            default: 0
        },
        {
            when: response => response.databaseType === 'sql',
            type: 'list',
            name: 'devDatabaseType',
            message: `Which ${chalk.yellow('*development*')} database would you like to use?`,
            choices: response => [
                {
                    value: 'h2Disk',
                    name: 'H2 with disk-based persistence'
                },
                {
                    value: 'h2Memory',
                    name: 'H2 with in-memory persistence'
                }
            ].concat(constants.SQL_DB_OPTIONS.find(it => it.value === response.prodDatabaseType)),
            default: 0
        },
        {
            // cache is mandatory for gateway and defined later to 'hazelcast' value
            when: response => applicationType !== 'gateway',
            type: 'list',
            name: 'cacheProvider',
            message: 'Do you want to use the Spring cache abstraction?',
            choices: [
                {
                    value: 'ehcache',
                    name: 'Yes, with the Ehcache implementation (local cache, for a single node)'
                },
                {
                    value: 'hazelcast',
                    name: 'Yes, with the Hazelcast implementation (distributed cache, for multiple nodes)'
                },
                {
                    value: 'infinispan',
                    name: '[BETA] Yes, with the Infinispan (hybrid cache, for multiple nodes)'
                },
                {
                    value: 'no',
                    name: 'No (when using an SQL database, this will also disable the Hibernate L2 cache)'
                }
            ],
            default: (applicationType === 'microservice' || applicationType === 'uaa') ? 1 : 0
        },
        {
            when: response => ((response.cacheProvider !== 'no' || applicationType === 'gateway') && response.databaseType === 'sql'),
            type: 'confirm',
            name: 'enableHibernateCache',
            message: 'Do you want to use Hibernate 2nd level cache?',
            default: true
        },
        {
            type: 'list',
            name: 'buildTool',
            message: 'Would you like to use Maven or Gradle for building the backend?',
            choices: [
                {
                    value: 'maven',
                    name: 'Maven'
                },
                {
                    value: 'gradle',
                    name: 'Gradle'
                }
            ],
            default: 'maven'
        }
    ];

    if (meta) return prompts; // eslint-disable-line consistent-return

    const done = this.async();

    this.prompt(prompts).then((props) => {
        this.serviceDiscoveryType = props.serviceDiscoveryType;
        this.authenticationType = props.authenticationType;

        // JWT authentication is mandatory with Eureka, so the JHipster Registry
        // can control the applications
        if (this.serviceDiscoveryType === 'eureka' && this.authenticationType !== 'uaa' && this.authenticationType !== 'oauth2') {
            this.authenticationType = 'jwt';
        }

        if (this.authenticationType === 'session') {
            this.rememberMeKey = crypto.randomBytes(20).toString('hex');
        }

        if (this.authenticationType === 'jwt' || this.applicationType === 'microservice') {
            this.jwtSecretKey = crypto.randomBytes(20).toString('hex');
        }

        // user-management will be handled by UAA app, oauth expects users to be managed in IpP
        if ((this.applicationType === 'gateway' && this.authenticationType === 'uaa') || this.authenticationType === 'oauth2') {
            this.skipUserManagement = true;
        }

        if (this.applicationType === 'uaa') {
            this.authenticationType = 'uaa';
        }

        this.packageName = props.packageName;
        this.serverPort = props.serverPort;
        if (this.serverPort === undefined) {
            this.serverPort = '8080';
        }
        this.cacheProvider = props.cacheProvider;
        this.enableHibernateCache = props.enableHibernateCache;
        this.databaseType = props.databaseType;
        this.devDatabaseType = props.devDatabaseType;
        this.prodDatabaseType = props.prodDatabaseType;
        this.searchEngine = props.searchEngine;
        this.buildTool = props.buildTool;
        this.uaaBaseName = this.getUaaAppName(props.uaaBaseName).baseName;

        if (this.databaseType === 'no') {
            this.devDatabaseType = 'no';
            this.prodDatabaseType = 'no';
            this.enableHibernateCache = false;
        } else if (this.databaseType === 'mongodb') {
            this.devDatabaseType = 'mongodb';
            this.prodDatabaseType = 'mongodb';
            this.enableHibernateCache = false;
        } else if (this.databaseType === 'couchbase') {
            this.devDatabaseType = 'couchbase';
            this.prodDatabaseType = 'couchbase';
            this.enableHibernateCache = false;
        } else if (this.databaseType === 'cassandra') {
            this.devDatabaseType = 'cassandra';
            this.prodDatabaseType = 'cassandra';
            this.enableHibernateCache = false;
        }
        // Hazelcast is mandatory for Gateways, as it is used for rate limiting
        if (this.applicationType === 'gateway') {
            this.cacheProvider = 'hazelcast';
        }
        done();
    });
}

function askForOptionalItems(meta) {
    if (!meta && this.existingProject) return;

    const applicationType = this.applicationType;
    const choices = [];
    const defaultChoice = [];
    choices.push({
        name: 'Reactive APIs, using Spring Webflux',
        value: 'reactive:true'
    });
    if (this.databaseType === 'sql' || this.databaseType === 'mongodb') {
        choices.push({
            name: 'Search engine using Elasticsearch',
            value: 'searchEngine:elasticsearch'
        });
    }
    if (applicationType === 'monolith' || applicationType === 'gateway') {
        choices.push({
            name: 'WebSockets using Spring Websocket',
            value: 'websocket:spring-websocket'
        });
    }
    choices.push({
        name: 'API first development using swagger-codegen',
        value: 'enableSwaggerCodegen:true'
    });
    choices.push({
        name: 'Asynchronous messages using Apache Kafka',
        value: 'messageBroker:kafka'
    });

    const PROMPTS = {
        type: 'checkbox',
        name: 'serverSideOptions',
        message: 'Which other technologies would you like to use?',
        choices,
        default: defaultChoice
    };

    if (meta) return PROMPTS; // eslint-disable-line consistent-return

    const done = this.async();
    if (choices.length > 0) {
        this.prompt(PROMPTS).then((prompt) => {
            this.serverSideOptions = prompt.serverSideOptions;
            this.reactive = this.getOptionFromArray(this.serverSideOptions, 'reactive');
            this.websocket = this.getOptionFromArray(this.serverSideOptions, 'websocket');
            this.searchEngine = this.getOptionFromArray(this.serverSideOptions, 'searchEngine');
            this.messageBroker = this.getOptionFromArray(this.serverSideOptions, 'messageBroker');
            this.enableSwaggerCodegen = this.getOptionFromArray(this.serverSideOptions, 'enableSwaggerCodegen');
            // Only set this option if it hasn't been set in a previous question, as it's only optional for monoliths
            if (!this.serviceDiscoveryType) {
                this.serviceDiscoveryType = this.getOptionFromArray(this.serverSideOptions, 'serviceDiscoveryType');
            }
            done();
        });
    } else {
        done();
    }
}

function askFori18n() {
    if (this.existingProject || this.configOptions.skipI18nQuestion) return;

    this.aski18n(this);
}
<commit_msg>Only allow db-less gateways when JWT is used<commit_after>/**
 * Copyright 2013-2018 the original author or authors from the JHipster project.
 *
 * This file is part of the JHipster project, see https://www.jhipster.tech/
 * for more information.
 *
 * Licensed 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 crypto = require('crypto');
const chalk = require('chalk');

const constants = require('../generator-constants');

module.exports = {
    askForModuleName,
    askForServerSideOpts,
    askForOptionalItems,
    askFori18n
};

function askForModuleName() {
    if (this.baseName) return;

    this.askModuleName(this);
}

function askForServerSideOpts(meta) {
    if (!meta && this.existingProject) return;

    const applicationType = this.applicationType;
    let defaultPort = applicationType === 'gateway' ? '8080' : '8081';
    if (applicationType === 'uaa') {
        defaultPort = '9999';
    }
    const prompts = [
        {
            when: response => (applicationType === 'gateway' || applicationType === 'microservice' || applicationType === 'uaa'),
            type: 'input',
            name: 'serverPort',
            validate: input => (/^([0-9]*)$/.test(input) ? true : 'This is not a valid port number.'),
            message: 'As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts.',
            default: defaultPort
        },
        {
            type: 'input',
            name: 'packageName',
            validate: input => (/^([a-z_]{1}[a-z0-9_]*(\.[a-z_]{1}[a-z0-9_]*)*)$/.test(input) ?
                true : 'The package name you have provided is not a valid Java package name.'),
            message: 'What is your default Java package name?',
            default: 'com.mycompany.myapp',
            store: true
        },
        {
            when: response => applicationType === 'gateway' || applicationType === 'microservice' || applicationType === 'uaa',
            type: 'list',
            name: 'serviceDiscoveryType',
            message: 'Which service discovery server do you want to use?',
            choices: [
                {
                    value: 'eureka',
                    name: 'JHipster Registry (uses Eureka, provides Spring Cloud Config support and monitoring dashboards)'
                },
                {
                    value: 'consul',
                    name: 'Consul'
                },
                {
                    value: false,
                    name: 'No service discovery'
                }
            ],
            default: 'eureka'
        },
        {
            when: response => applicationType === 'monolith',
            type: 'list',
            name: 'serviceDiscoveryType',
            message: 'Do you want to use the JHipster Registry to configure, monitor and scale your application?',
            choices: [
                {
                    value: false,
                    name: 'No'
                },
                {
                    value: 'eureka',
                    name: 'Yes'
                }
            ],
            default: false
        },
        {
            when: response => (
                (applicationType === 'monolith' && response.serviceDiscoveryType !== 'eureka') ||
                ['gateway', 'microservice'].includes(applicationType)
            ),
            type: 'list',
            name: 'authenticationType',
            message: `Which ${chalk.yellow('*type*')} of authentication would you like to use?`,
            choices: (response) => {
                const opts = [
                    {
                        value: 'jwt',
                        name: 'JWT authentication (stateless, with a token)'
                    },
                    {
                        value: 'oauth2',
                        name: 'OAuth 2.0 / OIDC Authentication (stateful, works with Keycloak and Okta)'
                    }
                ];
                if (applicationType === 'monolith' && response.serviceDiscoveryType !== 'eureka') {
                    opts.push({
                        value: 'session',
                        name: 'HTTP Session Authentication (stateful, default Spring Security mechanism)'
                    });
                } else if (['gateway', 'microservice'].includes(applicationType)) {
                    opts.push({
                        value: 'uaa',
                        name: 'Authentication with JHipster UAA server (the server must be generated separately)'
                    });
                }
                return opts;
            },
            default: 0
        },
        {
            when: response => ((applicationType === 'gateway' || applicationType === 'microservice') && response.authenticationType === 'uaa'),
            type: 'input',
            name: 'uaaBaseName',
            message: 'What is the folder path of your UAA application?',
            default: '../uaa',
            validate: (input) => {
                const uaaAppData = this.getUaaAppName(input);

                if (uaaAppData && uaaAppData.baseName && uaaAppData.applicationType === 'uaa') {
                    return true;
                }
                return `Could not find a valid JHipster UAA server in path "${input}"`;
            }
        },
        {
            type: 'list',
            name: 'databaseType',
            message: `Which ${chalk.yellow('*type*')} of database would you like to use?`,
            choices: (response) => {
                const opts = [
                    {
                        value: 'sql',
                        name: 'SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL)'
                    },
                    {
                        value: 'mongodb',
                        name: 'MongoDB'
                    },
                    {
                        value: 'cassandra',
                        name: 'Cassandra'
                    },
                    {
                        value: 'couchbase',
                        name: '[BETA] Couchbase'
                    }
                ];
                if (
                    applicationType === 'microservice' || (response.authenticationType === 'jwt' && applicationType === 'gateway')
                ) {
                    opts.push({
                        value: 'no',
                        name: 'No database'
                    });
                }
                return opts;
            },
            default: 0
        },
        {
            when: response => response.databaseType === 'sql',
            type: 'list',
            name: 'prodDatabaseType',
            message: `Which ${chalk.yellow('*production*')} database would you like to use?`,
            choices: constants.SQL_DB_OPTIONS,
            default: 0
        },
        {
            when: response => response.databaseType === 'sql',
            type: 'list',
            name: 'devDatabaseType',
            message: `Which ${chalk.yellow('*development*')} database would you like to use?`,
            choices: response => [
                {
                    value: 'h2Disk',
                    name: 'H2 with disk-based persistence'
                },
                {
                    value: 'h2Memory',
                    name: 'H2 with in-memory persistence'
                }
            ].concat(constants.SQL_DB_OPTIONS.find(it => it.value === response.prodDatabaseType)),
            default: 0
        },
        {
            // cache is mandatory for gateway and defined later to 'hazelcast' value
            when: response => applicationType !== 'gateway',
            type: 'list',
            name: 'cacheProvider',
            message: 'Do you want to use the Spring cache abstraction?',
            choices: [
                {
                    value: 'ehcache',
                    name: 'Yes, with the Ehcache implementation (local cache, for a single node)'
                },
                {
                    value: 'hazelcast',
                    name: 'Yes, with the Hazelcast implementation (distributed cache, for multiple nodes)'
                },
                {
                    value: 'infinispan',
                    name: '[BETA] Yes, with the Infinispan (hybrid cache, for multiple nodes)'
                },
                {
                    value: 'no',
                    name: 'No (when using an SQL database, this will also disable the Hibernate L2 cache)'
                }
            ],
            default: (applicationType === 'microservice' || applicationType === 'uaa') ? 1 : 0
        },
        {
            when: response => ((response.cacheProvider !== 'no' || applicationType === 'gateway') && response.databaseType === 'sql'),
            type: 'confirm',
            name: 'enableHibernateCache',
            message: 'Do you want to use Hibernate 2nd level cache?',
            default: true
        },
        {
            type: 'list',
            name: 'buildTool',
            message: 'Would you like to use Maven or Gradle for building the backend?',
            choices: [
                {
                    value: 'maven',
                    name: 'Maven'
                },
                {
                    value: 'gradle',
                    name: 'Gradle'
                }
            ],
            default: 'maven'
        }
    ];

    if (meta) return prompts; // eslint-disable-line consistent-return

    const done = this.async();

    this.prompt(prompts).then((props) => {
        this.serviceDiscoveryType = props.serviceDiscoveryType;
        this.authenticationType = props.authenticationType;

        // JWT authentication is mandatory with Eureka, so the JHipster Registry
        // can control the applications
        if (this.serviceDiscoveryType === 'eureka' && this.authenticationType !== 'uaa' && this.authenticationType !== 'oauth2') {
            this.authenticationType = 'jwt';
        }

        if (this.authenticationType === 'session') {
            this.rememberMeKey = crypto.randomBytes(20).toString('hex');
        }

        if (this.authenticationType === 'jwt' || this.applicationType === 'microservice') {
            this.jwtSecretKey = crypto.randomBytes(20).toString('hex');
        }

        // user-management will be handled by UAA app, oauth expects users to be managed in IpP
        if ((this.applicationType === 'gateway' && this.authenticationType === 'uaa') || this.authenticationType === 'oauth2') {
            this.skipUserManagement = true;
        }

        if (this.applicationType === 'uaa') {
            this.authenticationType = 'uaa';
        }

        this.packageName = props.packageName;
        this.serverPort = props.serverPort;
        if (this.serverPort === undefined) {
            this.serverPort = '8080';
        }
        this.cacheProvider = props.cacheProvider;
        this.enableHibernateCache = props.enableHibernateCache;
        this.databaseType = props.databaseType;
        this.devDatabaseType = props.devDatabaseType;
        this.prodDatabaseType = props.prodDatabaseType;
        this.searchEngine = props.searchEngine;
        this.buildTool = props.buildTool;
        this.uaaBaseName = this.getUaaAppName(props.uaaBaseName).baseName;

        if (this.databaseType === 'no') {
            this.devDatabaseType = 'no';
            this.prodDatabaseType = 'no';
            this.enableHibernateCache = false;
        } else if (this.databaseType === 'mongodb') {
            this.devDatabaseType = 'mongodb';
            this.prodDatabaseType = 'mongodb';
            this.enableHibernateCache = false;
        } else if (this.databaseType === 'couchbase') {
            this.devDatabaseType = 'couchbase';
            this.prodDatabaseType = 'couchbase';
            this.enableHibernateCache = false;
        } else if (this.databaseType === 'cassandra') {
            this.devDatabaseType = 'cassandra';
            this.prodDatabaseType = 'cassandra';
            this.enableHibernateCache = false;
        }
        // Hazelcast is mandatory for Gateways, as it is used for rate limiting
        if (this.applicationType === 'gateway') {
            this.cacheProvider = 'hazelcast';
        }
        done();
    });
}

function askForOptionalItems(meta) {
    if (!meta && this.existingProject) return;

    const applicationType = this.applicationType;
    const choices = [];
    const defaultChoice = [];
    choices.push({
        name: 'Reactive APIs, using Spring Webflux',
        value: 'reactive:true'
    });
    if (this.databaseType === 'sql' || this.databaseType === 'mongodb') {
        choices.push({
            name: 'Search engine using Elasticsearch',
            value: 'searchEngine:elasticsearch'
        });
    }
    if (applicationType === 'monolith' || applicationType === 'gateway') {
        choices.push({
            name: 'WebSockets using Spring Websocket',
            value: 'websocket:spring-websocket'
        });
    }
    choices.push({
        name: 'API first development using swagger-codegen',
        value: 'enableSwaggerCodegen:true'
    });
    choices.push({
        name: 'Asynchronous messages using Apache Kafka',
        value: 'messageBroker:kafka'
    });

    const PROMPTS = {
        type: 'checkbox',
        name: 'serverSideOptions',
        message: 'Which other technologies would you like to use?',
        choices,
        default: defaultChoice
    };

    if (meta) return PROMPTS; // eslint-disable-line consistent-return

    const done = this.async();
    if (choices.length > 0) {
        this.prompt(PROMPTS).then((prompt) => {
            this.serverSideOptions = prompt.serverSideOptions;
            this.reactive = this.getOptionFromArray(this.serverSideOptions, 'reactive');
            this.websocket = this.getOptionFromArray(this.serverSideOptions, 'websocket');
            this.searchEngine = this.getOptionFromArray(this.serverSideOptions, 'searchEngine');
            this.messageBroker = this.getOptionFromArray(this.serverSideOptions, 'messageBroker');
            this.enableSwaggerCodegen = this.getOptionFromArray(this.serverSideOptions, 'enableSwaggerCodegen');
            // Only set this option if it hasn't been set in a previous question, as it's only optional for monoliths
            if (!this.serviceDiscoveryType) {
                this.serviceDiscoveryType = this.getOptionFromArray(this.serverSideOptions, 'serviceDiscoveryType');
            }
            done();
        });
    } else {
        done();
    }
}

function askFori18n() {
    if (this.existingProject || this.configOptions.skipI18nQuestion) return;

    this.aski18n(this);
}
<|endoftext|>


<commit_before>#!/usr/bin/env node

var derivePkg = require('../index.js');

var argv = require('minimist')(process.argv.slice(2), {
  alias: {
    d: 'outDir',
    v: 'version',
    n: 'name'
  }
});

if (argv._.length > 1) {
  showUsage().on('end', function() {
    process.exit(1);
  });
}
else {
  derivePkg(opts._[0], opts);
}

function showUsage() {
  var fs = require('fs');
  var path = require('path');
  var usage = fs.createReadStream(path.join(__dirname, 'usage.txt'));
  usage.pipe(process.stdout);
  return usage;
}
<commit_msg>Fixed undefined variable error (#4)<commit_after>#!/usr/bin/env node

var derivePkg = require('../index.js');

var argv = require('minimist')(process.argv.slice(2), {
  alias: {
    d: 'outDir',
    v: 'version',
    n: 'name'
  }
});

if (argv._.length > 1) {
  showUsage().on('end', function() {
    process.exit(1);
  });
}
else {
  derivePkg(argv._[0], argv);
}

function showUsage() {
  var fs = require('fs');
  var path = require('path');
  var usage = fs.createReadStream(path.join(__dirname, 'usage.txt'));
  usage.pipe(process.stdout);
  return usage;
}
<|endoftext|>


<commit_before>#!/usr/bin/env node

var argv = require("subarg")(process.argv.slice(2), {
  boolean: ["source-map", "watch", "stub-not-found", "export-names"]
});

if (argv.print) {
  console.log("options: ", JSON.stringify(camelKeys(parseArgs(argv))));
  console.log("files: ", JSON.stringify(parseFiles(argv)));
}
else {
  require("../src/index").bundle(parseFiles(argv), camelKeys(parseArgs(argv)));
}

function parseFiles(argv) {
  return {
    src: argv._.concat(flattenDefault(argv, "src")).filter(Boolean),
    dest: argv.dest
  };
}

function parseArgs(argv) {
  return Object.assign({}, argv, {
    loader: [].concat(flattenDefault(argv, "loader")).filter(Boolean),
    bundler: [].concat(flattenDefault(argv, "bundler")).filter(Boolean)
  });
}

function flattenDefault(source, target) {
  return source[target] && source[target]._ ? source[target]._ : source[target];
}

function camelKeys(args) {
  var result;

  if (args && args.constructor === Object) {
    result = {}
    Object.keys(args).forEach(arg => result[toCamel(arg)] = camelKeys(args[arg]));
  }
  else if (Array.isArray(args)) {
    return args.map(camelKeys);
  }

  return result || args;
}

function toCamel(name) {
  return name.replace(/\-(\w)/g, (match, value) => value.toUpperCase());
}
<commit_msg>adding ignore-not-found to the list of boolean values<commit_after>#!/usr/bin/env node

var argv = require("subarg")(process.argv.slice(2), {
  boolean: ["source-map", "watch", "stub-not-found", "ignore-not-found", "export-names"]
});

if (argv.print) {
  console.log("options: ", JSON.stringify(camelKeys(parseArgs(argv))));
  console.log("files: ", JSON.stringify(parseFiles(argv)));
}
else {
  require("../src/index").bundle(parseFiles(argv), camelKeys(parseArgs(argv)));
}

function parseFiles(argv) {
  return {
    src: argv._.concat(flattenDefault(argv, "src")).filter(Boolean),
    dest: argv.dest
  };
}

function parseArgs(argv) {
  return Object.assign({}, argv, {
    loader: [].concat(flattenDefault(argv, "loader")).filter(Boolean),
    bundler: [].concat(flattenDefault(argv, "bundler")).filter(Boolean)
  });
}

function flattenDefault(source, target) {
  return source[target] && source[target]._ ? source[target]._ : source[target];
}

function camelKeys(args) {
  var result;

  if (args && args.constructor === Object) {
    result = {}
    Object.keys(args).forEach(arg => result[toCamel(arg)] = camelKeys(args[arg]));
  }
  else if (Array.isArray(args)) {
    return args.map(camelKeys);
  }

  return result || args;
}

function toCamel(name) {
  return name.replace(/\-(\w)/g, (match, value) => value.toUpperCase());
}
<|endoftext|>


<commit_before>'use strict';
var key = process.argv[2];
if (key==='--list') {
  var config = require('./config');
  var generators = config.get('generators') || [];
  if (generators.length>0) {
    var inquirer = require('inquirer');
    var list = {
      type: "list",
      message: "What generator will you use.",
      name: "key"
    };
    list.choices = generators.map(function(cur) {
      return {name: cur, value: cur};
    });
    inquirer.prompt(list,function(answer) {
      runGenerator(answer.key);
    });
  } else {
    console.log('No generators installed.')
  }
} else {
  runGenerator(key);
}

function runGenerator(key) {
  var func;
  if (!func) try { func = require(key); } catch(e) {}
  if (!func) try { func = require('nyg-'+key); } catch(e) {}
  if (func) {
    func();
  } else {
    console.log('Generator',key,'not installed.');
  }
}<commit_msg>Updated cli list<commit_after>'use strict';
var key = process.argv[2];
if (key==='--list') {
  var config = require('./config');
  var generators = config.get('generators') || [];
  if (generators.length>0) {
    var inquirer = require('inquirer');
    var list = {
      type: "list",
      message: "What generator will you use.",
      name: "key"
    };
    list.choices = generators.map(function(cur) {
      return {name: cur, value: cur};
    });
    list.choices.push({name: 'exit', value: 'exit'});
    inquirer.prompt(list,function(answer) {
      if (answer.key!='exit') runGenerator(answer.key);
    });
  } else {
    console.log('No generators installed.')
  }
} else {
  runGenerator(key);
}

function runGenerator(key) {
  var func;
  if (!func) try { func = require(key); } catch(e) {}
  if (!func) try { func = require('nyg-'+key); } catch(e) {}
  if (func) {
    func();
  } else {
    console.log('Generator',key,'not installed.');
  }
}<|endoftext|>


<commit_before>#!/usr/bin/env node
"use strict";

var cliArgs = require("command-line-args");
var dope = require("console-dope");
var jsdoc2md = require("../");
var domain = require("domain");
var dmd = require("dmd");
var jsdocParse = require("jsdoc-parse");

var args = [
    { name: "verbose", alias: "v", type: Boolean,
      description: "More verbose error reporting"
    },
    { name: "help", alias: "h", type: Boolean,
      description: "Print usage information"
    }
];

var cli = cliArgs(args.concat(jsdocParse.cliOptions).concat(dmd.cliOptions));

var usage = cli.getUsage({
    title: "jsdoc-to-markdown",
    header: "Markdown API documentation generator, good for Github projects",
    forms: [
        "$ jsdoc2md [<options>] <source_files>"
    ]
});

try{
    var argv = cli.parse({ unexpectedType: "string" });
} catch(err){
    halt(err);
}

if (argv.help){
    dope.log(usage);
    process.exit(0);
}

var d = domain.create();
d.on("error", halt);
d.run(function(){
    if(argv.src){
        jsdoc2md.render(argv.src, argv).pipe(process.stdout);
    } else {
        process.stdin.pipe(jsdoc2md.render(argv)).pipe(process.stdout);
    }
});

function halt(err){
    if (err.code === "EPIPE") process.exit(0); /* no big deal */

    if (argv){
        if (argv.verbose){
            dope.red.error(err.stack || err);
        } else {
            dope.red.error("Error: " + err.message);
            dope.red.error("(run jsdoc2md with --verbose for a stack trace)");
        }
    } else {
        dope.red.error(err.stack);
    }
    dope.error(usage);
    process.exit(1);
}
<commit_msg>restored --json<commit_after>#!/usr/bin/env node
"use strict";

var cliArgs = require("command-line-args");
var dope = require("console-dope");
var jsdoc2md = require("../");
var domain = require("domain");
var dmd = require("dmd");
var jsdocParse = require("jsdoc-parse");

var args = [
    { name: "verbose", alias: "v", type: Boolean,
      description: "More verbose error reporting"
    },
    { name: "help", alias: "h", type: Boolean,
      description: "Print usage information"
    },
    { name: "json", alias: "j", type: Boolean,
      description: "Output the parsed jsdoc data only"
    }
];

var cli = cliArgs(args.concat(jsdocParse.cliOptions).concat(dmd.cliOptions));

var usage = cli.getUsage({
    title: "jsdoc-to-markdown",
    header: "Markdown API documentation generator, good for Github projects",
    forms: [
        "$ jsdoc2md [<options>] <source_files>"
    ]
});

try{
    var argv = cli.parse({ unexpectedType: "string" });
} catch(err){
    halt(err);
}

if (argv.help){
    dope.log(usage);
    process.exit(0);
}

var d = domain.create();
d.on("error", halt);
d.run(function(){
    if(argv.src){
        jsdoc2md.render(argv.src, argv).pipe(process.stdout);
    } else {
        process.stdin.pipe(jsdoc2md.render(argv)).pipe(process.stdout);
    }
});

function halt(err){
    if (err.code === "EPIPE") process.exit(0); /* no big deal */

    if (argv){
        if (argv.verbose){
            dope.red.error(err.stack || err);
        } else {
            dope.red.error("Error: " + err.message);
            dope.red.error("(run jsdoc2md with --verbose for a stack trace)");
        }
    } else {
        dope.red.error(err.stack);
    }
    dope.error(usage);
    process.exit(1);
}
<|endoftext|>


<commit_before>#!/usr/bin/env node
/**
 * An executable command which will be added to $PATH.
 *
 * @copyright Copyright 2016 Kevin Locke <kevin@kevinlocke.name>
 * @license MIT
 */
'use strict';

var Command = require('commander').Command;
var modulename = require('..');
var packageJson = require('../package.json');

/** Result from command entry points.
 *
 * @typedef {{
 *   code: (?number|undefined),
 *   stdout: (?string|undefined),
 *   stderr: (?string|undefined)
 * }} CommandResult
 * @property {?number=} code Exit code for the command.
 * @property {?string=} stdout Content to write to stdout.
 * @property {?string=} stderr Content to write to stderr.
 */

/** Entry point for this command.
 *
 * @param {!Array<string>} args Command-line arguments.
 * @param {?function(Error, ?CommandResult=)=}
 * callback Callback for the command result or error.  Required if
 * global.Promise is not defined.
 * @return {Promise|undefined} If callback is not given and
 * global.Promise is defined, a Promise which will
 * resolve on completion.
 */
function modulenameCmd(args, callback) {
  if (!callback && typeof Promise === 'function') {
    return new Promise(function(resolve, reject) {
      modulenameCmd(args, function(err, result) {
        if (err) reject(err); else resolve(result);
      });
    });
  }

  if (typeof callback !== 'function') {
    throw new TypeError('callback must be a function');
  }

  // TODO:  Proxy console.{error,log} and process.exit so we can return result
  var command = new Command()
    // .arguments() splits on white space.  Call .parseExpectedArgs directly.
    .parseExpectedArgs(['<arg name>'])
    .option('-q, --quiet', 'print less output')
    .option('-v, --verbose', 'print more output')
    .version(packageJson.version)
    .parse(args);

  if (command.args.length !== 1) {
    callback(new Error('Exactly one argument is required.\n' +
          command.helpInformation()));
    return undefined;
  }

  // Parse arguments and call API function with parsed options
  modulename();
  return undefined;
}

module.exports = modulenameCmd;

if (require.main === module) {
  // This file was invoked directly.
  /* eslint-disable no-process-exit */
  modulenameCmd(process.argv, function(err, result) {
    var errOrResult = err || result;
    if (errOrResult.stdout) process.stdout.write(errOrResult.stdout);
    if (errOrResult.stderr) process.stderr.write(errOrResult.stderr);
    if (err) process.stderr.write(err.name + ': ' + err.message + '\n');

    var code = typeof errOrResult.code === 'number' ? errOrResult.code :
                err ? 1 : 0;
    process.exit(code);
  });
}
<commit_msg>Add description to commander invocation<commit_after>#!/usr/bin/env node
/**
 * An executable command which will be added to $PATH.
 *
 * @copyright Copyright 2016 Kevin Locke <kevin@kevinlocke.name>
 * @license MIT
 */
'use strict';

var Command = require('commander').Command;
var modulename = require('..');
var packageJson = require('../package.json');

/** Result from command entry points.
 *
 * @typedef {{
 *   code: (?number|undefined),
 *   stdout: (?string|undefined),
 *   stderr: (?string|undefined)
 * }} CommandResult
 * @property {?number=} code Exit code for the command.
 * @property {?string=} stdout Content to write to stdout.
 * @property {?string=} stderr Content to write to stderr.
 */

/** Entry point for this command.
 *
 * @param {!Array<string>} args Command-line arguments.
 * @param {?function(Error, ?CommandResult=)=}
 * callback Callback for the command result or error.  Required if
 * global.Promise is not defined.
 * @return {Promise|undefined} If callback is not given and
 * global.Promise is defined, a Promise which will
 * resolve on completion.
 */
function modulenameCmd(args, callback) {
  if (!callback && typeof Promise === 'function') {
    return new Promise(function(resolve, reject) {
      modulenameCmd(args, function(err, result) {
        if (err) reject(err); else resolve(result);
      });
    });
  }

  if (typeof callback !== 'function') {
    throw new TypeError('callback must be a function');
  }

  // TODO:  Proxy console.{error,log} and process.exit so we can return result
  var command = new Command()
    .description('Does the thing with the thing and stuff.')
    // .arguments() splits on white space.  Call .parseExpectedArgs directly.
    .parseExpectedArgs(['<arg name>'])
    .option('-q, --quiet', 'print less output')
    .option('-v, --verbose', 'print more output')
    .version(packageJson.version)
    .parse(args);

  if (command.args.length !== 1) {
    callback(new Error('Exactly one argument is required.\n' +
          command.helpInformation()));
    return undefined;
  }

  // Parse arguments and call API function with parsed options
  modulename();
  return undefined;
}

module.exports = modulenameCmd;

if (require.main === module) {
  // This file was invoked directly.
  /* eslint-disable no-process-exit */
  modulenameCmd(process.argv, function(err, result) {
    var errOrResult = err || result;
    if (errOrResult.stdout) process.stdout.write(errOrResult.stdout);
    if (errOrResult.stderr) process.stderr.write(errOrResult.stderr);
    if (err) process.stderr.write(err.name + ': ' + err.message + '\n');

    var code = typeof errOrResult.code === 'number' ? errOrResult.code :
                err ? 1 : 0;
    process.exit(code);
  });
}
<|endoftext|>


<commit_before>#!/usr/bin/env node
var foreground = require('foreground-child')
var path = require('path')
var sw = require('spawn-wrap')

if (process.env.NYC_CWD) {
  var NYC = require('../')
  ;(new NYC()).wrap()

  // make sure we can run coverage on
  // our own index.js, I like turtles.
  var name = require.resolve('../')
  delete require.cache[name]

  sw.runMain()
} else {
  var NYC = require('../')
  var yargs = require('yargs')
    .usage('$0 [command] [options]\n\nrun your tests with the nyc bin to instrument them with coverage')
    .command('report', 'run coverage report for .nyc_output', function (yargs) {
      yargs
        .usage('$0 report [options]')
        .option('r', {
          alias: 'reporter',
          describe: 'coverage reporter(s) to use',
          default: 'text'
        })
        .help('h')
        .alias('h', 'help')
        .example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')
    })
    .command('check-coverage', 'check whether coverage is within thresholds provided', function (yargs) {
      yargs
        .usage('$0 check-coverage [options]')
        .option('b', {
          alias: 'branches',
          default: 0,
          description: 'what % of branches must be covered?'
        })
        .option('f', {
          alias: 'functions',
          default: 0,
          description: 'what % of functions must be covered?'
        })
        .option('l', {
          alias: 'lines',
          default: 90,
          description: 'what % of lines must be covered?'
        })
        .option('s', {
          alias: 'statements',
          default: 0,
          description: 'what % of statements must be covered?'
        })
        .help('h')
        .alias('h', 'help')
        .example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided")
    })
    .option('r', {
      alias: 'reporter',
      describe: 'coverage reporter(s) to use',
      default: 'text'
    })
    .option('s', {
      alias: 'silent',
      default: false,
      type: 'boolean',
      describe: "don't output a report after tests finish running"
    })
    .help('h')
    .alias('h', 'help')
    .version(require('../package.json').version)
    .example('$0 npm test', 'instrument your tests with coverage')
    .example('$0 report --reporter=text-lcov', 'output lcov report after running your tests')
    .epilog('visit http://git.io/vTJJB for list of available reporters')
  var argv = yargs.argv

  if (~argv._.indexOf('report')) {
    // run a report.
    process.env.NYC_CWD = process.cwd()

    report(argv)
  } else if (~argv._.indexOf('check-coverage')) {
    foreground(
      'npm',
      [
        'run',
        'istanbul',
        '--',
        'check-coverage',
        '--lines=' + argv.lines,
        '--functions=' + argv.functions,
        '--branches=' + argv.branches,
        '--statements=' + argv.statements,
        path.resolve(process.cwd(), './.nyc_output/*.json')
      ]
    )
  } else if (argv._.length) {
    // wrap subprocesses and execute argv[1]
    var nyc = (new NYC())
    nyc.cleanup()

    sw([__filename], {
      NYC_CWD: process.cwd()
    })

    foreground(nyc.mungeArgs(argv), function (done) {
      if (!argv.silent) report(argv)
      return done()
    })
  } else {
    // I don't have a clue what you're doing.
    yargs.showHelp()
  }
}

function report (argv) {
  process.env.NYC_CWD = process.cwd()

  ;(new NYC({
    reporter: argv.reporter
  })).report()
}
<commit_msg>muck with the path<commit_after>#!/usr/bin/env node
var foreground = require('foreground-child')
var path = require('path')
var sw = require('spawn-wrap')

if (process.env.NYC_CWD) {
  var NYC = require('../')
  ;(new NYC()).wrap()

  // make sure we can run coverage on
  // our own index.js, I like turtles.
  var name = require.resolve('../')
  delete require.cache[name]

  sw.runMain()
} else {
  var NYC = require('../')
  var yargs = require('yargs')
    .usage('$0 [command] [options]\n\nrun your tests with the nyc bin to instrument them with coverage')
    .command('report', 'run coverage report for .nyc_output', function (yargs) {
      yargs
        .usage('$0 report [options]')
        .option('r', {
          alias: 'reporter',
          describe: 'coverage reporter(s) to use',
          default: 'text'
        })
        .help('h')
        .alias('h', 'help')
        .example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')
    })
    .command('check-coverage', 'check whether coverage is within thresholds provided', function (yargs) {
      yargs
        .usage('$0 check-coverage [options]')
        .option('b', {
          alias: 'branches',
          default: 0,
          description: 'what % of branches must be covered?'
        })
        .option('f', {
          alias: 'functions',
          default: 0,
          description: 'what % of functions must be covered?'
        })
        .option('l', {
          alias: 'lines',
          default: 90,
          description: 'what % of lines must be covered?'
        })
        .option('s', {
          alias: 'statements',
          default: 0,
          description: 'what % of statements must be covered?'
        })
        .help('h')
        .alias('h', 'help')
        .example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided")
    })
    .option('r', {
      alias: 'reporter',
      describe: 'coverage reporter(s) to use',
      default: 'text'
    })
    .option('s', {
      alias: 'silent',
      default: false,
      type: 'boolean',
      describe: "don't output a report after tests finish running"
    })
    .help('h')
    .alias('h', 'help')
    .version(require('../package.json').version)
    .example('$0 npm test', 'instrument your tests with coverage')
    .example('$0 report --reporter=text-lcov', 'output lcov report after running your tests')
    .epilog('visit http://git.io/vTJJB for list of available reporters')
  var argv = yargs.argv

  if (~argv._.indexOf('report')) {
    // run a report.
    process.env.NYC_CWD = process.cwd()

    report(argv)
  } else if (~argv._.indexOf('check-coverage')) {
    var nycOutputDir = path.resolve(process.cwd(), './.nyc_output/*.json')

    // switch the working directory to nyc,
    // so that we have the standard bin.
    process.chdir(path.resolve(__dirname, '../'))

    foreground(
      'npm',
      [
        'run',
        'istanbul',
        '--',
        'check-coverage',
        '--lines=' + argv.lines,
        '--functions=' + argv.functions,
        '--branches=' + argv.branches,
        '--statements=' + argv.statements,
        nycOutputDir
      ]
    )
  } else if (argv._.length) {
    // wrap subprocesses and execute argv[1]
    var nyc = (new NYC())
    nyc.cleanup()

    sw([__filename], {
      NYC_CWD: process.cwd()
    })

    foreground(nyc.mungeArgs(argv), function (done) {
      if (!argv.silent) report(argv)
      return done()
    })
  } else {
    // I don't have a clue what you're doing.
    yargs.showHelp()
  }
}

function report (argv) {
  process.env.NYC_CWD = process.cwd()

  ;(new NYC({
    reporter: argv.reporter
  })).report()
}
<|endoftext|>


<commit_before>'use strict';

const Promise = require('bluebird');
const wait = require('../src/util/wait');
const debug = require('../src/util/debug')('aggregate:run');
const connection = require('../src/mongo/connection');
const baseIdle = 6000; // 1 minute idle

// Config
const config = require('../src/config/config').globalConfig;
const sources = Object.keys(config.source);
const aggregations = Object.keys(config.aggregation);

const source = require('../src/source/source');
const aggregate = require('../src/aggregation/aggregate');

const syncHistory = require('../src/mongo/models/syncHistory');

const doOne = Promise.coroutine(function * (type, operation, collection) {
    var options, interval;
    if(type === 'source') {
        options = config.source[collection];
    } else {
        options = config.aggregation[collection];
    }

    if(operation === 'update') {
        interval = (options.updateInterval || config.updateInterval) * 1000;
    } else {
        interval = (options.deleteInterval || config.deleteInterval) * 1000;
    }
    try {
        var lastSync = yield syncHistory.getLast(type, operation, collection);
        if(lastSync) lastSync = lastSync.date;
        if(!lastSync || Date.now() - lastSync.getTime() > interval) {
            if(type === 'source' && operation === 'update') {
                yield source.copy(Object.assign({collection}, options));
            } else if(type === 'source' && operation === 'delete') {
                yield source.remove(Object.assign({collection}, options));
            }  else if (type === 'aggregation') {
                yield aggregate(collection);
            }
            debug.debug(`Synced ${collection} successfully`);
            yield syncHistory.save(type, operation, collection, 'success');
        } else {
            debug.trace(`Skipping sync of ${collection} of type ${type} and operation ${operation}`);
        }
    } catch(e) {
        debug.error(`Error syncing source ${collection}: ${e}`);
        yield syncHistory.save('source', collection, 'failure: ' + e.message);
    }
});

Promise.coroutine(function* () {
    yield connection();
    while (true) {
        for (const collection of sources) {
            yield doOne('source', 'update', collection);
            yield doOne('source', 'delete', collection);
        }

        for (const collection of aggregations) {
            yield doOne('aggregation', 'update', collection);
        }

        yield wait(baseIdle);
    }
})();
<commit_msg>fix syncHistory save when on failure<commit_after>'use strict';

const Promise = require('bluebird');
const wait = require('../src/util/wait');
const debug = require('../src/util/debug')('aggregate:run');
const connection = require('../src/mongo/connection');
const baseIdle = 6000; // 1 minute idle

// Config
const config = require('../src/config/config').globalConfig;
const sources = Object.keys(config.source);
const aggregations = Object.keys(config.aggregation);

const source = require('../src/source/source');
const aggregate = require('../src/aggregation/aggregate');

const syncHistory = require('../src/mongo/models/syncHistory');

const doOne = Promise.coroutine(function * (type, operation, collection) {
    var options, interval;
    if(type === 'source') {
        options = config.source[collection];
    } else {
        options = config.aggregation[collection];
    }

    if(operation === 'update') {
        interval = (options.updateInterval || config.updateInterval) * 1000;
    } else {
        interval = (options.deleteInterval || config.deleteInterval) * 1000;
    }
    try {
        var lastSync = yield syncHistory.getLast(type, operation, collection);
        if(lastSync) lastSync = lastSync.date;
        if(!lastSync || Date.now() - lastSync.getTime() > interval) {
            if(type === 'source' && operation === 'update') {
                yield source.copy(Object.assign({collection}, options));
            } else if(type === 'source' && operation === 'delete') {
                yield source.remove(Object.assign({collection}, options));
            }  else if (type === 'aggregation') {
                yield aggregate(collection);
            }
            debug.debug(`Synced ${collection} successfully`);
            yield syncHistory.save(type, operation, collection, 'success');
        } else {
            debug.trace(`Skipping sync of ${collection} of type ${type} and operation ${operation}`);
        }
    } catch(e) {
        debug.error(`Error syncing source ${collection}: ${e}`);
        yield syncHistory.save('source', operation, collection, 'failure: ' + e.message);
    }
});

Promise.coroutine(function* () {
    yield connection();
    while (true) {
        for (const collection of sources) {
            yield doOne('source', 'update', collection);
            yield doOne('source', 'delete', collection);
        }

        for (const collection of aggregations) {
            yield doOne('aggregation', 'update', collection);
        }

        yield wait(baseIdle);
    }
})();
<|endoftext|>


<commit_before>var request = require("request");
var zlib = require("zlib");

exports.get = function(url, cb) {
	try {
		var options = { url: url };
		var req = request(options);

		req.on("response", function(response) {
			var chunks = [];

			response.on("data", function(chunk) {
				chunks.push(chunk);
			});

			response.on("end", function() {
				var buffer = Buffer.concat(chunks);

				var encoding = response.headers["content-encoding"];
				if (encoding == "gzip") {
					zlib.gunzip(buffer, function(err, decoded) {
						cb(err, response, decoded && decoded.toString());
					});
				} else if (encoding == "deflate") {
					zlib.inflate(buffer, function(err, decoded) {
						cb(err, response, decoded && decoded.toString());
					})
				} else {
					cb(err, response, buffer.toString());
				}
			 });
		});

		req.on("error", cb);
	} catch(err) { return cb(err); }

	function response_available(err, response, body) {
		try {
			if(err) return cb(err, null);
			if(response.statusCode != 200) return cb("HTTP status code " + response.statusCode + " received.", null);

			console.log(typeof body);

			var encoding = response.headers['content-encoding'];
			if (encoding == 'gzip') {
				zlib.gunzip(body, function(err, decoded) {
					cb(err, response, decoded);
				});
			} else if (encoding == 'deflate') {
				zlib.inflate(body, function(err, decoded) {
					cb(err, response, decoded);
				})
			} else {
				cb(err, response, body);
			}
		} catch(err) { return cb(err); }
	}
}
<commit_msg>Fix undefined name in http.js.<commit_after>var request = require("request");
var zlib = require("zlib");

exports.get = function(url, cb) {
	try {
		var options = { url: url };
		var req = request(options);

		req.on("response", function(response) {
			var chunks = [];

			response.on("data", function(chunk) {
				chunks.push(chunk);
			});

			response.on("end", function() {
				var buffer = Buffer.concat(chunks);

				var encoding = response.headers["content-encoding"];
				if (encoding == "gzip") {
					zlib.gunzip(buffer, function(err, decoded) {
						cb(err, response, decoded && decoded.toString());
					});
				} else if (encoding == "deflate") {
					zlib.inflate(buffer, function(err, decoded) {
						cb(err, response, decoded && decoded.toString());
					})
				} else {
					cb(null, response, buffer.toString());
				}
			 });
		});

		req.on("error", cb);
	} catch(err) { return cb(err); }

	function response_available(err, response, body) {
		try {
			if(err) return cb(err, null);
			if(response.statusCode != 200) return cb("HTTP status code " + response.statusCode + " received.", null);

			console.log(typeof body);

			var encoding = response.headers['content-encoding'];
			if (encoding == 'gzip') {
				zlib.gunzip(body, function(err, decoded) {
					cb(err, response, decoded);
				});
			} else if (encoding == 'deflate') {
				zlib.inflate(body, function(err, decoded) {
					cb(err, response, decoded);
				})
			} else {
				cb(err, response, body);
			}
		} catch(err) { return cb(err); }
	}
}
<|endoftext|>


<commit_before>// ==UserScript==
// @name        bloater
// @author      Christopher Hunter
// @namespace   http://github.com/chuntr/
// @source      https://github.com/chuntr/pagebloat
// @version     0.2.11
// @updateURL   https://raw.githubusercontent.com/chuntr/pagebloat/master/bloater.js
// @description Tampermonkey script that resizes the default column widths for some popular sites to take advantage of wider monitors
// @match       http://github.com/*
// @match       http://git.corp.*/*
// @match       http://*.readthedocs.org/*
// @match       http://stackexchange.com/*
// @match       http://*.stackexchange.com/*
// @match       http://stackoverflow.com/*
// @match       http://serverfault.com/*
// @require     http://code.jquery.com/jquery-latest.js
// @grant       none
// ==/UserScript==
/* jshint -W097 */
'use strict';
function jQsetWidthByClassName(width,className){
    jQuery(className).css('width', width);
}
function jQsetMaxWidthByClassName(pxWidth,className){
    jQuery(className).css('max-width', pxWidth);
}
function setWidthByClassName(width,className){
    var elements = document.getElementsByClassName(className);
    for (var i=0; i<elements.length; i++){
        elements[i].setAttribute("style","width:"+width+";");
        elements[i].style.width = width;
    }
}
function setHeightByClassName(width,className){
    var elements = document.getElementsByClassName(className);
    for (var i=0; i<elements.length; i++){
        elements[i].setAttribute("style","height:"+height+";");
        elements[i].style.height = height;
    }
}
function setWidthById(width,id){
    var element = document.getElementById(id);
    element.setAttribute("style","width:"+width+";");
    element.style.width = width;
}

// readthedocs.org
jQsetWidthByClassName(1024,"div.rst-content");
jQsetMaxWidthByClassName("1128px","div.wy-nav-content");

// stackexchange
setWidthById("1400px","content");
setWidthById("1100px","mainbar");
setWidthById("1100px","question");
setWidthByClassName("1000px","post-text");
setWidthById("1000px","answers");

// github/git.corp
setHeightByClassName("509","ace_editor");
setHeightByClassName("509","ace_content");
<commit_msg>fixed wrong parameter name in setHeightByClassName<commit_after>// ==UserScript==
// @name        bloater
// @author      Christopher Hunter
// @namespace   http://github.com/chuntr/
// @source      https://github.com/chuntr/pagebloat
// @version     0.2.12
// @updateURL   https://raw.githubusercontent.com/chuntr/pagebloat/master/bloater.js
// @description Tampermonkey script that resizes the default column widths for some popular sites to take advantage of wider monitors
// @match       http://github.com/*
// @match       http://git.corp.*/*
// @match       http://*.readthedocs.org/*
// @match       http://stackexchange.com/*
// @match       http://*.stackexchange.com/*
// @match       http://stackoverflow.com/*
// @match       http://serverfault.com/*
// @require     http://code.jquery.com/jquery-latest.js
// @grant       none
// ==/UserScript==
/* jshint -W097 */
'use strict';
function jQsetWidthByClassName(width,className){
    jQuery(className).css('width', width);
}
function jQsetMaxWidthByClassName(pxWidth,className){
    jQuery(className).css('max-width', pxWidth);
}
function setWidthByClassName(width,className){
    var elements = document.getElementsByClassName(className);
    for (var i=0; i<elements.length; i++){
        elements[i].setAttribute("style","width:"+width+";");
        elements[i].style.width = width;
    }
}
function setHeightByClassName(height,className){
    var elements = document.getElementsByClassName(className);
    for (var i=0; i<elements.length; i++){
        elements[i].setAttribute("style","height:"+height+";");
        elements[i].style.height = height;
    }
}
function setWidthById(width,id){
    var element = document.getElementById(id);
    element.setAttribute("style","width:"+width+";");
    element.style.width = width;
}

// readthedocs.org
jQsetWidthByClassName(1024,"div.rst-content");
jQsetMaxWidthByClassName("1128px","div.wy-nav-content");

// stackexchange
setWidthById("1400px","content");
setWidthById("1100px","mainbar");
setWidthById("1100px","question");
setWidthByClassName("1000px","post-text");
setWidthById("1000px","answers");

// github/git.corp
setHeightByClassName("509","ace_editor");
setHeightByClassName("509","ace_content");
<|endoftext|>


<commit_before>/*global define $ requestAnimationFrame*/

define(function (require) {

	var MovieClip,
		Utils = require('app/utils');

	require('tweenmax');

	MovieClip = function (root, el, commandTimeline, resourceManager, transform) {
		var i,
			transformMat;

		this.root = root;
    	this.el = el;
		this.m_transform = transform;

		//Timeline is a collection of frames
		//Frame is a collection of Command Objects
		this.m_ctimeline = commandTimeline;
		this.m_timeline = new TimelineMax({
			useFrames: true,
			paused: true,
			//onRepeat: this.repeat.bind(this),
			onUpdate: this.runCommands.bind(this),
			onUpdateParams: [resourceManager]
			});
		this.m_currentFrameNo = this.m_timeline.time();

		this.m_frameCount = this.m_ctimeline.Frame.length;
		this.m_children = [];
		if(this.m_transform !== undefined)
		{
			//Apply the transformation on the parent MC
			var transformData =  this.m_transform;
			var transformArray = transformData.split(",");
			transformMat = new Snap.Matrix(transformArray[0],transformArray[1],transformArray[2],transformArray[3],transformArray[4],transformArray[5]);
			console.log('matrix', transformMat, transformMat.toTransformString());
			this.el.transform(transformMat.toTransformString());
		}

		this.m_timeline.add(function () {}, this.m_frameCount + 1);
	}

	//TODO:: should remove associated defs upon removing elements
	//TODO:: should also check if asset/def exists when adding new one to reuse instead of adding/removing

	MovieClip.prototype.clear = function () {
		var items = this.el.selectAll('g'),
			defs,
			i;

		for (i = 0; i < items.length; i += 1) {
			items[i].remove();
		}

		defs = this.el.selectAll('defs *');
		if (defs) {
			for (i = 0; i < defs.length; i += 1) {
				defs[i].remove();
			}
		}
	}

	MovieClip.prototype.runCommands = function (resourceManager) {
		var frame,
			commands,
			c,
			cmdData,
			type,
			command,
			time;

		time = Math.round(this.m_timeline.time()) - 1;
		frame = this.m_ctimeline.Frame[time];
		if (!frame) {
			return;
		}

		commands = frame.Command;

		for (c = 0; c < commands.length; c += 1) {
			cmdData = commands[c];
			type = cmdData.cmdType;
			command = undefined;

			switch(type)
			{
				case "Place":
					command = new PlaceObjectCommand(cmdData.charid, cmdData.objectId, cmdData.placeAfter, cmdData.transformMatrix);
				break;

				case "Move":
					command = new MoveObjectCommand(cmdData.objectId, cmdData.transformMatrix);
				break;

				case "Remove":
					command = new RemoveObjectCommand(cmdData.objectId);
				break;

				case "UpdateZOrder":
					command = new UpdateObjectCommand(cmdData.objectId , cmdData.placeAfter);
				break;

				case "UpdateVisibility":
					command = new UpdateVisibilityCommand(cmdData.objectId , cmdData.visibility);
				break;

				case "UpdateMask":
					command = new UpdateMaskCommand(cmdData.objectId , cmdData.maskTill);
				break;
			}

			if(command !== undefined) {
				command.execute(this, resourceManager);
			}
		}
	}

	MovieClip.prototype.getTimeline = function () {
		return this.m_timeline;
	}

	//PlaceObjectCommand Class
	var PlaceObjectCommand = function(charID, objectID, placeAfter, transform)
	{
		this.m_charID = charID;
		this.m_objectID = objectID;
		this.m_placeAfter = placeAfter;
		this.m_transform = transform;
	}

	//Execute function for PlaceObjectCommand
	PlaceObjectCommand.prototype.execute = function(stage, resourceManager)
	{

		var shape = resourceManager.getShape(this.m_charID),
			bitmap = resourceManager.getBitmap(this.m_charID),
			text = resourceManager.getText(this.m_charID),
			parentMC = stage.el,
			root = stage.root,
			movieclipTimeline,
			movieclip,
			children,
			i,
			childMC,
			index;

		if(shape !== null && shape !== undefined)
		{
			Utils.CreateShape(root, parentMC, resourceManager, this.m_charID, this.m_objectID, this.m_placeAfter, this.m_transform);
		}
		else if(bitmap !== null && bitmap !== undefined)
		{
			Utils.CreateBitmap(root, parentMC, resourceManager, this.m_charID, this.m_objectID, this.m_placeAfter, this.m_transform);
		}
		else if (text !== null && text !== undefined)
		{
			Utils.CreateText(root, parentMC, resourceManager, this.m_charID, this.m_objectID, this.m_placeAfter, this.m_transform);
		}
		else
		{
			// Movie clip logic starts here
			movieclipTimeline = resourceManager.getMovieClip(this.m_charID);

			if(parentMC != undefined)
			{
				//if already exists do not add
				if (parentMC.select('[token="' + this.m_objectID + '"]')) {
					//run move in case different
					var command = new MoveObjectCommand(this.m_objectID, this.m_transform);
					command.execute(stage, resourceManager);
					return;
				}

				//Create a  MC
				childMC = parentMC.g();
				childMC.attr({class: 'movieclip', token: this.m_objectID});

				if(this.m_placeAfter != 0)
				{
					children = parentMC.selectAll('[token="' + this.m_placeAfter + '"]');
					for (i = 0; i < children.length; i += 1) {
						if (children[i].parent().id == parentMC.id) { //ensure child of current movie clip
							children[i].before(childMC);
							break;
						}
					}
				}
				else
				{
					parentMC.add(childMC);
				}

				//Create a corresponding TimelineAnimator
				if(movieclipTimeline)
				{
					movieclip = new MovieClip(root, childMC, movieclipTimeline, resourceManager, this.m_transform);
					stage.m_children.push(movieclip);

					var tl = movieclip.getTimeline();
					tl.play();
				}
			}
		}

	}

	//MoveObjectCommand Class
	var MoveObjectCommand = function(objectID, transform)
	{
		this.m_objectID = objectID;
		//this.m_placeAfter = placeAfter;
		this.m_transform = transform;
	}

	//Execute function for PlaceObjectCommand
	MoveObjectCommand.prototype.execute = function(stage, resourceManager)
	{
		var parentMC,
			transform,
			transformArray,
			transformMat,
			children,
			i;

		parentMC = stage.el;
		transform =  this.m_transform;
		transformArray = transform.split(",");
		transformMat = new Snap.Matrix(transformArray[0],transformArray[1],transformArray[2],transformArray[3],transformArray[4],transformArray[5]);

		if(parentMC != undefined)
		{
			children = parentMC.selectAll('[token="' + this.m_objectID + '"]');

			for (i = 0; i < children.length; i += 1) {
				if (children[i].parent().id == parentMC.id) { //ensure child of current movie clip
					children[i].transform(transformMat.toTransformString());
				}
			}
		}

	}

	//UpdateObjectCommand Class
	var UpdateObjectCommand = function(objectID, placeAfter)
	{
		this.m_objectID = objectID;
		this.m_placeAfter = placeAfter;
	}

	//Execute function for UpdateObjectCommand
	UpdateObjectCommand.prototype.execute = function(timelineAnimator, resourceManager)
	{

		/*
		var parentMC = timelineAnimator.s;

		if(parentMC != undefined)
		{
			//Change the Z order of the targetMC
			var index;
			for(var indexz=0; indexz<parentMC.children.length; indexz++)
			{
				if(parentMC.children[indexz].id == parseInt(this.m_objectID))
				{
					var child = parentMC.getChildAt(indexz);
					if(this.m_placeAfter != 0)
					{
						for(var index=0; index<parentMC.children.length; index++)
						{
							if(parentMC.children[index].id == parseInt(this.m_placeAfter))
							{
								//child.addChildAt(childMC,index);
								parentMC.setChildIndex(child,index - 1)
								break;
							}
						}
					}
					break;
				}
			}
		}
		*/
	}

	//RemoveObjectCommand Class
	var RemoveObjectCommand = function(objectID)
	{
		this.m_objectID = objectID;
	}

	//Execute function for RemoveObjectCommand
	RemoveObjectCommand.prototype.execute = function(stage, resourceManager)
	{
		var parentMC = stage.el,
			child;

		if(parentMC != undefined)
		{
			children = parentMC.selectAll('[token="' + this.m_objectID + '"]');

			for (i = 0; i < children.length; i += 1) {
				if (children[i].parent() == parentMC) { //ensure child of current movie clip
					children[i].remove();
				}
			}
		}
	}

	//UpdateVisbilityCommand Class
	var UpdateVisibilityCommand = function(objectID,visibility)
	{
		this.m_objectID = objectID;
		this.m_visibilty = visibility;
	}

	//Execute function for UpdateVisbilityCommand
	UpdateVisibilityCommand.prototype.execute = function(timelineAnimator, resourceManager)
	{
		/*
		var parentMC = timelineAnimator.m_targetMC;
		if(parentMC != undefined)
		{
			//Remove the targetMC
			var index,visibleBool;
			for(var index=0; index<parentMC.children.length; index++)
			{
				if(parentMC.children[index].id == parseInt(this.m_objectID))
				{
					if(this.m_visibilty == "true")
						visibleBool = true;
						else
						visibleBool = false;

					parentMC.getChildAt(index).visible = visibleBool;

				}
			}
		}
		*/
	}

	var UpdateMaskCommand = function (objectID,maskTill)
	{
		this.m_objectID = objectID;
		this.m_maskTill = maskTill;
	}

	UpdateMaskCommand.prototype.execute = function (stage, resourceManager)
	{
		var parentMC = stage.el,
			mask,
			masked,
			i;

		console.log('till', this.m_maskTill);
		console.log('mask', this.m_objectID);

		if(parentMC != undefined)
		{
			baseMask = parentMC.select('[token="' + this.m_objectID + '"]');

			for (i = parseInt(this.m_maskTill); i > 1; i -= 1) {

				mask = baseMask.clone();
				masked = parentMC.select('[token="' + i + '"]');

				oldID = masked.attr('mask').replace('url(', '').replace(')', '');
				oldMask = parentMC.select(oldID);
				if (oldMask) {
					oldMask.remove();
				}

				masked.attr({
					mask: mask
				});
			}

			baseMask.remove();
		}
	}

	return MovieClip;

});<commit_msg>fix for multiple masks, still need to handle replay issue<commit_after>/*global define $ requestAnimationFrame*/

define(function (require) {

	var MovieClip,
		Utils = require('app/utils');

	require('tweenmax');

	MovieClip = function (root, el, commandTimeline, resourceManager, transform) {
		var i,
			transformMat;

		this.root = root;
    	this.el = el;
		this.m_transform = transform;

		//Timeline is a collection of frames
		//Frame is a collection of Command Objects
		this.m_ctimeline = commandTimeline;
		this.m_timeline = new TimelineMax({
			useFrames: true,
			paused: true,
			onRepeat: this.repeat.bind(this),
			onUpdate: this.runCommands.bind(this),
			onUpdateParams: [resourceManager]
			});
		this.m_currentFrameNo = this.m_timeline.time();

		this.m_frameCount = this.m_ctimeline.Frame.length;
		this.m_children = [];
		if(this.m_transform !== undefined)
		{
			//Apply the transformation on the parent MC
			var transformData =  this.m_transform;
			var transformArray = transformData.split(",");
			transformMat = new Snap.Matrix(transformArray[0],transformArray[1],transformArray[2],transformArray[3],transformArray[4],transformArray[5]);
			console.log('matrix', transformMat, transformMat.toTransformString());
			this.el.transform(transformMat.toTransformString());
		}

		this.m_timeline.add(function () {}, this.m_frameCount + 1);
	}

	//TODO:: should remove associated defs upon removing elements
	//TODO:: should also check if asset/def exists when adding new one to reuse instead of adding/removing

	MovieClip.prototype.repeat = function () {
		this.repeatFlag = true;
	}

	MovieClip.prototype.cleanup = function (commands) {
		//TODO:: clean up unused things on first frame leftover from last frame
	}

	MovieClip.prototype.clear = function () {
		var items = this.el.selectAll('g'),
			defs,
			i;

		for (i = 0; i < items.length; i += 1) {
			items[i].remove();
		}

		defs = this.el.selectAll('defs *');
		if (defs) {
			for (i = 0; i < defs.length; i += 1) {
				defs[i].remove();
			}
		}
	}

	MovieClip.prototype.runCommands = function (resourceManager) {
		var frame,
			commands,
			c,
			cmdData,
			type,
			command,
			time;

		time = Math.round(this.m_timeline.time()) - 1;
		frame = this.m_ctimeline.Frame[time];
		if (!frame) {
			return;
		}

		commands = frame.Command;

		for (c = 0; c < commands.length; c += 1) {
			cmdData = commands[c];
			type = cmdData.cmdType;
			command = undefined;

			switch(type)
			{
				case "Place":
					command = new PlaceObjectCommand(cmdData.charid, cmdData.objectId, cmdData.placeAfter, cmdData.transformMatrix);
				break;

				case "Move":
					command = new MoveObjectCommand(cmdData.objectId, cmdData.transformMatrix);
				break;

				case "Remove":
					command = new RemoveObjectCommand(cmdData.objectId);
				break;

				case "UpdateZOrder":
					command = new UpdateObjectCommand(cmdData.objectId , cmdData.placeAfter);
				break;

				case "UpdateVisibility":
					command = new UpdateVisibilityCommand(cmdData.objectId , cmdData.visibility);
				break;

				case "UpdateMask":
					command = new UpdateMaskCommand(cmdData.objectId , cmdData.maskTill);
				break;
			}

			if(command !== undefined) {
				command.execute(this, resourceManager);
			}
		}

		if (this.repeatFlag !== false) {
			this.cleanup(commands);
			this.repeatFlag = false;
		}
	}

	MovieClip.prototype.getTimeline = function () {
		return this.m_timeline;
	}

	//PlaceObjectCommand Class
	var PlaceObjectCommand = function(charID, objectID, placeAfter, transform)
	{
		this.m_charID = charID;
		this.m_objectID = objectID;
		this.m_placeAfter = placeAfter;
		this.m_transform = transform;
	}

	//Execute function for PlaceObjectCommand
	PlaceObjectCommand.prototype.execute = function(stage, resourceManager)
	{

		var shape = resourceManager.getShape(this.m_charID),
			bitmap = resourceManager.getBitmap(this.m_charID),
			text = resourceManager.getText(this.m_charID),
			parentMC = stage.el,
			root = stage.root,
			movieclipTimeline,
			movieclip,
			children,
			i,
			childMC,
			index;

		if(shape !== null && shape !== undefined)
		{
			Utils.CreateShape(root, parentMC, resourceManager, this.m_charID, this.m_objectID, this.m_placeAfter, this.m_transform);
		}
		else if(bitmap !== null && bitmap !== undefined)
		{
			Utils.CreateBitmap(root, parentMC, resourceManager, this.m_charID, this.m_objectID, this.m_placeAfter, this.m_transform);
		}
		else if (text !== null && text !== undefined)
		{
			Utils.CreateText(root, parentMC, resourceManager, this.m_charID, this.m_objectID, this.m_placeAfter, this.m_transform);
		}
		else
		{
			// Movie clip logic starts here
			movieclipTimeline = resourceManager.getMovieClip(this.m_charID);

			if(parentMC != undefined)
			{
				//if already exists do not add
				if (parentMC.select('[token="' + this.m_objectID + '"]')) {
					//run move in case different
					var command = new MoveObjectCommand(this.m_objectID, this.m_transform);
					command.execute(stage, resourceManager);
					return;
				}

				//Create a  MC
				childMC = parentMC.g();
				childMC.attr({class: 'movieclip', token: this.m_objectID});

				if(this.m_placeAfter != 0)
				{
					children = parentMC.selectAll('[token="' + this.m_placeAfter + '"]');
					for (i = 0; i < children.length; i += 1) {
						if (children[i].parent().id == parentMC.id) { //ensure child of current movie clip
							children[i].before(childMC);
							break;
						}
					}
				}
				else
				{
					parentMC.add(childMC);
				}

				//Create a corresponding TimelineAnimator
				if(movieclipTimeline)
				{
					movieclip = new MovieClip(root, childMC, movieclipTimeline, resourceManager, this.m_transform);
					stage.m_children.push(movieclip);

					var tl = movieclip.getTimeline();
					tl.play();
				}
			}
		}

	}

	//MoveObjectCommand Class
	var MoveObjectCommand = function(objectID, transform)
	{
		this.m_objectID = objectID;
		//this.m_placeAfter = placeAfter;
		this.m_transform = transform;
	}

	//Execute function for PlaceObjectCommand
	MoveObjectCommand.prototype.execute = function(stage, resourceManager)
	{
		var parentMC,
			transform,
			transformArray,
			transformMat,
			children,
			i;

		parentMC = stage.el;
		transform =  this.m_transform;
		transformArray = transform.split(",");
		transformMat = new Snap.Matrix(transformArray[0],transformArray[1],transformArray[2],transformArray[3],transformArray[4],transformArray[5]);

		if(parentMC != undefined)
		{
			children = parentMC.selectAll('[token="' + this.m_objectID + '"]');

			for (i = 0; i < children.length; i += 1) {
				if (children[i].parent().id == parentMC.id) { //ensure child of current movie clip
					children[i].transform(transformMat.toTransformString());
				}
			}
		}

	}

	//UpdateObjectCommand Class
	var UpdateObjectCommand = function(objectID, placeAfter)
	{
		this.m_objectID = objectID;
		this.m_placeAfter = placeAfter;
	}

	//Execute function for UpdateObjectCommand
	UpdateObjectCommand.prototype.execute = function(timelineAnimator, resourceManager)
	{

		/*
		var parentMC = timelineAnimator.s;

		if(parentMC != undefined)
		{
			//Change the Z order of the targetMC
			var index;
			for(var indexz=0; indexz<parentMC.children.length; indexz++)
			{
				if(parentMC.children[indexz].id == parseInt(this.m_objectID))
				{
					var child = parentMC.getChildAt(indexz);
					if(this.m_placeAfter != 0)
					{
						for(var index=0; index<parentMC.children.length; index++)
						{
							if(parentMC.children[index].id == parseInt(this.m_placeAfter))
							{
								//child.addChildAt(childMC,index);
								parentMC.setChildIndex(child,index - 1)
								break;
							}
						}
					}
					break;
				}
			}
		}
		*/
	}

	//RemoveObjectCommand Class
	var RemoveObjectCommand = function(objectID)
	{
		this.m_objectID = objectID;
	}

	//Execute function for RemoveObjectCommand
	RemoveObjectCommand.prototype.execute = function(stage, resourceManager)
	{
		var parentMC = stage.el,
			child;

		if(parentMC != undefined)
		{
			children = parentMC.selectAll('[token="' + this.m_objectID + '"]');

			for (i = 0; i < children.length; i += 1) {
				if (children[i].parent() == parentMC) { //ensure child of current movie clip
					children[i].remove();
				}
			}
		}
	}

	//UpdateVisbilityCommand Class
	var UpdateVisibilityCommand = function(objectID,visibility)
	{
		this.m_objectID = objectID;
		this.m_visibilty = visibility;
	}

	//Execute function for UpdateVisbilityCommand
	UpdateVisibilityCommand.prototype.execute = function(timelineAnimator, resourceManager)
	{
		/*
		var parentMC = timelineAnimator.m_targetMC;
		if(parentMC != undefined)
		{
			//Remove the targetMC
			var index,visibleBool;
			for(var index=0; index<parentMC.children.length; index++)
			{
				if(parentMC.children[index].id == parseInt(this.m_objectID))
				{
					if(this.m_visibilty == "true")
						visibleBool = true;
						else
						visibleBool = false;

					parentMC.getChildAt(index).visible = visibleBool;

				}
			}
		}
		*/
	}

	var UpdateMaskCommand = function (objectID,maskTill)
	{
		this.m_objectID = objectID;
		this.m_maskTill = maskTill;
	}

	UpdateMaskCommand.prototype.execute = function (stage, resourceManager)
	{
		var parentMC = stage.el,
			mask,
			masked,
			i,
			def,
			clone;

		if(parentMC != undefined)
		{
			baseMask = parentMC.select('[token="' + this.m_objectID + '"]');
			def = baseMask.toDefs();

			for (i = parseInt(this.m_maskTill); i > parseInt(this.m_objectID); i -= 1) {
				clone = def.clone(); //issue with reusing def ??
				masked = parentMC.select('[token="' + i + '"]');
				masked.attr({mask: clone});
			}
		}
	}

	return MovieClip;

});<|endoftext|>


<commit_before>'use strict';

var Hapi = require('hapi'),
		config = require('./config'),
		indexRoutes = require('./src/api/index/indexRoutes');

var options = {
	cors: true,
	cache: {
		engine: 'redis',
		partition: 'cm'
	}
};

var server = Hapi.createServer('0.0.0.0', config.port, options);
<% if (!!npmDependencies.flod) {%>
var Flod = require('flod');
var probe = new Flod.Probe(server, {server: 'hapi', version: '<%= npmDependencies.hapi%>'});
<%}%>

server.route(indexRoutes);

server.start();
console.log('Hapi server running in port ' + config.port);

module.exports = server;<commit_msg>Update server.js in hapi generator for the hapi-util<commit_after>'use strict';

var Hapi = require('hapi');
var config = require('./config');
var indexRoutes = require('./src/api/index/indexRoutes');
//mk:require

var options = {
	cors: true,
	cache: {
		engine: 'redis',
		partition: 'cm'
	}
};

var server = Hapi.createServer('0.0.0.0', config.port, options);
<% if (!!npmDependencies.flod) {%>
var Flod = require('flod');
var probe = new Flod.Probe(server, {server: 'hapi', version: '<%= npmDependencies.hapi%>'});
<%}%>

server.route(indexRoutes);
//mk:route

server.start();
console.log('Hapi server running in port ' + config.port);

module.exports = server;<|endoftext|>


<commit_before>import config from '../../config';
import api    from './api';
import mongo  from '../../libs/mongo';
import async  from 'async';

let c = mongo.collections;

export default {

    /**
     * API
     *
     * Standard AGAVE API Interactions
     */
    api: api,

    // compound API interactions ------------------------------------------------

    /**
     * Get Outputs
     *
     * Takes a Job ID and calls back with an
     * array of of results/outputs.
     */
    getOutputs (jobId, callback) {
        this.getLogs(jobId, (logs, status) => {
            this.getResults(jobId, (results) => {
                callback(results, logs, status);
            });
        });
    },

    /**
     * Get Logs
     */
    getLogs(jobId, callback) {
        let logs = [];
        let status = null;
        api.getJobLogs(jobId, (err, res) => {
            // get main logs files
            if (res.body.result) {
                async.each(res.body.result, (file, cb) => {
                    if (file.type === 'file') {
                        logs.push(file);
                        if (file.name === 'exit_code.txt') {
                            let path = file._links.self.href;
                            api.getPath(path, (err, res) => {
                                status = res.body;
                                cb();
                            });
                        } else {cb();}
                    }
                }, () => {
                    callback(logs, status);
                });
            }
        });
    },

    /**
     * Get Results
     */
    getResults (jobId, callback) {
        getDir(jobId, '/out', (results) => {
            results = results.length > 0 ? results : null;
            callback(results);
        });

        function getDir(jobId, dirPath, callback) {
            let results = [];
            api.getPath('jobs/v2/' + jobId + '/outputs/listings' + dirPath, (err, res) => {
                if (res.body.result) {
                    async.each(res.body.result, (result, cb) => {
                        if (result.type === 'file') {
                            results.push(result);
                            cb();
                        } else if (result.type === 'dir') {
                            getDir(jobId, result.path, (subResults) => {
                                result.children = subResults;
                                results.push(result);
                                cb();
                            });
                        }
                    }, () => {
                        callback(results);
                    });
                } else {
                    callback(results);
                }
            });
        }
    },

    /**
     * Submit Job
     *
     * Takes a job definition and callsback
     * with job submission results.
     */
    submitJob (job, callback) {
        let attempts = job.attempts ? job.attempts + 1: 1;

        // form job body
        let body = {
            name:              'crn-automated-job',
            appId:             job.appId,
            batchQueue:        job.batchQueue,
            executionSystem:   job.executionSystem,
            maxRunTime:        '05:00:00',
            memoryPerNode:     job.memoryPerNode,
            nodeCount:         job.nodeCount,
            processorsPerNode: job.processorsPerNode,
            archive:           true,
            archiveSystem:     'openfmri-archive',
            archivePath:       null,
            inputs: {},
            parameters: job.parameters ? job.parameters : {},
            notifications: [
                {
                    url:        config.url + config.apiPrefix +  'jobs/${JOB_ID}/results',
                    event:      '*',
                    persistent: true
                }
            ]
        };

        // set input
        let inputKey = job.input ? job.input : 'bidsFolder';
        body.inputs[inputKey] = config.agave.storage + job.datasetHash;

        // submit job
        api.createJob(body, (err, resp) => {

            // handle submission errors
            if (resp.body.status == 'error') {
                let error = new Error(resp.body.message);
                error.http_code = 400;
                return callback(error, null);
            }
            if (resp.statusCode !== 200 && resp.statusCode !== 201) {
                let error = new Error(resp.body ? resp.body : 'AGAVE was unable to process this job submission.');
                error.http_code = resp.statusCode ? resp.statusCode : 503;
                return callback(error, null);
            }

            // store job
            c.jobs.insertOne({
                jobId:             resp.body.result.id,
                agave:             resp.body.result,

                appId:             body.appId,
                parameters:        body.parameters,
                memoryPerNode:     body.memoryPerNode,
                nodeCount:         body.nodeCount,
                processorsPerNode: body.processorsPerNode,
                batchQueue:        body.batchQueue,

                appLabel:          job.appLabel,
                appVersion:        job.appVersion,
                datasetHash:       job.datasetHash,
                datasetId:         job.datasetId,
                datasetLabel:      job.datasetLabel,
                userId:            job.userId,
                parametersHash:    job.parametersHash,
                snapshotId:        job.snapshotId,

                attempts:          attempts
            }, () => {
                callback(null, resp.body);
            });
        });
    }

};<commit_msg>Handle logs not found<commit_after>import config from '../../config';
import api    from './api';
import mongo  from '../../libs/mongo';
import async  from 'async';

let c = mongo.collections;

export default {

    /**
     * API
     *
     * Standard AGAVE API Interactions
     */
    api: api,

    // compound API interactions ------------------------------------------------

    /**
     * Get Outputs
     *
     * Takes a Job ID and calls back with an
     * array of of results/outputs.
     */
    getOutputs (jobId, callback) {
        this.getLogs(jobId, (logs, status) => {
            this.getResults(jobId, (results) => {
                callback(results, logs, status);
            });
        });
    },

    /**
     * Get Logs
     */
    getLogs(jobId, callback) {
        let logs = [];
        let status = null;
        api.getJobLogs(jobId, (err, res) => {
            // get main logs files
            if (res.body.result) {
                async.each(res.body.result, (file, cb) => {
                    if (file.type === 'file') {
                        logs.push(file);
                        if (file.name === 'exit_code.txt') {
                            let path = file._links.self.href;
                            api.getPath(path, (err, res) => {
                                status = res.body;
                                cb();
                            });
                        } else {cb();}
                    }
                }, () => {
                    callback(logs, status);
                });
            } else {
                callback(logs, status);
            }
        });
    },

    /**
     * Get Results
     */
    getResults (jobId, callback) {
        getDir(jobId, '/out', (results) => {
            results = results.length > 0 ? results : null;
            callback(results);
        });

        function getDir(jobId, dirPath, callback) {
            let results = [];
            api.getPath('jobs/v2/' + jobId + '/outputs/listings' + dirPath, (err, res) => {
                if (res.body.result) {
                    async.each(res.body.result, (result, cb) => {
                        if (result.type === 'file') {
                            results.push(result);
                            cb();
                        } else if (result.type === 'dir') {
                            getDir(jobId, result.path, (subResults) => {
                                result.children = subResults;
                                results.push(result);
                                cb();
                            });
                        }
                    }, () => {
                        callback(results);
                    });
                } else {
                    callback(results);
                }
            });
        }
    },

    /**
     * Submit Job
     *
     * Takes a job definition and callsback
     * with job submission results.
     */
    submitJob (job, callback) {
        let attempts = job.attempts ? job.attempts + 1: 1;

        // form job body
        let body = {
            name:              'crn-automated-job',
            appId:             job.appId,
            batchQueue:        job.batchQueue,
            executionSystem:   job.executionSystem,
            maxRunTime:        '05:00:00',
            memoryPerNode:     job.memoryPerNode,
            nodeCount:         job.nodeCount,
            processorsPerNode: job.processorsPerNode,
            archive:           true,
            archiveSystem:     'openfmri-archive',
            archivePath:       null,
            inputs: {},
            parameters: job.parameters ? job.parameters : {},
            notifications: [
                {
                    url:        config.url + config.apiPrefix +  'jobs/${JOB_ID}/results',
                    event:      '*',
                    persistent: true
                }
            ]
        };

        // set input
        let inputKey = job.input ? job.input : 'bidsFolder';
        body.inputs[inputKey] = config.agave.storage + job.datasetHash;

        // submit job
        api.createJob(body, (err, resp) => {

            // handle submission errors
            if (resp.body.status == 'error') {
                let error = new Error(resp.body.message);
                error.http_code = 400;
                return callback(error, null);
            }
            if (resp.statusCode !== 200 && resp.statusCode !== 201) {
                let error = new Error(resp.body ? resp.body : 'AGAVE was unable to process this job submission.');
                error.http_code = resp.statusCode ? resp.statusCode : 503;
                return callback(error, null);
            }

            // store job
            c.jobs.insertOne({
                jobId:             resp.body.result.id,
                agave:             resp.body.result,

                appId:             body.appId,
                parameters:        body.parameters,
                memoryPerNode:     body.memoryPerNode,
                nodeCount:         body.nodeCount,
                processorsPerNode: body.processorsPerNode,
                batchQueue:        body.batchQueue,

                appLabel:          job.appLabel,
                appVersion:        job.appVersion,
                datasetHash:       job.datasetHash,
                datasetId:         job.datasetId,
                datasetLabel:      job.datasetLabel,
                userId:            job.userId,
                parametersHash:    job.parametersHash,
                snapshotId:        job.snapshotId,

                attempts:          attempts
            }, () => {
                callback(null, resp.body);
            });
        });
    }

};<|endoftext|>