|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2019-Present Datadog, Inc. |
| 4 | + |
| 5 | +import { Command, Option } from 'clipanion'; |
| 6 | +import fs from 'fs'; |
| 7 | +import path from 'path'; |
| 8 | + |
| 9 | +class PrepareLink extends Command { |
| 10 | + static paths = [['prepare-link']]; |
| 11 | + |
| 12 | + static usage = Command.Usage({ |
| 13 | + category: `Contribution`, |
| 14 | + description: `Prepare our published packages to be linked from another project.`, |
| 15 | + details: ` |
| 16 | + This command will change the package.json values of "exports" so they can be used from another project. |
| 17 | +
|
| 18 | + This is necessary to be sure that the outside project loads the built files and not the dev files. |
| 19 | + `, |
| 20 | + examples: [ |
| 21 | + [`Prepare for link`, `$0 prepare-link`], |
| 22 | + [`Revert change`, `$0 prepare-link --revert`], |
| 23 | + ], |
| 24 | + }); |
| 25 | + |
| 26 | + revert = Option.Boolean('--revert', { |
| 27 | + description: 'Revert the changes.', |
| 28 | + }); |
| 29 | + |
| 30 | + async execute() { |
| 31 | + const { getWorkspaces, green, red } = await import('@dd/tools/helpers'); |
| 32 | + const { ROOT } = await import('@dd/tools/constants'); |
| 33 | + |
| 34 | + const workspaces = await getWorkspaces(); |
| 35 | + |
| 36 | + // Only get the published packages. |
| 37 | + const publishedPackages = workspaces.filter((workspace) => |
| 38 | + workspace.name.match(/^@datadog\/.*-plugin$/), |
| 39 | + ); |
| 40 | + |
| 41 | + try { |
| 42 | + for (const pkg of publishedPackages) { |
| 43 | + const pkgJsonPath = path.resolve(ROOT, pkg.location, 'package.json'); |
| 44 | + const pkgJson = require(pkgJsonPath); |
| 45 | + if (this.revert) { |
| 46 | + pkgJson.exports = { '.': './src/index.ts' }; |
| 47 | + } else { |
| 48 | + pkgJson.exports = pkgJson.publishConfig.exports; |
| 49 | + } |
| 50 | + |
| 51 | + fs.writeFileSync(pkgJsonPath, `${JSON.stringify(pkgJson, null, 4)}\n`); |
| 52 | + } |
| 53 | + |
| 54 | + console.log( |
| 55 | + green( |
| 56 | + `All packages have been ${this.revert ? 'restored from linking.' : 'prepared for linking.'}`, |
| 57 | + ), |
| 58 | + ); |
| 59 | + } catch (error: any) { |
| 60 | + console.error(red(error)); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +export default [PrepareLink]; |
0 commit comments