init.js 7.7 KB

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