|
| 1 | +///<reference path="../.d.ts"/> |
| 2 | + |
| 3 | +"use strict"; |
| 4 | + |
| 5 | +import * as path from 'path'; |
| 6 | +import * as util from 'util'; |
| 7 | +import {TESTING_FRAMEWORKS} from '../constants'; |
| 8 | + |
| 9 | +class TestInitCommand implements ICommand { |
| 10 | + constructor(private $npm: INodePackageManager, |
| 11 | + private $projectData: IProjectData, |
| 12 | + private $errors: IErrors, |
| 13 | + private $options: IOptions, |
| 14 | + private $prompter: IPrompter, |
| 15 | + private $fs: IFileSystem, |
| 16 | + private $resources: IResourceLoader, |
| 17 | + private $pluginsService: IPluginsService, |
| 18 | + private $logger: ILogger) { |
| 19 | + } |
| 20 | + |
| 21 | + private frameworkDependencies:IDictionary<string[]> = { |
| 22 | + jasmine: [], |
| 23 | + mocha: ['chai'], |
| 24 | + }; |
| 25 | + |
| 26 | + public execute(args: string[]) : IFuture<void> { |
| 27 | + return (() => { |
| 28 | + let projectDir = this.$projectData.projectDir; |
| 29 | + |
| 30 | + let frameworkToInstall = this.$options.framework |
| 31 | + || this.$prompter.promptForChoice('Select testing framework:', TESTING_FRAMEWORKS).wait(); |
| 32 | + if (TESTING_FRAMEWORKS.indexOf(frameworkToInstall) === -1) { |
| 33 | + this.$errors.fail(`Unknown or unsupported unit testing framework: ${frameworkToInstall}`); |
| 34 | + } |
| 35 | + |
| 36 | + ['karma', 'karma-' + frameworkToInstall, 'karma-nativescript-launcher'] |
| 37 | + .concat(this.frameworkDependencies[frameworkToInstall].map(f => 'karma-' + f)) |
| 38 | + .forEach(mod => { |
| 39 | + this.$npm.install(mod, projectDir, { |
| 40 | + 'save-dev': true, |
| 41 | + optional: false, |
| 42 | + }).wait(); |
| 43 | + }); |
| 44 | + |
| 45 | + this.$pluginsService.add('nativescript-unit-test-runner').wait(); |
| 46 | + |
| 47 | + let testsDir = path.join(projectDir, 'app/tests'); |
| 48 | + let shouldCreateSampleTests = true; |
| 49 | + if (this.$fs.exists(testsDir).wait()) { |
| 50 | + this.$logger.info('app/tests/ directory already exists, will not create an example test project.'); |
| 51 | + shouldCreateSampleTests = false; |
| 52 | + } |
| 53 | + |
| 54 | + this.$fs.ensureDirectoryExists(testsDir).wait(); |
| 55 | + |
| 56 | + let karmaConfTemplate = this.$resources.readText('test/karma.conf.js').wait(); |
| 57 | + let karmaConf = _.template(karmaConfTemplate)({ |
| 58 | + frameworks: [frameworkToInstall].concat(this.frameworkDependencies[frameworkToInstall]) |
| 59 | + .map(fw => `'${fw}'`) |
| 60 | + .join(', ') |
| 61 | + }); |
| 62 | + |
| 63 | + this.$fs.writeFile(path.join(projectDir, 'karma.conf.js'), karmaConf).wait(); |
| 64 | + |
| 65 | + let exampleFilePath = this.$resources.resolvePath(util.format('test/example.%s.js', frameworkToInstall)); |
| 66 | + |
| 67 | + if (shouldCreateSampleTests && this.$fs.exists(exampleFilePath).wait()) { |
| 68 | + this.$fs.copyFile(exampleFilePath, path.join(testsDir, 'example.js')).wait(); |
| 69 | + this.$logger.info('\nExample test file created in app/tests/'.yellow); |
| 70 | + } else { |
| 71 | + this.$logger.info('\nPlace your test files under app/tests/'.yellow); |
| 72 | + } |
| 73 | + |
| 74 | + this.$logger.info('Run your tests using the "$ tns test <platform>" command.'.yellow); |
| 75 | + }).future<void>()(); |
| 76 | + } |
| 77 | + |
| 78 | + allowedParameters: ICommandParameter[] = []; |
| 79 | +} |
| 80 | +$injector.registerCommand("test|init", TestInitCommand); |
0 commit comments