summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/utils/generate_sources.ts
blob: 1507d11e5e07d221fc34132577b56382295d25ff (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
import {createHash} from 'crypto';
import esbuild from 'esbuild';
import {mkdir, mkdtemp, readFile, rm, writeFile} from 'fs/promises';
import {sync as glob} from 'glob';
import path from 'path';
import {job} from './internal/job.js';

const INCLUDED_FOLDERS = ['common', 'node', 'generated', 'util'];

(async () => {
  await job('', async ({outputs}) => {
    await Promise.all(
      outputs.map(outputs => {
        return mkdir(outputs, {recursive: true});
      })
    );
  })
    .outputs(['src/generated'])
    .build();

  await job('', async ({name, inputs, outputs}) => {
    const input = inputs.find(input => {
      return input.endsWith('injected.ts');
    })!;
    const template = await readFile(
      inputs.find(input => {
        return input.includes('injected.ts.tmpl');
      })!,
      'utf8'
    );
    const tmp = await mkdtemp(name);
    await esbuild.build({
      entryPoints: [input],
      bundle: true,
      outdir: tmp,
      format: 'cjs',
      platform: 'browser',
      target: 'ES2019',
    });
    const baseName = path.basename(input);
    const content = await readFile(
      path.join(tmp, baseName.replace('.ts', '.js')),
      'utf-8'
    );
    const scriptContent = template.replace(
      'SOURCE_CODE',
      JSON.stringify(content)
    );
    await writeFile(outputs[0]!, scriptContent);
    await rm(tmp, {recursive: true, force: true});
  })
    .inputs(['src/templates/injected.ts.tmpl', 'src/injected/**/*.ts'])
    .outputs(['src/generated/injected.ts'])
    .build();

  const sources = glob(
    `src/{@(${INCLUDED_FOLDERS.join('|')})/*.ts,!(types|puppeteer-core).ts}`
  );
  await job('', async ({outputs}) => {
    let types =
      '// AUTOGENERATED - Use `npm run generate:sources` to regenerate.\n\n';
    for (const input of sources.map(source => {
      return `.${source.slice(3)}`;
    })) {
      types += `export * from '${input.replace('.ts', '.js')}';\n`;
    }
    await writeFile(outputs[0]!, types);
  })
    .value(
      sources
        .reduce((hmac, value) => {
          return hmac.update(value);
        }, createHash('sha256'))
        .digest('hex')
    )
    .outputs(['src/types.ts'])
    .build();

  if (process.env['PUBLISH']) {
    job('', async ({inputs}) => {
      const version = JSON.parse(await readFile(inputs[0]!, 'utf8')).version;
      await writeFile(
        inputs[1]!,
        (
          await readFile(inputs[1]!, {
            encoding: 'utf-8',
          })
        ).replace("'NEXT'", `'v${version}'`)
      );
    })
      .inputs(['package.json', 'versions.js'])
      .build();
  }

  job('', async ({inputs, outputs}) => {
    const version = JSON.parse(await readFile(inputs[0]!, 'utf8')).version;
    await writeFile(
      outputs[0]!,
      (await readFile(inputs[1]!, 'utf8')).replace('PACKAGE_VERSION', version)
    );
  })
    .inputs(['package.json', 'src/templates/version.ts.tmpl'])
    .outputs(['src/generated/version.ts'])
    .build();
})();