installer: Namespace more installer classes
[mediawiki.git] / Gruntfile.js
blob39fc2ae73f1d2e809601e45a67faa06fc50551ce
1 'use strict';
2 module.exports = function ( grunt ) {
3         grunt.loadNpmTasks( 'grunt-banana-checker' );
4         grunt.loadNpmTasks( 'grunt-eslint' );
5         grunt.loadNpmTasks( 'grunt-karma' );
6         grunt.loadNpmTasks( 'grunt-stylelint' );
8         const fs = require( 'fs' );
9         const path = require( 'path' );
10         const wgServer = process.env.MW_SERVER;
11         const wgScriptPath = process.env.MW_SCRIPT_PATH;
12         const karmaProxy = {};
14         let qunitPattern = wgServer + wgScriptPath + '/index.php?title=Special:JavaScriptTest/qunit/export';
16         // "MediaWiki" for core, or extension/skin name (e.g. "GrowthExperiments")
17         const qunitComponent = grunt.option( 'qunit-component' );
18         const qunitWatch = grunt.option( 'qunit-watch' ) || false;
19         const qunitWatchFiles = [];
20         if ( qunitComponent ) {
21                 let qunitWatchSourcePattern;
22                 let qunitWatchTestPattern;
23                 qunitPattern = qunitPattern + '&component=' + qunitComponent;
24                 if ( qunitWatch ) {
25                         // Special-case MediaWiki core.
26                         if ( qunitComponent === 'MediaWiki' ) {
27                                 qunitWatchTestPattern = 'tests/qunit/**/*.js';
28                                 qunitWatchSourcePattern = 'resources/**/*.js';
29                         } else {
30                                 let settingsJson,
31                                         basePath;
32                                 try {
33                                         basePath = 'extensions';
34                                         // eslint-disable-next-line security/detect-non-literal-fs-filename
35                                         settingsJson = fs.readFileSync(
36                                                 path.resolve( __dirname + '/' + basePath + '/' + qunitComponent + '/extension.json' )
37                                         );
38                                 } catch ( e ) {
39                                         basePath = 'skins';
40                                         // eslint-disable-next-line security/detect-non-literal-fs-filename
41                                         settingsJson = fs.readFileSync(
42                                                 path.resolve( __dirname + '/' + basePath + '/' + qunitComponent + '/skin.json' )
43                                         );
44                                 }
45                                 settingsJson = JSON.parse( settingsJson );
46                                 qunitWatchSourcePattern =
47                                         path.resolve( __dirname + '/' + basePath + '/' + qunitComponent + '/' + settingsJson.ResourceFileModulePaths.localBasePath + '/**/*.js' );
48                                 qunitWatchTestPattern = path.resolve( __dirname + '/' + basePath + '/' + qunitComponent + '/tests/qunit/**/*.js' );
49                         }
50                         qunitWatchFiles.push( {
51                                 pattern: qunitWatchSourcePattern,
52                                 type: 'js',
53                                 watched: true,
54                                 included: false,
55                                 served: false
56                         } );
57                         qunitWatchFiles.push( {
58                                 pattern: qunitWatchTestPattern,
59                                 type: 'js',
60                                 watched: true,
61                                 included: false,
62                                 served: false
63                         } );
64                 }
65         }
67         karmaProxy[ wgScriptPath ] = {
68                 target: wgServer + wgScriptPath,
69                 changeOrigin: true
70         };
72         grunt.initConfig( {
73                 eslint: {
74                         options: {
75                                 extensions: [ '.js', '.json', '.vue' ],
76                                 cache: true,
77                                 fix: grunt.option( 'fix' )
78                         },
79                         all: '.'
80                 },
81                 banana: {
82                         options: {
83                                 requireLowerCase: false,
84                                 disallowBlankTranslations: false
85                         },
86                         core: 'languages/i18n/',
87                         exif: 'languages/i18n/exif/',
88                         api: 'includes/api/i18n/',
89                         rest: 'includes/Rest/i18n/',
90                         installer: 'includes/installer/i18n/',
91                         paramvalidator: 'includes/libs/ParamValidator/i18n/'
92                 },
93                 stylelint: {
94                         options: {
95                                 reportNeedlessDisables: true
96                         },
97                         resources: 'resources/src/**/*.{css,less,vue}',
98                         config: 'mw-config/**/*.css'
99                 },
100                 watch: {
101                         files: [
102                                 '.{stylelintrc,eslintrc}.json',
103                                 '**/*',
104                                 '!{extensions,node_modules,skins,vendor}/**'
105                         ],
106                         tasks: 'test'
107                 },
108                 karma: {
109                         options: {
110                                 customLaunchers: {
111                                         ChromeCustom: {
112                                                 base: 'ChromeHeadless',
113                                                 // Chrome requires --no-sandbox in Docker/CI.
114                                                 // WMF CI images expose CHROMIUM_FLAGS which sets that.
115                                                 flags: process.env.CHROMIUM_FLAGS ? ( process.env.CHROMIUM_FLAGS || '' ).split( ' ' ) : []
116                                         }
117                                 },
118                                 proxies: karmaProxy,
119                                 files: [ {
120                                         pattern: qunitPattern,
121                                         type: 'js',
122                                         watched: false,
123                                         included: true,
124                                         served: false
125                                 }, ...qunitWatchFiles ],
126                                 logLevel: ( process.env.ZUUL_PROJECT ? 'DEBUG' : 'INFO' ),
127                                 frameworks: [ 'qunit' ],
128                                 reporters: [ 'mocha' ],
129                                 singleRun: !qunitWatch,
130                                 autoWatch: qunitWatch,
131                                 // Some tests in extensions don't yield for more than the default 10s (T89075)
132                                 browserNoActivityTimeout: 60 * 1000,
133                                 // Karma requires Same-Origin (or CORS) by default since v1.1.1
134                                 // for better stacktraces. But we load the first request from wgServer
135                                 crossOriginAttribute: false
136                         },
137                         main: {
138                                 browsers: [ 'ChromeCustom' ]
139                         },
140                         firefox: {
141                                 browsers: [ 'FirefoxHeadless' ]
142                         }
143                 }
144         } );
146         grunt.registerTask( 'assert-mw-env', function () {
147                 let ok = true;
148                 if ( !process.env.MW_SERVER ) {
149                         grunt.log.error( 'Environment variable MW_SERVER must be set.\n' +
150                                 'Set this like $wgServer, e.g. "http://localhost"'
151                         );
152                         ok = false;
153                 }
154                 if ( !process.env.MW_SCRIPT_PATH ) {
155                         grunt.log.error( 'Environment variable MW_SCRIPT_PATH must be set.\n' +
156                                 'Set this like $wgScriptPath, e.g. "/w"' );
157                         ok = false;
158                 }
159                 return ok;
160         } );
162         grunt.registerTask( 'lint', [ 'eslint', 'banana', 'stylelint' ] );
163         grunt.registerTask( 'qunit', [ 'assert-mw-env', 'karma:main' ] );