2021-01-06 23:41:17 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-08-05 22:07:43 +03:00
|
|
|
// @ts-check
|
|
|
|
|
2021-01-06 23:41:17 +03:00
|
|
|
const child_process = require('child_process');
|
|
|
|
const path = require('path');
|
2021-01-08 00:26:34 +03:00
|
|
|
const chokidar = require('chokidar');
|
2021-06-24 04:01:48 +03:00
|
|
|
const fs = require('fs');
|
2021-10-11 17:52:17 +03:00
|
|
|
const { packages } = require('../list_packages');
|
2021-01-06 23:41:17 +03:00
|
|
|
|
2021-08-05 22:07:43 +03:00
|
|
|
/**
|
|
|
|
* @typedef {{
|
|
|
|
* command: string,
|
|
|
|
* args: string[],
|
|
|
|
* shell: boolean,
|
|
|
|
* env?: NodeJS.ProcessEnv,
|
2021-10-22 11:13:05 +03:00
|
|
|
* cwd?: string,
|
2021-08-05 22:07:43 +03:00
|
|
|
* }} Step
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-10-22 11:13:05 +03:00
|
|
|
* @typedef {{
|
|
|
|
* files: string,
|
|
|
|
* from: string,
|
|
|
|
* to: string,
|
|
|
|
* ignored?: string[],
|
|
|
|
* }} CopyFile
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {{
|
|
|
|
* committed: boolean,
|
|
|
|
* inputs: string[],
|
|
|
|
* mustExist?: string[],
|
|
|
|
* script: string,
|
|
|
|
* }} OnChange
|
2021-08-05 22:07:43 +03:00
|
|
|
*/
|
2021-10-22 11:13:05 +03:00
|
|
|
|
|
|
|
/** @type {Step[]} */
|
2021-01-06 23:41:17 +03:00
|
|
|
const steps = [];
|
2021-10-22 11:13:05 +03:00
|
|
|
/** @type {OnChange[]} */
|
2021-01-06 23:41:17 +03:00
|
|
|
const onChanges = [];
|
2021-10-22 11:13:05 +03:00
|
|
|
/** @type {CopyFile[]} */
|
2021-06-24 04:01:48 +03:00
|
|
|
const copyFiles = [];
|
2021-01-06 23:41:17 +03:00
|
|
|
|
|
|
|
const watchMode = process.argv.slice(2).includes('--watch');
|
2021-06-24 04:01:48 +03:00
|
|
|
const lintMode = process.argv.slice(2).includes('--lint');
|
2021-01-06 23:41:17 +03:00
|
|
|
const ROOT = path.join(__dirname, '..', '..');
|
|
|
|
|
2021-10-22 11:13:05 +03:00
|
|
|
/**
|
|
|
|
* @param {string} relative
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-01-06 23:41:17 +03:00
|
|
|
function filePath(relative) {
|
|
|
|
return path.join(ROOT, ...relative.split('/'));
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:35:50 +03:00
|
|
|
async function runWatch() {
|
|
|
|
function runOnChanges(paths, mustExist = [], nodeFile) {
|
2021-01-08 00:26:34 +03:00
|
|
|
nodeFile = filePath(nodeFile);
|
2021-01-06 23:41:17 +03:00
|
|
|
function callback() {
|
2021-10-13 23:35:50 +03:00
|
|
|
for (const fileMustExist of mustExist) {
|
|
|
|
if (!fs.existsSync(filePath(fileMustExist)))
|
|
|
|
return;
|
|
|
|
}
|
2021-01-08 00:26:34 +03:00
|
|
|
child_process.spawnSync('node', [nodeFile], { stdio: 'inherit' });
|
2021-01-06 23:41:17 +03:00
|
|
|
}
|
2021-10-13 23:35:50 +03:00
|
|
|
chokidar.watch([...paths, ...mustExist, nodeFile].map(filePath)).on('all', callback);
|
2021-01-08 00:26:34 +03:00
|
|
|
callback();
|
2021-01-06 23:41:17 +03:00
|
|
|
}
|
|
|
|
|
2021-10-11 17:52:17 +03:00
|
|
|
for (const { files, from, to, ignored } of copyFiles) {
|
|
|
|
const watcher = chokidar.watch([filePath(files)], { ignored });
|
|
|
|
watcher.on('all', (event, file) => {
|
|
|
|
copyFile(file, from, to);
|
|
|
|
});
|
|
|
|
}
|
2021-10-22 11:13:05 +03:00
|
|
|
/** @type{import('child_process').ChildProcess[]} */
|
2021-01-06 23:41:17 +03:00
|
|
|
const spawns = [];
|
|
|
|
for (const step of steps)
|
2021-10-11 17:52:17 +03:00
|
|
|
spawns.push(child_process.spawn(step.command, step.args, {
|
2021-10-22 11:13:05 +03:00
|
|
|
stdio: 'inherit',
|
|
|
|
shell: step.shell,
|
|
|
|
env: {
|
2021-10-11 17:52:17 +03:00
|
|
|
...process.env,
|
|
|
|
...step.env,
|
2021-10-22 11:13:05 +03:00
|
|
|
},
|
|
|
|
cwd: step.cwd,
|
2021-10-11 17:52:17 +03:00
|
|
|
}));
|
2021-01-06 23:41:17 +03:00
|
|
|
process.on('exit', () => spawns.forEach(s => s.kill()));
|
|
|
|
for (const onChange of onChanges)
|
2021-10-13 23:35:50 +03:00
|
|
|
runOnChanges(onChange.inputs, onChange.mustExist, onChange.script);
|
2021-01-06 23:41:17 +03:00
|
|
|
}
|
|
|
|
|
2021-06-24 04:01:48 +03:00
|
|
|
async function runBuild() {
|
2021-08-05 22:07:43 +03:00
|
|
|
/**
|
|
|
|
* @param {Step} step
|
|
|
|
*/
|
|
|
|
function runStep(step) {
|
2021-10-11 17:52:17 +03:00
|
|
|
const out = child_process.spawnSync(step.command, step.args, {
|
2021-10-22 11:13:05 +03:00
|
|
|
stdio: 'inherit',
|
|
|
|
shell: step.shell,
|
|
|
|
env: {
|
2021-10-11 17:52:17 +03:00
|
|
|
...process.env,
|
|
|
|
...step.env
|
2021-10-22 11:13:05 +03:00
|
|
|
},
|
|
|
|
cwd: step.cwd,
|
2021-10-11 17:52:17 +03:00
|
|
|
});
|
2021-01-06 23:41:17 +03:00
|
|
|
if (out.status)
|
|
|
|
process.exit(out.status);
|
|
|
|
}
|
|
|
|
|
2021-10-11 17:52:17 +03:00
|
|
|
for (const { files, from, to, ignored } of copyFiles) {
|
2021-06-24 04:01:48 +03:00
|
|
|
const watcher = chokidar.watch([filePath(files)], {
|
|
|
|
ignored
|
|
|
|
});
|
|
|
|
watcher.on('add', file => {
|
|
|
|
copyFile(file, from, to);
|
|
|
|
});
|
|
|
|
await new Promise(x => watcher.once('ready', x));
|
|
|
|
watcher.close();
|
|
|
|
}
|
2021-10-11 17:52:17 +03:00
|
|
|
for (const step of steps)
|
|
|
|
runStep(step);
|
|
|
|
for (const onChange of onChanges) {
|
|
|
|
if (!onChange.committed)
|
|
|
|
runStep({ command: 'node', args: [filePath(onChange.script)], shell: false });
|
|
|
|
}
|
2021-06-24 04:01:48 +03:00
|
|
|
}
|
|
|
|
|
2021-10-22 11:13:05 +03:00
|
|
|
/**
|
|
|
|
* @param {string} file
|
|
|
|
* @param {string} from
|
|
|
|
* @param {string} to
|
|
|
|
*/
|
2021-06-24 04:01:48 +03:00
|
|
|
function copyFile(file, from, to) {
|
|
|
|
const destination = path.resolve(filePath(to), path.relative(filePath(from), file));
|
|
|
|
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
|
|
|
fs.copyFileSync(file, destination);
|
2021-01-06 23:41:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build injected scripts.
|
|
|
|
const webPackFiles = [
|
2021-10-11 17:52:17 +03:00
|
|
|
'packages/playwright-core/src/server/injected/webpack.config.js',
|
|
|
|
'packages/playwright-core/src/web/traceViewer/webpack.config.js',
|
2021-10-12 06:52:28 +03:00
|
|
|
'packages/playwright-core/src/web/traceViewer/webpack-sw.config.js',
|
2021-10-11 17:52:17 +03:00
|
|
|
'packages/playwright-core/src/web/recorder/webpack.config.js',
|
|
|
|
'packages/playwright-core/src/web/htmlReport/webpack.config.js',
|
2021-01-06 23:41:17 +03:00
|
|
|
];
|
|
|
|
for (const file of webPackFiles) {
|
|
|
|
steps.push({
|
|
|
|
command: 'npx',
|
2021-10-14 22:16:15 +03:00
|
|
|
args: ['webpack', '--config', filePath(file), ...(watchMode ? ['--watch', '--stats', 'none'] : [])],
|
2021-01-06 23:41:17 +03:00
|
|
|
shell: true,
|
2021-01-09 03:15:05 +03:00
|
|
|
env: {
|
|
|
|
NODE_ENV: watchMode ? 'development' : 'production'
|
|
|
|
}
|
2021-01-06 23:41:17 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-24 04:01:48 +03:00
|
|
|
// Run Babel.
|
2021-10-11 17:52:17 +03:00
|
|
|
for (const packageDir of packages) {
|
|
|
|
if (!fs.existsSync(path.join(packageDir, 'src')))
|
|
|
|
continue;
|
2021-10-22 11:13:05 +03:00
|
|
|
if (path.basename(packageDir) === 'create-playwright')
|
|
|
|
continue;
|
2021-10-11 17:52:17 +03:00
|
|
|
steps.push({
|
|
|
|
command: 'npx',
|
2021-10-14 05:27:50 +03:00
|
|
|
args: [
|
|
|
|
'babel',
|
|
|
|
...(watchMode ? ['-w', '--source-maps'] : []),
|
|
|
|
'--extensions', '.ts',
|
|
|
|
'--out-dir', path.join(packageDir, 'lib'),
|
2021-10-22 11:13:05 +03:00
|
|
|
'--ignore', '"packages/playwright-core/src/server/injected/**/*","packages/create-playwright/**/*"',
|
2021-10-14 05:27:50 +03:00
|
|
|
path.join(packageDir, 'src')],
|
2021-10-11 17:52:17 +03:00
|
|
|
shell: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-06 23:41:17 +03:00
|
|
|
|
|
|
|
// Generate channels.
|
|
|
|
onChanges.push({
|
|
|
|
committed: false,
|
|
|
|
inputs: [
|
2021-10-11 17:52:17 +03:00
|
|
|
'packages/playwright-core/src/protocol/protocol.yml'
|
2021-01-06 23:41:17 +03:00
|
|
|
],
|
|
|
|
script: 'utils/generate_channels.js',
|
|
|
|
});
|
|
|
|
|
|
|
|
// Generate types.
|
|
|
|
onChanges.push({
|
|
|
|
committed: false,
|
|
|
|
inputs: [
|
2021-01-08 00:26:34 +03:00
|
|
|
'docs/src/api/',
|
2021-07-30 00:33:47 +03:00
|
|
|
'docs/src/test-api/',
|
|
|
|
'docs/src/test-reporter-api/',
|
2021-01-06 23:41:17 +03:00
|
|
|
'utils/generate_types/overrides.d.ts',
|
2021-07-30 00:33:47 +03:00
|
|
|
'utils/generate_types/overrides-test.d.ts',
|
|
|
|
'utils/generate_types/overrides-testReporter.d.ts',
|
2021-01-06 23:41:17 +03:00
|
|
|
'utils/generate_types/exported.json',
|
2021-10-11 17:52:17 +03:00
|
|
|
'packages/playwright-core/src/server/chromium/protocol.d.ts',
|
2021-01-06 23:41:17 +03:00
|
|
|
],
|
2021-10-13 23:35:50 +03:00
|
|
|
mustExist: [
|
|
|
|
'packages/playwright-core/lib/server/deviceDescriptors.js',
|
|
|
|
'packages/playwright-core/lib/server/deviceDescriptorsSource.json',
|
|
|
|
],
|
2021-01-06 23:41:17 +03:00
|
|
|
script: 'utils/generate_types/index.js',
|
|
|
|
});
|
|
|
|
|
2021-07-02 03:05:14 +03:00
|
|
|
// The recorder and trace viewer have an app_icon.png that needs to be copied.
|
2021-06-24 04:01:48 +03:00
|
|
|
copyFiles.push({
|
2021-10-11 17:52:17 +03:00
|
|
|
files: 'packages/playwright-core/src/server/chromium/*.png',
|
|
|
|
from: 'packages/playwright-core/src',
|
|
|
|
to: 'packages/playwright-core/lib',
|
2021-06-24 04:01:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Babel doesn't touch JS files, so copy them manually.
|
|
|
|
// For example: diff_match_patch.js
|
|
|
|
copyFiles.push({
|
2021-10-11 17:52:17 +03:00
|
|
|
files: 'packages/playwright-core/src/**/*.js',
|
|
|
|
from: 'packages/playwright-core/src',
|
|
|
|
to: 'packages/playwright-core/lib',
|
2021-10-12 06:52:28 +03:00
|
|
|
ignored: ['**/.eslintrc.js', '**/webpack*.config.js', '**/injected/**/*']
|
2021-02-01 03:37:13 +03:00
|
|
|
});
|
|
|
|
|
2021-10-13 23:38:26 +03:00
|
|
|
copyFiles.push({
|
|
|
|
files: 'packages/playwright-test/src/**/*.js',
|
|
|
|
from: 'packages/playwright-test/src',
|
|
|
|
to: 'packages/playwright-test/lib',
|
|
|
|
ignored: ['**/.eslintrc.js']
|
|
|
|
});
|
|
|
|
|
2021-06-24 04:01:48 +03:00
|
|
|
// Sometimes we require JSON files that babel ignores.
|
|
|
|
// For example, deviceDescriptorsSource.json
|
|
|
|
copyFiles.push({
|
2021-10-11 17:52:17 +03:00
|
|
|
files: 'packages/playwright-core/src/**/*.json',
|
2021-06-24 04:01:48 +03:00
|
|
|
ignored: ['**/injected/**/*'],
|
2021-10-11 17:52:17 +03:00
|
|
|
from: 'packages/playwright-core/src',
|
|
|
|
to: 'packages/playwright-core/lib',
|
2021-06-24 04:01:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (lintMode) {
|
|
|
|
// Run TypeScript for type chekcing.
|
|
|
|
steps.push({
|
|
|
|
command: 'npx',
|
|
|
|
args: ['tsc', ...(watchMode ? ['-w'] : []), '-p', filePath('.')],
|
|
|
|
shell: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-22 11:13:05 +03:00
|
|
|
// create-playwright package
|
|
|
|
steps.push({
|
|
|
|
command: 'npx',
|
|
|
|
args: ['ncc', 'build', 'cli.ts', (watchMode ? '--watch' : '--minify'), '--out', '../lib'],
|
|
|
|
shell: true,
|
|
|
|
cwd: 'packages/create-playwright/src'
|
|
|
|
});
|
|
|
|
|
2021-01-06 23:41:17 +03:00
|
|
|
watchMode ? runWatch() : runBuild();
|