const { series, crossEnv, concurrent, rimraf, runInNewWindow } = require('nps-utils');

module.exports = {
    scripts: {
        /**
         * Starts the builded app from the dist directory
         */
        start: {
            script: 'node dist/app.js'
        },
        /**
         * Serves the current app and watches for changes to restart it
         */
        serve: {
            script: series(
                'nps banner.serve',
                '\"./node_modules/.bin/nodemon\" --watch src --watch .env'
            )
        },
        /**
         * Setup's the development environment and the database
         */
        setup: {
            script: series(
                'yarn install',
                'nps db.migrate',
                'nps db.seed'
            )
        },
        /**
         * Builds the app into the dist directory
         */
        build: {
            script: series(
                'nps banner.build',
                'nps lint',
                'nps clean.dist',
                'nps transpile',
                'nps copy'
            )
        },
        /**
         * These run various kinds of tests. Default is unit.
         */
        test: {
            default: 'nps test.unit',
            unit: {
                default: {
                    script: series(
                        'nps banner.test',
                        'nps test.unit.pretest',
                        'nps test.unit.run'
                    )
                },
                pretest: {
                    script: './node_modules/.bin/tslint -c ./tslint.json -t stylish "./test/unit/**/*.ts"'
                },
                run: {
                    script: './node_modules/.bin/cross-env NODE_ENV=test \"./node_modules/.bin/jest\" --testPathPattern=unit'
                },
                verbose: {
                    script: 'nps "test --verbose"'
                },
                coverage: {
                    script: 'nps "test --coverage"'
                },
            },
            e2e: {
                default: {
                    script: series(
                        'nps banner.test',
                        'nps test.e2e.pretest',
                        runInNewWindow(series('nps build', 'nps start')),
                        'nps test.e2e.run'
                    )
                },
                pretest: {
                    script: './node_modules/.bin/tslint -c ./tslint.json -t stylish "./test/e2e/**/*.ts"'
                },
                verbose: {
                    script: 'nps "test.e2e --verbose"'
                },
                run: series(
                    `wait-on --timeout 120000 http-get://localhost:3000/api/info`,
                    './node_modules/.bin/cross-env NODE_ENV=test \"./node_modules/.bin/jest\" --testPathPattern=e2e -i'
                ),
            }
        },
        /**
         * Runs TSLint over your project
         */
        lint: {
            script: `./node_modules/.bin/tslint -c ./tslint.json -p tsconfig.json 'src/**/*.ts' --format stylish`
        },
        /**
         * Transpile your app into javascript
         */
        transpile: {
            script: `./node_modules/.bin/tsc`
        },
        /**
         * Clean files and folders
         */
        clean: {
            default: {
                script: series(`nps banner.clean`, `nps clean.dist`)
            },
            dist: {
                script: `./node_modules/.bin/trash './dist'`
            }
        },
        /**
         * Copies static files to the build folder
         */
        copy: {
            default: {
                script: `nps copy.swagger && nps copy.public`
            },
            swagger: {
                script: copy('./src/api/swagger.json', './dist')
            },
            public: {
                script: copy('./src/public/*', './dist')
            }
        },
        /**
         * This our scaffold api
         * @example > nps "console update:targets"
         */
        console: {
            default: {
                script: runFast('./src/console/lib/console.ts')
            },
            dev: run('./src/console/lib/console.ts'),
            help: runFast('./src/console/lib/console.ts --help')
        },
        /**
         * All database related scripts are here
         */
        db: {
            migrate: {
                default: {
                    script: series('nps banner.migrate', '\"./node_modules/.bin/knex\" migrate:latest')
                },
                rollback: series('nps banner.rollback', '\"./node_modules/.bin/knex\" migrate:rollback')
            },
            seed: {
                script: series('nps banner.seed', '\"./node_modules/.bin/knex\" seed:run')
            },
            reset: {
                script: series('nps banner.dbReset', 'nps console db:reset')
            }
        },
        /**
         * This creates pretty banner to the terminal
         */
        banner: {
            build: banner('build'),
            serve: banner('serve'),
            test: banner('test'),
            migrate: banner('db.migrate'),
            rollback: banner('db.migrate.rollback'),
            dbReset: banner('db.reset'),
            seed: banner('db.seed'),
            clean: banner('clean')
        }
    }
};

function banner(name) {
    return {
        hiddenFromHelp: true,
        silent: true,
        logLevel: 'error',
        description: `Shows ${name} banners to the console`,
        script: runFast(`./src/console/lib/banner.ts ${name}`)
    }
}

function copy(source, target) {
    return `./node_modules/.bin/copyup ${source} ${target}`
}

function run(path) {
    return `./node_modules/.bin/ts-node ${path}`
}

function runFast(path) {
    return run(`-F ${path}`)
}

@@ -1545,32 +1545,34 @@
        script: '
+%5C%22
 ./node_modules/.
@@ -1573,32 +1573,34 @@
 ules/.bin/tslint
+%5C%22
  -c ./tslint.jso
@@ -1698,32 +1698,34 @@
        script: '
+%5C%22
 ./node_modules/.
@@ -1729,32 +1729,34 @@
 s/.bin/cross-env
+%5C%22
  NODE_ENV=test %5C
@@ -2424,16 +2424,18 @@
 cript: '
+%5C%22
 ./node_m
@@ -2444,32 +2444,34 @@
 ules/.bin/tslint
+%5C%22
  -c ./tslint.jso
@@ -2758,16 +2758,18 @@
        '
+%5C%22
 ./node_m
@@ -2789,16 +2789,18 @@
 ross-env
+%5C%22
  NODE_EN
@@ -2999,32 +2999,33 @@
        script: %60
+%22
 ./node_modules/.
@@ -3034,16 +3034,17 @@
 n/tslint
+%22
  -c ./ts
@@ -3217,32 +3217,33 @@
        script: %60
+%22
 ./node_modules/.
@@ -3249,16 +3249,17 @@
 .bin/tsc
+%22
 %60%0A      
@@ -3491,16 +3491,17 @@
 cript: %60
+%22
 ./node_m
@@ -3517,16 +3517,17 @@
 in/trash
+%22
  './dist
@@ -5656,32 +5656,33 @@
 ) %7B%0A    return %60
+%22
 ./node_modules/.
@@ -5691,16 +5691,17 @@
 n/copyup
+%22
  $%7Bsourc
@@ -5750,16 +5750,17 @@
 return %60
+%22
 ./node_m
@@ -5778,16 +5778,17 @@
 /ts-node
+%22
  $%7Bpath%7D

Fix nps script problems on windows