init.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. 'use strict'
  2. const readline = require('readline')
  3. const path = require('path')
  4. const glob = require('glob')
  5. const mm = require('minimatch')
  6. const exec = require('child_process').exec
  7. const helper = require('./helper')
  8. const logger = require('./logger')
  9. const log = logger.create('init')
  10. const logQueue = require('./init/log-queue')
  11. const StateMachine = require('./init/state_machine')
  12. const COLOR_SCHEME = require('./init/color_schemes')
  13. const formatters = require('./init/formatters')
  14. // TODO(vojta): coverage
  15. // TODO(vojta): html preprocessors
  16. // TODO(vojta): SauceLabs
  17. // TODO(vojta): BrowserStack
  18. var NODE_MODULES_DIR = path.resolve(__dirname, '../..')
  19. // Karma is not in node_modules, probably a symlink,
  20. // use current working dir.
  21. if (!/node_modules$/.test(NODE_MODULES_DIR)) {
  22. NODE_MODULES_DIR = path.resolve('node_modules')
  23. }
  24. function installPackage (pkgName) {
  25. // Do not install if already installed.
  26. try {
  27. require(NODE_MODULES_DIR + '/' + pkgName)
  28. return
  29. } catch (e) {}
  30. log.debug('Missing plugin "%s". Installing...', pkgName)
  31. const options = {
  32. cwd: path.resolve(NODE_MODULES_DIR, '..')
  33. }
  34. exec('npm install ' + pkgName + ' --save-dev', options, function (err, stdout, stderr) {
  35. // Put the logs into the queue and print them after answering current question.
  36. // Otherwise the log would clobber the interactive terminal.
  37. logQueue.push(function () {
  38. if (!err) {
  39. log.debug('%s successfully installed.', pkgName)
  40. } else if (/is not in the npm registry/.test(stderr)) {
  41. log.warn('Failed to install "%s". It is not in the NPM registry!\n' +
  42. ' Please install it manually.', pkgName)
  43. } else if (/Error: EACCES/.test(stderr)) {
  44. log.warn('Failed to install "%s". No permissions to write in %s!\n' +
  45. ' Please install it manually.', pkgName, options.cwd)
  46. } else {
  47. log.warn('Failed to install "%s"\n Please install it manually.', pkgName)
  48. }
  49. })
  50. })
  51. }
  52. function validatePattern (pattern) {
  53. if (!glob.sync(pattern).length) {
  54. log.warn('There is no file matching this pattern.\n')
  55. }
  56. }
  57. function validateBrowser (name) {
  58. // TODO(vojta): check if the path resolves to a binary
  59. installPackage('karma-' + name.toLowerCase().replace('canary', '') + '-launcher')
  60. }
  61. function validateFramework (name) {
  62. installPackage('karma-' + name)
  63. }
  64. function validateRequireJs (useRequire) {
  65. if (useRequire) {
  66. validateFramework('requirejs')
  67. }
  68. }
  69. var questions = [{
  70. id: 'framework',
  71. question: 'Which testing framework do you want to use ?',
  72. hint: 'Press tab to list possible options. Enter to move to the next question.',
  73. options: ['jasmine', 'mocha', 'qunit', 'nodeunit', 'nunit', ''],
  74. validate: validateFramework
  75. }, {
  76. id: 'requirejs',
  77. question: 'Do you want to use Require.js ?',
  78. hint: 'This will add Require.js plugin.\n' +
  79. 'Press tab to list possible options. Enter to move to the next question.',
  80. options: ['no', 'yes'],
  81. validate: validateRequireJs,
  82. boolean: true
  83. }, {
  84. id: 'browsers',
  85. question: 'Do you want to capture any browsers automatically ?',
  86. hint: 'Press tab to list possible options. Enter empty string to move to the next question.',
  87. options: ['Chrome', 'ChromeCanary', 'Firefox', 'Safari', 'PhantomJS', 'Opera', 'IE', ''],
  88. validate: validateBrowser,
  89. multiple: true
  90. }, {
  91. id: 'files',
  92. question: 'What is the location of your source and test files ?',
  93. hint: 'You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".\n' +
  94. 'Enter empty string to move to the next question.',
  95. multiple: true,
  96. validate: validatePattern
  97. }, {
  98. id: 'exclude',
  99. question: 'Should any of the files included by the previous patterns be excluded ?',
  100. hint: 'You can use glob patterns, eg. "**/*.swp".\n' +
  101. 'Enter empty string to move to the next question.',
  102. multiple: true,
  103. validate: validatePattern
  104. }, {
  105. id: 'generateTestMain',
  106. question: 'Do you wanna generate a bootstrap file for RequireJS?',
  107. hint: 'This will generate test-main.js/coffee that configures RequireJS and starts the tests.',
  108. options: ['no', 'yes'],
  109. boolean: true,
  110. condition: (answers) => answers.requirejs
  111. }, {
  112. id: 'includedFiles',
  113. question: 'Which files do you want to include with <script> tag ?',
  114. hint: 'This should be a script that bootstraps your test by configuring Require.js and ' +
  115. 'kicking __karma__.start(), probably your test-main.js file.\n' +
  116. 'Enter empty string to move to the next question.',
  117. multiple: true,
  118. validate: validatePattern,
  119. condition: (answers) => answers.requirejs && !answers.generateTestMain
  120. }, {
  121. id: 'autoWatch',
  122. question: 'Do you want Karma to watch all the files and run the tests on change ?',
  123. hint: 'Press tab to list possible options.',
  124. options: ['yes', 'no'],
  125. boolean: true
  126. }]
  127. function getBasePath (configFilePath, cwd) {
  128. const configParts = path.dirname(configFilePath).split(path.sep)
  129. const cwdParts = cwd.split(path.sep)
  130. const base = []
  131. while (configParts.length && configParts[0] === cwdParts[0]) {
  132. configParts.shift()
  133. cwdParts.shift()
  134. }
  135. while (configParts.length) {
  136. const part = configParts.shift()
  137. if (part === '..') {
  138. base.unshift(cwdParts.pop())
  139. } else if (part !== '.') {
  140. base.unshift('..')
  141. }
  142. }
  143. return base.join(path.sep)
  144. }
  145. function processAnswers (answers, basePath, testMainFile) {
  146. const processedAnswers = {
  147. basePath: basePath,
  148. files: answers.files,
  149. onlyServedFiles: [],
  150. exclude: answers.exclude,
  151. autoWatch: answers.autoWatch,
  152. generateTestMain: answers.generateTestMain,
  153. browsers: answers.browsers,
  154. frameworks: [],
  155. preprocessors: {}
  156. }
  157. if (answers.framework) {
  158. processedAnswers.frameworks.push(answers.framework)
  159. }
  160. if (answers.requirejs) {
  161. processedAnswers.frameworks.push('requirejs')
  162. processedAnswers.files = answers.includedFiles || []
  163. processedAnswers.onlyServedFiles = answers.files
  164. if (answers.generateTestMain) {
  165. processedAnswers.files.push(testMainFile)
  166. }
  167. }
  168. const allPatterns = answers.files.concat(answers.includedFiles || [])
  169. if (allPatterns.some((pattern) => mm(pattern, '**/*.coffee'))) {
  170. installPackage('karma-coffee-preprocessor')
  171. processedAnswers.preprocessors['**/*.coffee'] = ['coffee']
  172. }
  173. return processedAnswers
  174. }
  175. exports.init = function (config) {
  176. logger.setupFromConfig(config)
  177. const colorScheme = !helper.isDefined(config.colors) || config.colors ? COLOR_SCHEME.ON : COLOR_SCHEME.OFF
  178. // need to be registered before creating readlineInterface
  179. process.stdin.on('keypress', function (s, key) {
  180. sm.onKeypress(key)
  181. })
  182. const rli = readline.createInterface(process.stdin, process.stdout)
  183. const sm = new StateMachine(rli, colorScheme)
  184. rli.on('line', sm.onLine.bind(sm))
  185. // clean colors
  186. rli.on('SIGINT', function () {
  187. sm.kill()
  188. process.exit(0)
  189. })
  190. sm.process(questions, function (answers) {
  191. const cwd = process.cwd()
  192. const configFile = config.configFile || 'karma.conf.js'
  193. const isCoffee = path.extname(configFile) === '.coffee'
  194. const testMainFile = isCoffee ? 'test-main.coffee' : 'test-main.js'
  195. const formatter = formatters.createForPath(configFile)
  196. const processedAnswers = processAnswers(answers, getBasePath(configFile, cwd), testMainFile)
  197. const configFilePath = path.resolve(cwd, configFile)
  198. const testMainFilePath = path.resolve(cwd, testMainFile)
  199. if (isCoffee) {
  200. installPackage('coffee-script')
  201. }
  202. if (processedAnswers.generateTestMain) {
  203. formatter.writeRequirejsConfigFile(testMainFilePath)
  204. console.log(colorScheme.success(
  205. 'RequireJS bootstrap file generated at "' + testMainFilePath + '".\n'
  206. ))
  207. }
  208. formatter.writeConfigFile(configFilePath, processedAnswers)
  209. console.log(colorScheme.success('Config file generated at "' + configFilePath + '".\n'))
  210. })
  211. }