summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc/tools/html-codemod.js
blob: 6a31e8c4c605f0611ca12c3f3727c890a938426c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * extract script content from a series of html files, run a
 * jscodeshift codemod on them and overwrite the original file.
 *
 * Usage: node html-codemod.js codemod-file list of files to process
 */
const { JSDOM } = require('jsdom');
const fs = require('fs');
const {execFileSync} = require('child_process');

const codemod = process.argv[2];
const filenames = process.argv.slice(3);
filenames.forEach((filename) => {
    const originalContent = fs.readFileSync(filename, 'utf-8');
    const dom = new JSDOM(originalContent);
    const document = dom.window.document;
    const scriptTags = document.querySelectorAll('script');
    const lastTag = scriptTags[scriptTags.length - 1];
    const script = lastTag.innerHTML;
    if (!script) {
        console.log('NO SCRIPT FOUND', filename);
        return;
    }
    const scriptFilename = filename + '.codemod.js';
    const scriptFile = fs.writeFileSync(scriptFilename, script);
    // exec jscodeshift
    const output = execFileSync('./node_modules/.bin/jscodeshift', ['-t', codemod, scriptFilename]);
    console.log(filename, output.toString()); // output jscodeshift output.
    // read back file, resubstitute
    const newScript = fs.readFileSync(scriptFilename, 'utf-8').toString();
    const modifiedContent = originalContent.split(script).join(newScript);
    fs.writeFileSync(filename, modifiedContent);
    fs.unlinkSync(scriptFilename);
});