|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const GitHubTree = require('../github/tree'); |
| 4 | +const path = require('path'); |
| 5 | +const { writeFile, readJson, writeJson, readFile } = require('../file'); |
| 6 | +const _ = require('lodash'); |
| 7 | + |
| 8 | +class WPTUpdater { |
| 9 | + constructor(path, cli, request, nodedir) { |
| 10 | + this.path = path; |
| 11 | + this.nodedir = nodedir; |
| 12 | + |
| 13 | + this.cli = cli; |
| 14 | + this.request = request; |
| 15 | + this.treeParams = { |
| 16 | + owner: 'web-platform-tests', |
| 17 | + repo: 'wpt', |
| 18 | + branch: 'master', |
| 19 | + path |
| 20 | + }; |
| 21 | + this.tree = new GitHubTree(cli, request, this.treeParams); |
| 22 | + this.assets = []; |
| 23 | + } |
| 24 | + |
| 25 | + templates(...args) { |
| 26 | + return _.template(readFile(path.join(__dirname, 'templates', ...args))); |
| 27 | + } |
| 28 | + |
| 29 | + fixtures(...args) { |
| 30 | + return path.join(this.nodedir, 'test', 'fixtures', 'wpt', ...args); |
| 31 | + } |
| 32 | + |
| 33 | + // If filepath starts with '/', the path is relative to WPT project root, |
| 34 | + // otherwise it's relative to the path of this updater |
| 35 | + async pullTextFile(dest, filepath) { |
| 36 | + const content = await this.tree.text(filepath); |
| 37 | + const filename = path.join(dest, filepath); |
| 38 | + writeFile(filename, content); |
| 39 | + this.cli.updateSpinner(`Downloaded ${filename}`); |
| 40 | + } |
| 41 | + |
| 42 | + async pullAllAssets() { |
| 43 | + const fixtures = this.fixtures(this.path); |
| 44 | + this.cli.separator(`Writing test fixtures to ${fixtures}...`); |
| 45 | + |
| 46 | + this.cli.startSpinner('Querying asset list...'); |
| 47 | + const assets = this.assets = await this.tree.getFiles(); |
| 48 | + this.cli.stopSpinner( |
| 49 | + `Downloaded asset list, ${assets.length} files to write.` |
| 50 | + ); |
| 51 | + |
| 52 | + this.cli.startSpinner('Pulling assets...'); |
| 53 | + await Promise.all(assets.map( |
| 54 | + (asset) => this.pullTextFile(fixtures, asset.name) |
| 55 | + )); |
| 56 | + this.cli.stopSpinner(`Downloaded ${assets.length} assets.`); |
| 57 | + |
| 58 | + return assets; |
| 59 | + } |
| 60 | + |
| 61 | + getTreeUrl(path, commit) { |
| 62 | + const params = Object.assign({}, this.treeParams, { commit, path }); |
| 63 | + const tree = new GitHubTree(this.cli, this.request, params); |
| 64 | + return tree.getPermanentUrl(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param {string} nodedir |
| 69 | + * @param {Object<string, {commit: string, path: string}>} updated |
| 70 | + */ |
| 71 | + async updateVersions(updated) { |
| 72 | + const versionsPath = this.fixtures('versions.json'); |
| 73 | + const readmePath = this.fixtures('README.md'); |
| 74 | + |
| 75 | + this.cli.startSpinner('Updating versions.json ...'); |
| 76 | + const versions = readJson(versionsPath); |
| 77 | + Object.assign(versions, updated); |
| 78 | + writeJson(versionsPath, versions); |
| 79 | + this.cli.stopSpinner(`Updated ${versionsPath}`); |
| 80 | + |
| 81 | + const urlMap = Object.keys(versions).map( |
| 82 | + (key) => [key, this.getTreeUrl(versions[key].path, versions[key].commit)] |
| 83 | + ); |
| 84 | + |
| 85 | + this.cli.startSpinner('Updating README ...'); |
| 86 | + const readme = this.templates('README.md')({map: urlMap}); |
| 87 | + writeFile(readmePath, readme); |
| 88 | + this.cli.stopSpinner(`Updated ${readmePath}`); |
| 89 | + } |
| 90 | + |
| 91 | + async updateLicense() { |
| 92 | + this.cli.startSpinner('Updating license...'); |
| 93 | + await this.pullTextFile(this.fixtures(), '/LICENSE.md'); |
| 94 | + this.cli.stopSpinner(`Updated ${this.fixtures('LICENSE.md')}.`); |
| 95 | + } |
| 96 | + |
| 97 | + async update() { |
| 98 | + await this.pullAllAssets(); |
| 99 | + const lastCommit = await this.tree.getLastCommit(); |
| 100 | + await this.updateVersions({ |
| 101 | + [this.path]: { |
| 102 | + commit: lastCommit, |
| 103 | + path: this.path |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +class HarnessUpdater extends WPTUpdater { |
| 110 | + constructor(cli, request, nodedir) { |
| 111 | + super('resources', cli, request, nodedir); |
| 112 | + } |
| 113 | + |
| 114 | + async update() { // override |
| 115 | + const harnessPath = this.fixtures(this.path, 'testharness.js'); |
| 116 | + this.cli.startSpinner(`Downloading ${harnessPath}...`); |
| 117 | + await this.pullTextFile(this.fixtures(this.path), 'testharness.js'); |
| 118 | + this.cli.stopSpinner(`Downloaded ${harnessPath}`); |
| 119 | + const lastCommit = this.tree.lastCommit; |
| 120 | + await this.updateVersions({ |
| 121 | + harness: { commit: lastCommit, path: 'resources' } |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +module.exports = { |
| 127 | + WPTUpdater, |
| 128 | + HarnessUpdater |
| 129 | +}; |
0 commit comments