|
| 1 | +/* |
| 2 | + * Copyright 2020 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +const tempy = require('tempy'); |
| 14 | +const fs = require('fs-extra'); |
| 15 | +const packageJSON = require('../package.json'); |
| 16 | +const path = require('path'); |
| 17 | +const glob = require('fast-glob'); |
| 18 | +const spawn = require('cross-spawn'); |
| 19 | + |
| 20 | +build().catch(err => { |
| 21 | + console.error(err.stack); |
| 22 | + process.exit(1); |
| 23 | +}); |
| 24 | + |
| 25 | +/** |
| 26 | + * Building this will run the docs builder using the apiCheck pipeline in .parcelrc |
| 27 | + * This will generate json containing the visible (API/exposed) type definitions for each package |
| 28 | + * This is run against the current branch by copying the current branch into a temporary directory and building there |
| 29 | + */ |
| 30 | +async function build() { |
| 31 | + // Create a temp directory to build the site in |
| 32 | + let dir = tempy.directory(); |
| 33 | + console.log(`Building branch api into ${dir}...`); |
| 34 | + |
| 35 | + // Generate a package.json containing just what we need to build the website |
| 36 | + let pkg = { |
| 37 | + name: 'rsp-website', |
| 38 | + version: '0.0.0', |
| 39 | + private: true, |
| 40 | + workspaces: [ |
| 41 | + 'packages/*/*' |
| 42 | + ], |
| 43 | + devDependencies: Object.fromEntries( |
| 44 | + Object.entries(packageJSON.devDependencies) |
| 45 | + .filter(([name]) => |
| 46 | + name.startsWith('@parcel') || |
| 47 | + name === 'parcel' || |
| 48 | + name === 'patch-package' || |
| 49 | + name.startsWith('@spectrum-css') || |
| 50 | + name.startsWith('postcss') || |
| 51 | + name.startsWith('@adobe') |
| 52 | + ) |
| 53 | + ), |
| 54 | + dependencies: {}, |
| 55 | + resolutions: packageJSON.resolutions, |
| 56 | + browserslist: packageJSON.browserslist, |
| 57 | + scripts: { |
| 58 | + build: 'yarn parcel build packages/@react-spectrum/actiongroup', |
| 59 | + postinstall: 'patch-package' |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + // Add dependencies on each published package to the package.json, and |
| 64 | + // copy the docs from the current package into the temp dir. |
| 65 | + let packagesDir = path.join(__dirname, '..', 'packages'); |
| 66 | + let packages = glob.sync('*/**/package.json', {cwd: packagesDir}); |
| 67 | + |
| 68 | + pkg.devDependencies['babel-plugin-transform-glob-import'] = '*'; |
| 69 | + |
| 70 | + fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(pkg, false, 2)); |
| 71 | + |
| 72 | + fs.writeFileSync(path.join(dir, 'babel.config.json'), `{ |
| 73 | + "plugins": [ |
| 74 | + "transform-glob-import" |
| 75 | + ] |
| 76 | + }`); |
| 77 | + |
| 78 | + // Copy necessary code and configuration over |
| 79 | + fs.copySync(path.join(__dirname, '..', 'yarn.lock'), path.join(dir, 'yarn.lock')); |
| 80 | + fs.copySync(path.join(__dirname, '..', 'packages', 'dev'), path.join(dir, 'packages', 'dev')); |
| 81 | + fs.removeSync(path.join(dir, 'packages', 'dev', 'v2-test-deps')); |
| 82 | + fs.copySync(path.join(__dirname, '..', 'packages', '@adobe', 'spectrum-css-temp'), path.join(dir, 'packages', '@adobe', 'spectrum-css-temp')); |
| 83 | + fs.copySync(path.join(__dirname, '..', '.parcelrc'), path.join(dir, '.parcelrc')); |
| 84 | + fs.copySync(path.join(__dirname, '..', 'cssnano.config.js'), path.join(dir, 'cssnano.config.js')); |
| 85 | + fs.copySync(path.join(__dirname, '..', 'postcss.config.js'), path.join(dir, 'postcss.config.js')); |
| 86 | + fs.copySync(path.join(__dirname, '..', 'lib'), path.join(dir, 'lib')); |
| 87 | + fs.copySync(path.join(__dirname, '..', 'CONTRIBUTING.md'), path.join(dir, 'CONTRIBUTING.md')); |
| 88 | + |
| 89 | + // Only copy babel patch over |
| 90 | + let patches = fs.readdirSync(path.join(__dirname, '..', 'patches')); |
| 91 | + let babelPatch = patches.find(name => name.startsWith('@babel')); |
| 92 | + fs.copySync(path.join(__dirname, '..', 'patches', babelPatch), path.join(dir, 'patches', babelPatch)); |
| 93 | + |
| 94 | + // Copy packages over to temp dir |
| 95 | + console.log('copying over'); |
| 96 | + for (let p of packages) { |
| 97 | + if (!p.includes('spectrum-css') && !p.includes('dev/')) { |
| 98 | + let json = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'packages', p)), 'utf8'); |
| 99 | + if (json.private) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + fs.copySync(path.join(__dirname, '..', 'packages', path.dirname(p)), path.join(dir, 'packages', path.dirname(p))); |
| 103 | + if (!p.includes('@react-types')) { |
| 104 | + delete json.types; |
| 105 | + } |
| 106 | + delete json.main; |
| 107 | + delete json.module; |
| 108 | + delete json.devDependencies; |
| 109 | + json.apiCheck = 'dist/api.json'; |
| 110 | + json.targets = { |
| 111 | + apiCheck: {} |
| 112 | + }; |
| 113 | + fs.writeFileSync(path.join(dir, 'packages', p), JSON.stringify(json, false, 2)); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Install dependencies from npm |
| 118 | + await run('yarn', {cwd: dir, stdio: 'inherit'}); |
| 119 | + |
| 120 | + // Build the website |
| 121 | + console.log('building api files'); |
| 122 | + await run('yarn', ['parcel', 'build', 'packages/@react-{spectrum,aria,stately}/*/', 'packages/@internationalized/*/', '--target', 'apiCheck'], {cwd: dir, stdio: 'inherit'}); |
| 123 | + |
| 124 | + // Copy the build back into dist, and delete the temp dir. |
| 125 | + fs.copySync(path.join(dir, 'packages'), path.join(__dirname, '..', 'dist', 'branch-api')); |
| 126 | + fs.removeSync(dir); |
| 127 | +} |
| 128 | + |
| 129 | +function run(cmd, args, opts) { |
| 130 | + return new Promise((resolve, reject) => { |
| 131 | + let child = spawn(cmd, args, opts); |
| 132 | + child.on('error', reject); |
| 133 | + child.on('close', code => { |
| 134 | + if (code !== 0) { |
| 135 | + reject(new Error('Child process failed')); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + resolve(); |
| 140 | + }); |
| 141 | + }); |
| 142 | +} |
0 commit comments