plugins {
  id "org.implab.gradle-typescript" version "1.3.4"
  id "org.implab.gradle-mercurial" version "1.0.1"
}

if (!symbols in ['local', 'pack', 'none'])
    throw new Exception("The symbols property value is invalid: $symbols");

if (!flavour in ['browser', 'node'])
    throw new Exception("The flavour property value is invalid: $flavour");

ext {
    packageName = flavour == 'browser' ? "@$npmScope/$name-amd" : "@$npmScope/$name"
    lint = project.hasProperty('lint') ? project.lint ?: true : false
}

mercurial {
    preReleasePolicy { it
        .addPatch(versionDistance)
        .withPreRelease('dev')
        .withMeta(changeset)
    }

    applyVersioningPolicy()
}

typescript {
    compilerOptions {
        types = []
        declaration = true
        experimentalDecorators = true
        strict = true
        module = "nodenext"
        target = "ESNext"
    }
    tscCmd = "$projectDir/node_modules/.bin/tsc"
    tsLintCmd = "$projectDir/node_modules/.bin/tslint"
	esLintCmd = "$projectDir/node_modules/.bin/eslint"
}

tasks.matching{ it.name =~ /^lint/ }.configureEach {
    onlyIf { lint }
}

if (symbols == 'local') {
    tasks.matching{ it.name =~ /^configureTs/ }.configureEach {
        compilerOptions {
            sourceRoot = "file://" + it.rootDir
        }
    }
}

task packSources {
    
}

printVersion {
    doLast {
        println "packageName: $packageName";
        println "flavour: $flavour";
        println "target: $typescript.compilerOptions.target";
        println "module: $typescript.compilerOptions.module";
        println "lint: $lint";
        println "symbols: $symbols";
    }
}

test {
    commandLine "npx", "mocha", "${->typescript.testDir.get()}";
}