/*

Siesta 5.1.0
Copyright(c) 2009-2018 Bryntum AB
https://bryntum.com/contact
https://bryntum.com/products/siesta/license

*/
/**
@class Siesta.Project.NodeJS
@extends Siesta.Project

Class, representing the NodeJS project.

For a getting start guide and manual, please refer to <a href="#!/guide/getting_started_browser">Siesta getting started in browser environment</a> guide.

Synopsys
========

    var siesta      = require('siesta-lite')

    var project     = new siesta.NodeJsHarness()

    project.configure({
        title               : 'Awesome Test Suite',

        autoCheckGlobals    : true,
        expectedGlobals     : [
            'Ext',
            'Sch'
        ],

        preload : [
        ]
    })


    project.plan(
        // simple string - url relative to project file
        'sanity.t.js',

        // test file descriptor with own configuration options
        {
            url     : 'basic.t.js',

            // replace `preload` option of project
            preload : [
                "../awesome-project-all.js"
            ]
        },

        // groups ("folders") of test files (possibly with own options)
        {
            group       : 'Sanity',

            autoCheckGlobals    : false,

            items       : [
                'data/crud.t.js',
                ...
            ]
        },
        ...
    )

    project.start()

Running the test suite in NodeJS
================================

To run the suite in NodeJS, launch the project javascript file:

    > node t/index.js


*/

!function () {

var fs            = require('fs')
var path          = require('path')

var glob          = require('glob')


Class('Siesta.Project.NodeJS', {

    isa         : Siesta.Project,

    does        : [
        Siesta.Util.Role.CanEscapeRegExp,
        Siesta.Launcher.Role.CanProcessArguments
    ],


    has : {
        /**
         * @cfg {Class} testClass The test class which will be used for creating test instances, defaults to {@link Siesta.Test.NodeJS}.
         * You can subclass {@link Siesta.Test.NodeJS} and provide a new class, please refer to the <a href="#!/guide/extending_test_clas">Extending test class</a> guide.
         *
         * This option can be also specified in the test file descriptor.
         */
        testClass               : Siesta.Test.NodeJS,

        baseDirectory           : null,

        contentManagerClass     : Siesta.Content.Manager.NodeJS,
        scopeProvider           : 'Scope.Provider.NodeJsEmbed',

        chdirToIndex            : true,

        ecmaModulesEnabled      : function () {
            return Boolean(require('module')._extensions[ '.mjs' ])
        }
    },


    methods : {

        getScopeProviderConfigFor : function (desc, launchId) {
            var config = this.SUPER(desc, launchId)

            config.requireFunc      = PROJECT_REQUIRE || require

            return config
        },


        startAnonymously : function () {
            var isLauncher          = typeof AS_LAUNCHER != 'undefined'
            var testFileGlob        = this.ecmaModulesEnabled ? "**/*.t.?(m)js" : "**/*.t.js"

            var res                 = this.processArguments(process.argv.slice(isLauncher ? 2 : 3))
            var argv                = res.argv
            var options             = res.options

            if (argv.length == 0 && (options.help || options.version)) {
                // shortcut to show the help/version text immediately, w/o scanning current directory
                this.start()
            } else {
                var globPattern         = argv[ 0 ] || options.glob || testFileGlob

                var files               = glob.sync(globPattern, { matchBase : true })
                var baseDir             = process.cwd()

                var me                  = this
                var items               = []

                files.forEach(function (file) {
                    var stat    = fs.statSync(file)

                    if (stat.isDirectory()) {
                        me.planDirectory(file, baseDir)
                    } else
                        items.push(file)
                })

                this.plan(items)

                this.start()
            }
        },


        /**
         * This method scans the given directory for test files (`*.t.js` by default)
         * and adds them to the project plan.
         *
         * @param {String} dirname The directory to scan for test files
         */
        planDirectory : function (dirname, baseDir) {
            baseDir                 = baseDir || dirname

            var testFileGlob        = this.ecmaModulesEnabled ? "**/*.t.?(m)js" : "**/*.t.js"

            var testsInDir          = glob.sync(testFileGlob, { cwd : dirname, matchBase : true })

            this.plan(
                testsInDir.map(function (testFile) {
                    return path.relative(baseDir, path.resolve(dirname, testFile))
                })
            )
        }
    }
})
//eof Siesta.Project.NodeJS


}()