|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Copyright (c) Microsoft Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +const fs = require('fs'); |
| 18 | +const path = require('path'); |
| 19 | +const rmSync = require('rimraf').sync; |
| 20 | +const ncp = require('ncp'); |
| 21 | +const {spawnSync} = require('child_process'); |
| 22 | +const util = require('util'); |
| 23 | + |
| 24 | +const writeFileAsync = util.promisify(fs.writeFile.bind(fs)); |
| 25 | +const cpAsync = util.promisify(ncp); |
| 26 | + |
| 27 | +const SCRIPT_NAME = path.basename(__filename); |
| 28 | +const ROOT_PATH = path.join(__dirname, '..'); |
| 29 | + |
| 30 | +const PACKAGE_FILES = ['lib', 'types', 'NOTICE', 'LICENSE', '.npmignore']; |
| 31 | + |
| 32 | +const PACKAGES = { |
| 33 | + 'playwright': { |
| 34 | + description: 'A high-level API to automate web browsers', |
| 35 | + whitelistedBrowsers: ['chromium', 'firefox', 'webkit'], |
| 36 | + // We copy README.md additionally for Playwright so that it looks nice on NPM. |
| 37 | + files: [...PACKAGE_FILES, 'README.md'], |
| 38 | + }, |
| 39 | + 'playwright-core': { |
| 40 | + description: 'A high-level API to automate web browsers', |
| 41 | + whitelistedBrowsers: [], |
| 42 | + files: PACKAGE_FILES, |
| 43 | + }, |
| 44 | + 'playwright-webkit': { |
| 45 | + description: 'A high-level API to automate WebKit', |
| 46 | + whitelistedBrowsers: ['webkit'], |
| 47 | + files: PACKAGE_FILES, |
| 48 | + }, |
| 49 | + 'playwright-firefox': { |
| 50 | + description: 'A high-level API to automate Firefox', |
| 51 | + whitelistedBrowsers: ['firefox'], |
| 52 | + files: PACKAGE_FILES, |
| 53 | + }, |
| 54 | + 'playwright-chromium': { |
| 55 | + description: 'A high-level API to automate Chromium', |
| 56 | + whitelistedBrowsers: ['chromium'], |
| 57 | + files: PACKAGE_FILES, |
| 58 | + }, |
| 59 | +}; |
| 60 | + |
| 61 | +const cleanupPaths = []; |
| 62 | + |
| 63 | +// 1. Parse CLI arguments |
| 64 | +const args = process.argv.slice(2); |
| 65 | +if (args.some(arg => arg === '--help')) { |
| 66 | + console.log(usage()); |
| 67 | + process.exit(1); |
| 68 | +} else if (args.length < 1) { |
| 69 | + console.log(`Please specify package name, e.g. 'playwright' or 'playwright-chromium'.`); |
| 70 | + console.log(`Try running ${SCRIPT_NAME} --help`); |
| 71 | + process.exit(1); |
| 72 | +} else if (args.length < 2) { |
| 73 | + console.log(`Please specify output path`); |
| 74 | + console.log(`Try running ${SCRIPT_NAME} --help`); |
| 75 | + process.exit(1); |
| 76 | +} |
| 77 | + |
| 78 | +// 2. Setup cleanup if needed |
| 79 | +if (!args.some(arg => arg === '--no-cleanup')) { |
| 80 | + process.on('exit', () => { |
| 81 | + cleanupPaths.forEach(cleanupPath => rmSync(cleanupPath, {})); |
| 82 | + }); |
| 83 | + process.on('SIGINT', () => process.exit(2)); |
| 84 | + process.on('SIGHUP', () => process.exit(3)); |
| 85 | + process.on('SIGTERM', () => process.exit(4)); |
| 86 | + process.on('uncaughtException', error => { |
| 87 | + console.error(error); |
| 88 | + process.exit(5); |
| 89 | + }); |
| 90 | + process.on('unhandledRejection', error => { |
| 91 | + console.error(error); |
| 92 | + process.exit(6); |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +const packageName = args[0]; |
| 97 | +const outputPath = path.resolve(args[1]); |
| 98 | +const packagePath = path.join(__dirname, packageName); |
| 99 | +const package = PACKAGES[packageName]; |
| 100 | +if (!package) { |
| 101 | + console.log(`ERROR: unknown package ${packageName}`); |
| 102 | + process.exit(1); |
| 103 | +} |
| 104 | + |
| 105 | +(async () => { |
| 106 | + // 3. Copy package files. |
| 107 | + for (const file of package.files) |
| 108 | + await copyToPackage(file); |
| 109 | + |
| 110 | + // 4. Generate package.json |
| 111 | + const packageJSON = require(path.join(ROOT_PATH, 'package.json')); |
| 112 | + await writeToPackage('package.json', JSON.stringify({ |
| 113 | + name: packageName, |
| 114 | + version: packageJSON.version, |
| 115 | + description: package.description, |
| 116 | + repository: packageJSON.repository, |
| 117 | + engines: packageJSON.engines, |
| 118 | + homepage: packageJSON.homepage, |
| 119 | + main: 'index.js', |
| 120 | + scripts: { |
| 121 | + install: 'node install.js', |
| 122 | + }, |
| 123 | + author: packageJSON.author, |
| 124 | + license: packageJSON.license, |
| 125 | + dependencies: packageJSON.dependencies |
| 126 | + }, null, 2)); |
| 127 | + |
| 128 | + // 5. Generate browsers.json |
| 129 | + const browsersJSON = require(path.join(ROOT_PATH, 'browsers.json')); |
| 130 | + browsersJSON.browsers = browsersJSON.browsers.filter(browser => package.whitelistedBrowsers.includes(browser.name)); |
| 131 | + await writeToPackage('browsers.json', JSON.stringify(browsersJSON, null, 2)); |
| 132 | + |
| 133 | + // 6. Run npm pack |
| 134 | + const {stdout, stderr, status} = spawnSync('npm', ['pack'], {cwd: packagePath, encoding: 'utf8'}); |
| 135 | + if (status !== 0) { |
| 136 | + console.log(`ERROR: "npm pack" failed`); |
| 137 | + console.log(stderr); |
| 138 | + process.exit(1); |
| 139 | + } |
| 140 | + const tgzName = stdout.trim(); |
| 141 | + |
| 142 | + // 7. Move result to the outputPath |
| 143 | + fs.renameSync(path.join(packagePath, tgzName), outputPath); |
| 144 | + console.log(outputPath); |
| 145 | +})(); |
| 146 | + |
| 147 | +async function writeToPackage(fileName, content) { |
| 148 | + const toPath = path.join(packagePath, fileName); |
| 149 | + cleanupPaths.push(toPath); |
| 150 | + console.error(`- generating: //${path.relative(ROOT_PATH, toPath)}`); |
| 151 | + await writeFileAsync(toPath, content); |
| 152 | +} |
| 153 | + |
| 154 | +async function copyToPackage(fileOrDirectoryName) { |
| 155 | + const fromPath = path.join(ROOT_PATH, fileOrDirectoryName); |
| 156 | + const toPath = path.join(packagePath, fileOrDirectoryName); |
| 157 | + cleanupPaths.push(toPath); |
| 158 | + console.error(`- copying: //${path.relative(ROOT_PATH, fromPath)} -> //${path.relative(ROOT_PATH, toPath)}`); |
| 159 | + await cpAsync(fromPath, toPath); |
| 160 | +} |
| 161 | + |
| 162 | +function usage() { |
| 163 | + return ` |
| 164 | +usage: ${SCRIPT_NAME} <package-name> <output-path> [--no-cleanup] |
| 165 | +
|
| 166 | +Creates a .tgz of the package and saves it at the given output path |
| 167 | +
|
| 168 | + --no-cleanup skip cleaning up generated files from package directory |
| 169 | +
|
| 170 | +Example: |
| 171 | + ${SCRIPT_NAME} playwright ./playwright.tgz |
| 172 | +`; |
| 173 | +} |
| 174 | + |
0 commit comments