/* * Copyright 2020 Adobe. All rights reserved. * This file is licensed to you 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 REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ const tempy = require('tempy'); const fs = require('fs'); const packageJSON = require('../package.json'); const path = require('path'); const glob = require('fast-glob'); const spawn = require('cross-spawn'); const {parseArgs} = require('util'); const args = parseArgs({ options: { verbose: { short: 'v', type: 'boolean' }, output: { short: 'o', type: 'string' }, githash: { type: 'string' } } }); build().catch(err => { console.error(err.stack); process.exit(1); }); /** * Building this will run the docs builder using the apiCheck pipeline in .parcelrc This will * generate json containing the visible (API/exposed) type definitions for each package This is run * against the current branch by copying the current branch into a temporary directory and building * there. */ async function build() { let backupDir = tempy.directory(); let archiveDir; if (args.values.githash) { archiveDir = tempy.directory(); console.log('checking out archive of', args.values.githash, 'into', archiveDir); await run('sh', ['-c', `git archive ${args.values.githash} | tar -x -C ${archiveDir}`], { stdio: 'inherit' }); await run('sh', ['-c', `git archive HEAD | tar -x -C ${backupDir}`], {stdio: 'inherit'}); } let srcDir = archiveDir ?? path.join(__dirname, '..'); let distDir = path.join(__dirname, '..', 'dist', args.values.output ?? 'branch-api'); // if we already have a directory with a built dist, remove it so we can write cleanly into it at the end fs.rmSync(distDir, {recursive: true, force: true}); // Create a temp directory to build the site in let dir = tempy.directory(); console.log(`Building branch api into ${dir}...`); // Generate a package.json containing just what we need to build the website let pkg = { name: 'rsp-website', packageManager: 'yarn@4.18.0', version: '0.0.0', private: true, workspaces: [ 'packages/react-stately', 'packages/react-aria', 'packages/react-aria-components', 'packages/tailwindcss-react-aria-components', 'packages/*/*' ], devDependencies: Object.fromEntries( Object.entries(packageJSON.devDependencies).filter( ([name]) => name.startsWith('@parcel') || name === 'parcel' || name === 'patch-package' || name.startsWith('@spectrum-css') || name.startsWith('@testing-library') || name.startsWith('postcss') || name.startsWith('@adobe') || name === 'react' || name === 'react-dom' ) ), dependencies: {}, resolutions: packageJSON.resolutions, browserslist: packageJSON.browserslist, scripts: { build: 'yarn parcel build packages/@react-spectrum/actiongroup', postinstall: 'patch-package' }, '@parcel/resolver-default': { packageExports: true } }; // Add dependencies on each published package to the package.json, and // copy the docs from the current package into the temp dir. let packagesDir = path.join(srcDir, 'packages'); let packages = glob.sync('*/**/package.json', {cwd: packagesDir, ignore: ['**/node_modules/**']}); pkg.devDependencies['babel-plugin-transform-glob-import'] = '*'; fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(pkg, false, 2)); fs.writeFileSync( path.join(dir, 'babel.config.json'), `{ "plugins": [ "transform-glob-import" ] }` ); // Copy necessary code and configuration over fs.cpSync( path.join(srcDir, 'packages', '@adobe', 'spectrum-css-temp'), path.join(dir, 'packages', '@adobe', 'spectrum-css-temp'), {recursive: true} ); try { fs.cpSync( path.join(srcDir, 'packages', '@adobe', 'spectrum-css-builder-temp'), path.join(dir, 'packages', '@adobe', 'spectrum-css-builder-temp'), {recursive: true} ); } catch (e) { fs.cpSync( path.join(backupDir, 'packages', '@adobe', 'spectrum-css-builder-temp'), path.join(dir, 'packages', '@adobe', 'spectrum-css-builder-temp'), {recursive: true} ); } fs.cpSync(path.join(srcDir, 'postcss.config.js'), path.join(dir, 'postcss.config.js')); fs.cpSync(path.join(srcDir, 'lib'), path.join(dir, 'lib'), {recursive: true}); fs.cpSync(path.join(srcDir, 'CONTRIBUTING.md'), path.join(dir, 'CONTRIBUTING.md')); // need dev from latest on branch since it will generate the API for diffing, and in older commits it may not be able to do this or // does it in a different format fs.cpSync(path.join(__dirname, '..', 'packages', 'dev'), path.join(dir, 'packages', 'dev'), { recursive: true }); fs.cpSync(path.join(__dirname, '..', '.parcelrc'), path.join(dir, '.parcelrc')); // Delete test-utils from copied packages since we don't expose anything from there fs.rmSync(path.join(dir, 'packages', 'dev', 'test-utils'), {recursive: true, force: true}); fs.cpSync(path.join(__dirname, '..', '.yarn'), path.join(dir, '.yarn'), {recursive: true}); fs.cpSync(path.join(__dirname, '..', '.yarnrc.yml'), path.join(dir, '.yarnrc.yml')); // Only copy babel patch over let patches = fs.readdirSync(path.join(srcDir, 'patches')); let babelPatch = patches.find(name => name.startsWith('@babel')); fs.cpSync(path.join(srcDir, 'patches', babelPatch), path.join(dir, 'patches', babelPatch), { recursive: true }); // Collect workspace package names so we can strip intra-monorepo dependencies. // Sibling packages may pin exact versions (e.g. "@adobe/react-spectrum": "3.47.0") // that don't match the workspace version being built, causing yarn to install from // npm instead of symlinking to the workspace. Removing these entries lets yarn always // resolve them via the workspace. let workspacePackageNames = new Set( packages .map(p => { try { return JSON.parse(fs.readFileSync(path.join(srcDir, 'packages', p), 'utf8')).name; } catch { return null; } }) .filter(Boolean) ); // similar to above, dev/* packages may contain pinned dependency versions (e.g. codemods pins to specific S2 versions) // problem that arises is if we do a patch release since our weekly tsdiffer looks for the last MINOR instead // this causes a miss match in the versions since the dev/* package gets copied over and might still have the updated S2 patch // version whereas the workspace (aka the last minor baseline we checked out) has the last S2 minor version // handle this by deleting the pinned versions so we resolve to the workspace's version let devPackageJsons = glob.sync('dev/**/package.json', { cwd: path.join(dir, 'packages'), ignore: ['**/node_modules/**'] }); for (let p of devPackageJsons) { let fullPath = path.join(dir, 'packages', p); let json = JSON.parse(fs.readFileSync(fullPath, 'utf8')); let changed = false; for (let depField of ['dependencies', 'devDependencies']) { if (json[depField]) { for (let dep of Object.keys(json[depField])) { if (workspacePackageNames.has(dep)) { delete json[depField][dep]; changed = true; } } } } if (changed) { fs.writeFileSync(fullPath, JSON.stringify(json, false, 2)); } } let excludeList = ['@react-spectrum/story-utils']; // Copy packages over to temp dir console.log('copying over'); for (let p of packages) { if (!p.includes('spectrum-css') && !p.includes('example-theme') && !p.includes('dev/')) { let json = JSON.parse(fs.readFileSync(path.join(srcDir, 'packages', p)), 'utf8'); if (json.name in excludeList) { continue; } fs.cpSync( path.join(srcDir, 'packages', path.dirname(p)), path.join(dir, 'packages', path.dirname(p)), {dereference: true, recursive: true} ); if (!p.includes('@react-types')) { delete json.types; } delete json.main; delete json.module; delete json.devDependencies; if (json.dependencies) { for (let dep of Object.keys(json.dependencies)) { if (workspacePackageNames.has(dep)) { delete json.dependencies[dep]; } } } json.apiCheck = 'dist/api.json'; json.targets = { apiCheck: {} }; fs.writeFileSync(path.join(dir, 'packages', p), JSON.stringify(json, false, 2)); } } // Install dependencies from npm fs.cpSync(path.join(srcDir, 'yarn.lock'), path.join(dir, 'yarn.lock')); await run('yarn', ['--no-immutable'], {cwd: dir, stdio: 'inherit'}); // Build the website console.log('building api files'); await run( 'yarn', [ 'parcel', 'build', 'packages/react-aria-components', 'packages/@react-{spectrum,aria,stately}/*/', 'packages/@internationalized/{message,string,date,number}', '--target', 'apiCheck' ], {cwd: dir, stdio: 'inherit'} ); // Copy the build back into dist, and delete the temp dir. fs.cpSync(path.join(dir, 'packages'), distDir, {dereference: true, recursive: true}); fs.rmSync(dir, {recursive: true, force: true}); if (archiveDir) { fs.rmSync(archiveDir, {recursive: true, force: true}); } } function run(cmd, args, opts) { return new Promise((resolve, reject) => { let child = spawn(cmd, args, opts); child.on('error', reject); child.on('close', code => { if (code !== 0) { reject(new Error('Child process failed')); return; } resolve(); }); }); }