Skip to content

Commit

Permalink
fix(third-party-notices): move Commander config from rnx-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Sep 10, 2024
1 parent cf2e9e7 commit 63654fa
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 160 deletions.
6 changes: 6 additions & 0 deletions .changeset/eight-pots-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rnx-kit/third-party-notices": patch
---

Moved Commander config from `@rnx-kit/cli` to `@rnx-kit/third-party-notices` and
resynced all flags
8 changes: 8 additions & 0 deletions .changeset/hot-carrots-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@rnx-kit/cli": minor
---

Moved Commander config from `@rnx-kit/cli` to `@rnx-kit/third-party-notices` and
resynced all flags. Unfortunately, this means that if you were using the
`rnx-write-third-party-notices` command before, you might have to adjust your
flags when upgrading to this version.
12 changes: 6 additions & 6 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Command } from "@react-native-community/cli-types";
import { alignDepsCommand } from "@rnx-kit/align-deps";
import { writeThirdPartyNoticesCommand } from "@rnx-kit/third-party-notices";
import { rnxBuildCommand } from "./build";
import { rnxBundleCommand } from "./bundle";
import { rnxCleanCommand } from "./clean";
Expand All @@ -8,7 +9,6 @@ import { rnxRamBundleCommand } from "./ram-bundle";
import { rnxRunCommand } from "./run";
import { rnxStartCommand } from "./start";
import { rnxTestCommand } from "./test";
import { rnxWriteThirdPartyNoticesCommand } from "./write-third-party-notices";

