2022-08-17 12:39:41 +00:00
|
|
|
#!/usr/bin/env node
|
2022-08-31 12:42:53 +00:00
|
|
|
import {createHash} from 'crypto';
|
2022-08-17 12:39:41 +00:00
|
|
|
import esbuild from 'esbuild';
|
2022-08-31 12:42:53 +00:00
|
|
|
import {mkdir, mkdtemp, readFile, rm, writeFile} from 'fs/promises';
|
|
|
|
import {sync as glob} from 'glob';
|
2022-08-17 12:39:41 +00:00
|
|
|
import path from 'path';
|
2022-08-17 13:40:32 +00:00
|
|
|
import {job} from './internal/job.js';
|
2022-08-17 12:39:41 +00:00
|
|
|
|
2022-08-31 12:42:53 +00:00
|
|
|
const INCLUDED_FOLDERS = ['common', 'node', 'generated', 'util'];
|
|
|
|
|
2022-08-17 12:39:41 +00:00
|
|
|
(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}) => {
|
2022-08-31 08:50:22 +00:00
|
|
|
const input = inputs.find(input => {
|
|
|
|
return input.endsWith('injected.ts');
|
|
|
|
})!;
|
|
|
|
const template = await readFile(
|
|
|
|
inputs.find(input => {
|
|
|
|
return input.includes('injected.ts.tmpl');
|
|
|
|
})!,
|
|
|
|
'utf8'
|
|
|
|
);
|
2022-08-17 12:39:41 +00:00
|
|
|
const tmp = await mkdtemp(name);
|
|
|
|
await esbuild.build({
|
2022-08-31 08:50:22 +00:00
|
|
|
entryPoints: [input],
|
2022-08-17 12:39:41 +00:00
|
|
|
bundle: true,
|
|
|
|
outdir: tmp,
|
|
|
|
format: 'cjs',
|
|
|
|
platform: 'browser',
|
|
|
|
target: 'ES2019',
|
|
|
|
});
|
2022-08-31 08:50:22 +00:00
|
|
|
const baseName = path.basename(input);
|
2022-08-17 12:39:41 +00:00
|
|
|
const content = await readFile(
|
|
|
|
path.join(tmp, baseName.replace('.ts', '.js')),
|
|
|
|
'utf-8'
|
|
|
|
);
|
2022-08-31 08:50:22 +00:00
|
|
|
const scriptContent = template.replace(
|
|
|
|
'SOURCE_CODE',
|
|
|
|
JSON.stringify(content)
|
|
|
|
);
|
2022-08-17 12:39:41 +00:00
|
|
|
await writeFile(outputs[0]!, scriptContent);
|
2022-08-31 12:42:53 +00:00
|
|
|
await rm(tmp, {recursive: true, force: true});
|
2022-08-17 12:39:41 +00:00
|
|
|
})
|
2022-08-31 12:42:53 +00:00
|
|
|
.inputs(['src/templates/injected.ts.tmpl', 'src/injected/**/*.ts'])
|
2022-08-17 12:39:41 +00:00
|
|
|
.outputs(['src/generated/injected.ts'])
|
|
|
|
.build();
|
|
|
|
|
2022-08-31 12:42:53 +00:00
|
|
|
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();
|
|
|
|
|
2022-08-31 15:21:25 +00:00
|
|
|
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();
|
|
|
|
}
|
2022-08-17 12:39:41 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
})();
|