|
| 1 | +import path from 'node:path'; |
| 2 | +import yargs from 'yargs'; |
| 3 | +import {hideBin} from 'yargs/helpers' |
| 4 | +import {constructExampleSandbox} from './example-sandbox.mjs'; |
| 5 | + |
| 6 | +if (!process.env.BUILD_WORKSPACE_DIRECTORY) { |
| 7 | + console.error( |
| 8 | + 'Not running script as part of `bazel run`.' |
| 9 | + ) |
| 10 | + process.exit(1); |
| 11 | +} |
| 12 | +const sourceRoot = process.env.BUILD_WORKSPACE_DIRECTORY; |
| 13 | +const runfilesRoot = path.join(process.env.RUNFILES, 'angular'); |
| 14 | +const playgroundRoot = path.join(sourceRoot, 'aio', 'content', 'example-playground'); |
| 15 | + |
| 16 | +/** |
| 17 | + * Create an example playground with shared example deps and optionally linked local |
| 18 | + * angular packages in the source tree under content/examples/example-playground. This |
| 19 | + * script is intended to only be run under bazel as it has the localPackage arguments |
| 20 | + * hardcoded into the binary via starlark. |
| 21 | + * |
| 22 | + * Usage: bazel run //aio/tools/examples:create-example-playground -- --example=<example> |
| 23 | + * |
| 24 | + * Args: |
| 25 | + * example: name of the example |
| 26 | + */ |
| 27 | + |
| 28 | +async function main(args) { |
| 29 | + const options = |
| 30 | + yargs(args) |
| 31 | + // Note: localPackage not listed above in usage as it's hardcoded in by the nodejs_binary |
| 32 | + .option('localPackage', { |
| 33 | + array: true, |
| 34 | + type: 'string', |
| 35 | + default: [], |
| 36 | + describe: 'Locally built package to substitute, in the form `packageName#packagePath`' |
| 37 | + }) |
| 38 | + .option('example', { |
| 39 | + type: 'string', |
| 40 | + describe: 'Name of the example' |
| 41 | + }) |
| 42 | + .demandOption('example') |
| 43 | + .strict() |
| 44 | + .version(false) |
| 45 | + .argv; |
| 46 | + |
| 47 | + const localPackages = options.localPackage.reduce((pkgs, pkgNameAndPath) => { |
| 48 | + const [pkgName, pkgPath] = pkgNameAndPath.split('#'); |
| 49 | + pkgs[pkgName] = path.resolve(pkgPath); |
| 50 | + return pkgs; |
| 51 | + }, {}); |
| 52 | + |
| 53 | + const exampleName = options.example; |
| 54 | + |
| 55 | + // Note: the example sources plus boilerplate are merged into a target named after the example |
| 56 | + const fullExamplePath = path.join(runfilesRoot, 'aio', 'content', 'examples', exampleName, exampleName); |
| 57 | + |
| 58 | + const destPath = path.join(playgroundRoot, exampleName); |
| 59 | + const nodeModules = path.join(runfilesRoot, '..', 'aio_example_deps', 'node_modules'); |
| 60 | + |
| 61 | + await constructExampleSandbox(fullExamplePath, destPath, nodeModules, localPackages); |
| 62 | + |
| 63 | + console.log(`A playground folder for ${exampleName} has been set up at\n\n ${destPath}\n`); |
| 64 | +} |
| 65 | + |
| 66 | +(async () => await main(hideBin(process.argv)))(); |
0 commit comments