export const reactNativeConfig = {
commands: [
Expand All @@ -20,7 +20,7 @@ export const reactNativeConfig = {
rnxCopyAssetsCommand,
alignDepsCommand,
rnxTestCommand,
rnxWriteThirdPartyNoticesCommand,
writeThirdPartyNoticesCommand,
rnxCleanCommand,
] as Command<false>[],
};
Expand All @@ -29,6 +29,10 @@ export const reactNativeConfig = {
export const rnxAlignDeps = alignDepsCommand.func;
export const rnxAlignDepsCommand = alignDepsCommand;

// @rnx-kit/third-party-notices
export const rnxWriteThirdPartyNotices = writeThirdPartyNoticesCommand.func;
export const rnxWriteThirdPartyNoticesCommand = writeThirdPartyNoticesCommand;

export { rnxBuild, rnxBuildCommand } from "./build";
export { rnxBundle, rnxBundleCommand } from "./bundle";
export { rnxClean, rnxCleanCommand } from "./clean";
Expand All @@ -37,7 +41,3 @@ export { rnxRamBundle, rnxRamBundleCommand } from "./ram-bundle";
export { rnxRun, rnxRunCommand } from "./run";
export { rnxStart, rnxStartCommand } from "./start";
export { rnxTest, rnxTestCommand } from "./test";
export {
rnxWriteThirdPartyNotices,
rnxWriteThirdPartyNoticesCommand,
} from "./write-third-party-notices";
85 changes: 0 additions & 85 deletions packages/cli/src/write-third-party-notices.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/third-party-notices/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"yargs": "^16.0.0"
},
"devDependencies": {
"@react-native-community/cli-types": "^14.0.0",
"@rnx-kit/eslint-config": "*",
"@rnx-kit/jest-preset": "*",
"@rnx-kit/metro-serializer": "*",
Expand Down
159 changes: 90 additions & 69 deletions packages/third-party-notices/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,100 @@ import * as yargs from "yargs";
import type { WriteThirdPartyNoticesOptions } from "./types";
import { writeThirdPartyNotices } from "./write-third-party-notices";

function fail(message: string): never {
export const cliOptions = {
"root-path": {
type: "string",
description: "The root of the repo to start resolving modules from",
demandOption: true,
default: process.cwd(),
argsString: "<path>",
},
"source-map-file": {
type: "string",
description: "The source map file to generate license contents for",
demandOption: true,
argsString: "<path>",
},
json: {
type: "boolean",
description: "Output license information as a JSON",
default: false,
},
"output-file": {
type: "string",
description: "The output file to write the license file to",
argsString: "<path>",
},
"ignore-scopes": {
string: true,
array: true,
description: "npm scopes to ignore and not emit license information for",
argsString: "<string>",
},
"ignore-modules": {
string: true,
array: true,
description: "Modules (JS packages) to not emit license information for",
argsString: "<string>",
},
"preamble-text": {
string: true,
array: true,
description:
"A list of lines to prepend at the start of the generated license file",
argsString: "<string>",
},
"additional-text": {
string: true,
array: true,
description:
"A list of lines to append at the end of the generated license file",
argsString: "<string>",
},
"full-license-text": {
type: "boolean",
description: "Include full license text in the JSON output",
default: false,
implies: "json",
},
} as const;

function fail(message: string): void {
error(message);
process.exit(1);
process.exitCode = 1;
}

function getArgs(): WriteThirdPartyNoticesOptions {
const argv = yargs.options({
rootPath: {
type: "string",
describe: "The root of the repo where to start resolving modules from.",
require: true,
},
sourceMapFile: {
type: "string",
describe: "The sourceMap file to generate license contents for.",
require: true,
},
json: {
type: "boolean",
describe: "Output license information as a JSON",
default: false,
},
outputFile: {
type: "string",
describe: "The output file to write the license file to.",
},
ignoreScopes: {
string: true,
array: true,
describe: "Npm scopes to ignore and not emit license information for",
},
ignoreModules: {
string: true,
array: true,
describe: "Modules (js packages) to not emit license information for ",
},
preambleText: {
string: true,
array: true,
describe:
"A list of lines to prepend at the start of the generated license file.",
},
additionalText: {
string: true,
array: true,
describe:
"A list of lines to append at the end of the generated license file.",
},
fullLicenseText: {
type: "boolean",
describe: "Include full license text in the JSON output",
default: false,
implies: "json",
},
}).argv;

const writeTpnArgs: WriteThirdPartyNoticesOptions = argv;

if (!writeTpnArgs.outputFile) {
writeTpnArgs.outputFile =
writeTpnArgs.sourceMapFile?.substring(
function parseArgs(): WriteThirdPartyNoticesOptions {
const {
"root-path": rootPath,
"source-map-file": sourceMapFile,
json,
"output-file": outputFile,
"ignore-scopes": ignoreScopes,
"ignore-modules": ignoreModules,
"preamble-text": preambleText,
"additional-text": additionalText,
"full-license-text": fullLicenseText,
} = yargs.options(cliOptions).argv;
return {
rootPath,
sourceMapFile,
json,
outputFile:
outputFile ||
sourceMapFile.substring(
0,
writeTpnArgs.sourceMapFile.length -
path.extname(writeTpnArgs.sourceMapFile).length
) + ".tpn";
}

return writeTpnArgs;
sourceMapFile.length - path.extname(sourceMapFile).length
) + ".tpn",
ignoreScopes,
ignoreModules,
preambleText,
additionalText,
fullLicenseText,
};
}

async function main(): Promise<void> {
const writeTpnArgs = getArgs();
await writeThirdPartyNotices(writeTpnArgs);
if (require.main === module) {
const writeTpnArgs = parseArgs();
writeThirdPartyNotices(writeTpnArgs).catch(fail);
}

main().catch((err: string) => fail(err));
58 changes: 58 additions & 0 deletions packages/third-party-notices/src/commander.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { Config } from "@react-native-community/cli-types";
import { cliOptions } from "./cli";
import type { WriteThirdPartyNoticesOptions } from "./types";
import { writeThirdPartyNotices } from "./write-third-party-notices";

type InputArgs = WriteThirdPartyNoticesOptions & {
ignoreScopes?: string;
ignoreModules?: string;
preambleText?: string;
additionalText?: string;
};

function rnxWriteThirdPartyNotices(
_argv: string[],
_config: Config,
{
additionalText,
fullLicenseText,
ignoreModules,
ignoreScopes,
json,
outputFile,
preambleText,
rootPath,
sourceMapFile,
}: InputArgs
): void {
// react-native-cli is not as rich as yargs, so we have to perform a mapping.
writeThirdPartyNotices({
rootPath,
sourceMapFile,
json,
outputFile,
ignoreScopes: ignoreScopes?.split(","),
ignoreModules: ignoreModules?.split(","),
preambleText: preambleText ? [preambleText] : undefined,
additionalText: additionalText ? [additionalText] : undefined,
fullLicenseText,
});
}

export const writeThirdPartyNoticesCommand = {
name: "rnx-write-third-party-notices",
description: "Writes third party notices based on the given bundle",
func: rnxWriteThirdPartyNotices,
get options() {
return Object.entries(cliOptions).map(([flag, options]) => {
return {
name:
"argsString" in options
? `--${flag} ${options.argsString}`
: `--${flag}`,
description: options.description,
default: "default" in options ? options.default : undefined,
};
});
},
};
1 change: 1 addition & 0 deletions packages/third-party-notices/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
writeThirdPartyNoticesFromMap,
} from "./write-third-party-notices";

export { writeThirdPartyNoticesCommand } from "./commander";
export { writeThirdPartyNotices } from "./write-third-party-notices";

export function ThirdPartyNotices(
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4516,6 +4516,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@rnx-kit/third-party-notices@workspace:packages/third-party-notices"
dependencies:
"@react-native-community/cli-types": "npm:^14.0.0"
"@rnx-kit/console": "npm:^1.0.11"
"@rnx-kit/eslint-config": "npm:*"
"@rnx-kit/jest-preset": "npm:*"
Expand Down

0 comments on commit 63654fa

Please sign in to comment.