tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Gruntfile.js (10503B)


      1 /* eslint-disable node/no-unpublished-require */
      2 /* eslint-disable no-console */
      3 
      4 const timer = require('grunt-timer');
      5 const { spawnSync } = require('child_process');
      6 const path = require('path');
      7 
      8 const kAllSuites = ['webgpu', 'stress', 'manual', 'unittests', 'demo'];
      9 
     10 const kFilesForEslint = [
     11  // TS
     12  'src/**/*.ts',
     13  // JS
     14  '*.js',
     15  '.*.js',
     16  'tools/**/*.js',
     17 ];
     18 
     19 module.exports = function (grunt) {
     20  timer.init(grunt);
     21 
     22  // Project configuration.
     23  grunt.initConfig({
     24    pkg: grunt.file.readJSON('package.json'),
     25 
     26    clean: {
     27      gen: ['gen/'],
     28      out: ['out/'],
     29      'out-wpt': ['out-wpt/'],
     30      'out-node': ['out-node/'],
     31    },
     32 
     33    run: {
     34      'generate-version': {
     35        cmd: 'node',
     36        args: ['tools/gen_version'],
     37      },
     38      'generate-listings-and-webworkers': {
     39        cmd: 'node',
     40        args: ['tools/gen_listings_and_webworkers', 'gen/', ...kAllSuites.map(s => 'src/' + s)],
     41      },
     42      validate: {
     43        cmd: 'node',
     44        args: ['tools/validate', ...kAllSuites.map(s => 'src/' + s)],
     45      },
     46      'generate-cache': {
     47        // Note this generates files into the src/ directory (not the gen/ directory).
     48        cmd: 'node',
     49        args: ['tools/gen_cache', 'src/webgpu'],
     50      },
     51      'validate-cache': {
     52        cmd: 'node',
     53        args: ['tools/gen_cache', 'src/webgpu', '--validate'],
     54      },
     55      // Note these generate `cts*.https.html` directly into the out-wpt/ directory rather than
     56      // the gen/ directory (as well as generating a `webgpu_variant_list*.json` file in gen/).
     57      'write-out-wpt-cts-html': {
     58        cmd: 'node',
     59        args: ['tools/gen_wpt_cts_html', 'tools/gen_wpt_cfg_unchunked.json'],
     60      },
     61      'write-out-wpt-cts-html-chunked2sec': {
     62        cmd: 'node',
     63        args: ['tools/gen_wpt_cts_html', 'tools/gen_wpt_cfg_chunked2sec.json'],
     64      },
     65      'write-out-wpt-cts-html-withsomeworkers': {
     66        cmd: 'node',
     67        args: ['tools/gen_wpt_cts_html', 'tools/gen_wpt_cfg_withsomeworkers.json'],
     68      },
     69      unittest: {
     70        cmd: 'node',
     71        args: ['tools/run_node', 'unittests:*'],
     72      },
     73      'build-out': {
     74        cmd: 'node',
     75        args: [
     76          'node_modules/@babel/cli/bin/babel',
     77          '--extensions=.ts,.js',
     78          '--source-maps=true',
     79          '--out-dir=out/',
     80          'src/',
     81          // These files will be generated, instead of compiled from TypeScript.
     82          '--ignore=src/common/internal/version.ts',
     83          '--ignore=src/*/listing.ts',
     84        ],
     85      },
     86      'build-out-wpt': {
     87        cmd: 'node',
     88        args: [
     89          'node_modules/@babel/cli/bin/babel',
     90          '--extensions=.ts,.js',
     91          '--source-maps=false',
     92          '--delete-dir-on-start',
     93          '--out-dir=out-wpt/',
     94          'src/',
     95          '--only=src/common/',
     96          '--only=src/external/',
     97          '--only=src/webgpu/',
     98          // These files will be generated, instead of compiled from TypeScript.
     99          '--ignore=src/common/internal/version.ts',
    100          '--ignore=src/*/listing.ts',
    101          // These files are only used by non-WPT builds.
    102          '--ignore=src/common/runtime/cmdline.ts',
    103          '--ignore=src/common/runtime/server.ts',
    104          '--ignore=src/common/runtime/standalone.ts',
    105          '--ignore=src/common/runtime/helper/sys.ts',
    106          '--ignore=src/common/tools',
    107        ],
    108      },
    109      'build-out-node': {
    110        cmd: 'node',
    111        args: [
    112          'node_modules/typescript/lib/tsc.js',
    113          '--project',
    114          'node.tsconfig.json',
    115          '--outDir',
    116          'out-node/',
    117        ],
    118      },
    119      'copy-assets': {
    120        cmd: 'node',
    121        args: [
    122          'node_modules/@babel/cli/bin/babel',
    123          'src/resources/',
    124          '--out-dir=out/resources/',
    125          '--copy-files',
    126        ],
    127      },
    128      'copy-assets-wpt': {
    129        cmd: 'node',
    130        args: [
    131          'node_modules/@babel/cli/bin/babel',
    132          'src/resources/',
    133          '--out-dir=out-wpt/resources/',
    134          '--copy-files',
    135        ],
    136      },
    137      'copy-assets-node': {
    138        cmd: 'node',
    139        args: [
    140          'node_modules/@babel/cli/bin/babel',
    141          'src/resources/',
    142          '--out-dir=out-node/resources/',
    143          '--copy-files',
    144        ],
    145      },
    146      lint: {
    147        cmd: 'node',
    148        args: ['node_modules/eslint/bin/eslint', ...kFilesForEslint, '--max-warnings=0'],
    149      },
    150      fix: {
    151        cmd: 'node',
    152        args: ['node_modules/eslint/bin/eslint', ...kFilesForEslint, '--fix'],
    153      },
    154      'autoformat-out-wpt': {
    155        cmd: 'node',
    156        // MAINTENANCE_TODO(gpuweb/cts#3128): This autoformat step is broken after a dependencies upgrade.
    157        args: [
    158          'node_modules/prettier/bin/prettier.cjs',
    159          '--log-level=warn',
    160          '--write',
    161          'out-wpt/**/*.js',
    162        ],
    163      },
    164      tsdoc: {
    165        cmd: 'node',
    166        args: ['node_modules/typedoc/bin/typedoc'],
    167      },
    168      'tsdoc-treatWarningsAsErrors': {
    169        cmd: 'node',
    170        args: ['node_modules/typedoc/bin/typedoc', '--treatWarningsAsErrors'],
    171      },
    172 
    173      serve: {
    174        cmd: 'node',
    175        args: ['node_modules/http-server/bin/http-server', '-p8080', '-a127.0.0.1', '-c-1'],
    176      },
    177    },
    178 
    179    copy: {
    180      'gen-to-out': {
    181        // Must run after generate-common and run:build-out.
    182        files: [
    183          { expand: true, dest: 'out/', cwd: 'gen', src: 'common/internal/version.js' },
    184          { expand: true, dest: 'out/', cwd: 'gen', src: '*/**/*.js' },
    185        ],
    186      },
    187      'gen-to-out-wpt': {
    188        // Must run after generate-common and run:build-out-wpt.
    189        files: [
    190          { expand: true, dest: 'out-wpt/', cwd: 'gen', src: 'common/internal/version.js' },
    191          { expand: true, dest: 'out-wpt/', cwd: 'gen', src: 'webgpu/**/*.js' },
    192        ],
    193      },
    194      'htmlfiles-to-out': {
    195        // Must run after run:build-out.
    196        files: [{ expand: true, dest: 'out/', cwd: 'src', src: 'webgpu/**/*.html' }],
    197      },
    198      'htmlfiles-to-out-wpt': {
    199        // Must run after run:build-out-wpt.
    200        files: [{ expand: true, dest: 'out-wpt/', cwd: 'src', src: 'webgpu/**/*.html' }],
    201      },
    202    },
    203 
    204    concurrent: {
    205      'write-out-wpt-cts-html-all': {
    206        tasks: [
    207          'run:write-out-wpt-cts-html',
    208          'run:write-out-wpt-cts-html-chunked2sec',
    209          'run:write-out-wpt-cts-html-withsomeworkers',
    210        ],
    211      },
    212      'all-builds': {
    213        tasks: ['build-standalone', 'build-wpt', 'run:build-out-node'],
    214      },
    215      'all-checks': {
    216        tasks: [
    217          'ts-check',
    218          'run:validate',
    219          'run:validate-cache',
    220          'run:unittest',
    221          'run:lint',
    222          'run:tsdoc-treatWarningsAsErrors',
    223        ],
    224      },
    225      'all-builds-and-checks': {
    226        tasks: [
    227          'build-all', // Internally concurrent
    228          'concurrent:all-checks',
    229        ],
    230      },
    231    },
    232  });
    233 
    234  grunt.loadNpmTasks('grunt-contrib-clean');
    235  grunt.loadNpmTasks('grunt-contrib-copy');
    236  grunt.loadNpmTasks('grunt-concurrent');
    237  grunt.loadNpmTasks('grunt-run');
    238 
    239  const helpMessageTasks = [];
    240  function registerTaskAndAddToHelp(name, desc, deps) {
    241    grunt.registerTask(name, deps);
    242    addExistingTaskToHelp(name, desc);
    243  }
    244  function addExistingTaskToHelp(name, desc) {
    245    helpMessageTasks.push({ name, desc });
    246  }
    247 
    248  grunt.registerTask('ts-check', () => {
    249    spawnSync(
    250      path.join('node_modules', '.bin', 'tsc'),
    251      ['--project', 'tsconfig.json', '--noEmit'],
    252      {
    253        shell: true,
    254        stdio: 'inherit',
    255      }
    256    );
    257  });
    258 
    259  grunt.registerTask('generate-common', 'Generate files into gen/ and src/', [
    260    'clean:gen',
    261    'run:generate-version',
    262    'run:generate-listings-and-webworkers',
    263    'run:generate-cache',
    264  ]);
    265  grunt.registerTask('build-standalone', 'Build out/ (no checks; run after generate-common)', [
    266    'clean:out',
    267    'run:build-out',
    268    'run:copy-assets',
    269    'copy:gen-to-out',
    270    'copy:htmlfiles-to-out',
    271  ]);
    272  grunt.registerTask('build-wpt', 'Build out-wpt/ (no checks; run after generate-common)', [
    273    'clean:out-wpt',
    274    'run:build-out-wpt',
    275    'run:copy-assets-wpt',
    276    'copy:gen-to-out-wpt',
    277    'copy:htmlfiles-to-out-wpt',
    278    'concurrent:write-out-wpt-cts-html-all',
    279    'run:autoformat-out-wpt',
    280  ]);
    281  grunt.registerTask('build-node', 'Build out-node/ (no checks; run after generate-common)', [
    282    'clean:out-node',
    283    'run:build-out-node',
    284    'run:copy-assets-node',
    285  ]);
    286  grunt.registerTask('build-all', 'Build out*/ (no checks; run after generate-common)', [
    287    'concurrent:all-builds',
    288    'build-done-message',
    289  ]);
    290  grunt.registerTask('build-done-message', () => {
    291    grunt.log.writeln(`\
    292 =====================================================
    293 ==== Build completed! Continuing checks/tests... ====
    294 =====================================================`);
    295  });
    296 
    297  grunt.registerTask('pre', ['all']);
    298 
    299  registerTaskAndAddToHelp('all', 'Run all builds and checks', [
    300    'generate-common',
    301    'concurrent:all-builds-and-checks',
    302  ]);
    303  registerTaskAndAddToHelp('standalone', 'Build standalone (out/) (no checks)', [
    304    'generate-common',
    305    'build-standalone',
    306    'build-done-message',
    307  ]);
    308  registerTaskAndAddToHelp('wpt', 'Build for WPT (out-wpt/) (no checks)', [
    309    'generate-common',
    310    'build-wpt',
    311    'build-done-message',
    312  ]);
    313  registerTaskAndAddToHelp('node', 'Build node (out-node/) (no checks)', [
    314    'generate-common',
    315    'build-node',
    316    'build-done-message',
    317  ]);
    318  registerTaskAndAddToHelp('checks', 'Run all checks (and build tsdoc)', ['concurrent:all-checks']);
    319  registerTaskAndAddToHelp('unittest', 'Just run unittests', ['run:unittest']);
    320  registerTaskAndAddToHelp('typecheck', 'Just typecheck', ['ts-check']);
    321  registerTaskAndAddToHelp('tsdoc', 'Just build tsdoc', ['run:tsdoc']);
    322 
    323  registerTaskAndAddToHelp('serve', 'Serve out/ (without building anything)', ['run:serve']);
    324  registerTaskAndAddToHelp('lint', 'Check lint and formatting', ['run:lint']);
    325  registerTaskAndAddToHelp('fix', 'Fix lint and formatting', ['run:fix']);
    326 
    327  addExistingTaskToHelp('clean', 'Delete built and generated files');
    328 
    329  grunt.registerTask('default', '', () => {
    330    console.error('\nRecommended tasks:');
    331    const nameColumnSize = Math.max(...helpMessageTasks.map(({ name }) => name.length));
    332    for (const { name, desc } of helpMessageTasks) {
    333      console.error(`$ grunt ${name.padEnd(nameColumnSize)}  # ${desc}`);
    334    }
    335  });
    336 };