Skip to content

Commit

Permalink
feat(third-party-notices): add full license info (#3165)
Browse files Browse the repository at this point in the history
  • Loading branch information
AAHAbbas authored May 30, 2024
1 parent 9ddb5f8 commit 758cc29
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-doors-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/third-party-notices": minor
---

Add full license text and format the JSON output
30 changes: 14 additions & 16 deletions packages/third-party-notices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ output file.

This package works for npm, yarn and pnpm package layouts formats.

At the moment this package only supports webpack based bundles, there is nothing
preventing adding metro support, the current customers of this module are
basedon webpack at the moment.

## Usage

### Commandline
Expand All @@ -32,23 +28,25 @@ npx @rnx-kit/third-party-notices \

```
Options:
--help Show help [boolean]
--version Show version number [boolean]
--rootPath The root of the repo where to start resolving modules from.
--help Show help [boolean]
--version Show version number [boolean]
--rootPath The root of the repo where to start resolving modules from.
[string] [required]
--sourceMapFile The sourceMap file to generate licence contents for.
--sourceMapFile The sourceMap file to generate license contents for.
[string] [required]
--outputFile The output file to write the licence file to. [string]
--json Output license information as a JSON
--json Output license information as a JSON
[boolean] [default: false]
--ignoreScopes Npm scopes to ignore and not emit licence information for
--outputFile The output file to write the license file to. [string]
--ignoreScopes Npm scopes to ignore and not emit license information for
[array]
--ignoreModules Modules (js packages) to not emit licence information for
--ignoreModules Modules (js packages) to not emit license information for
[array]
--preambleText A list of lines to prepend at the start of the generated
licence file. [array]
--additionalText A list of lines to append at the end of the generated
licence file. [array]
--preambleText A list of lines to prepend at the start of the generated
license file. [array]
--additionalText A list of lines to append at the end of the generated
license file. [array]
--fullLicenseText Include full license text in the JSON output
[boolean] [default: false]
```

### As a library
Expand Down
6 changes: 6 additions & 0 deletions packages/third-party-notices/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ function getArgs(): WriteThirdPartyNoticesOptions {
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;
Expand Down
45 changes: 29 additions & 16 deletions packages/third-party-notices/src/output/json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { findPackage, readPackage } from "@rnx-kit/tools-node/package";
import type { License } from "../types";
import type { License, LicenseJSONInfo } from "../types";

function getPackageAuthor(modulePath: string): string | undefined {
const pkgFile = findPackage(modulePath);
Expand All @@ -26,20 +26,33 @@ function parseCopyright(
return m[0].trim();
}

export function createLicenseJSON(licenses: License[]): string {
return JSON.stringify({
packages: licenses.map(
({ name, path: modulePath, version, license, licenseText }) => {
if (!license) {
throw new Error(`No license for ${name}`);
export function createLicenseJSON(
licenses: License[],
fullLicenseText?: boolean
): string {
return JSON.stringify(
{
packages: licenses.map(
({ name, path: modulePath, version, license, licenseText }) => {
if (!license) {
throw new Error(`No license for ${name}`);
}
const info: LicenseJSONInfo = {
name,
version,
license,
copyright: parseCopyright(modulePath, licenseText),
};

if (fullLicenseText) {
info.text = licenseText?.replace(/\r\n|\r|\n/g, "\n").trim();
}

return info;
}
return {
name,
version,
license,
copyright: parseCopyright(modulePath, licenseText),
};
}
),
});
),
},
null,
2
);
}
9 changes: 9 additions & 0 deletions packages/third-party-notices/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export type License = ModuleNamePathPair & {
noticeText?: string;
};

export type LicenseJSONInfo = {
name: string;
version: string;
license: string;
copyright: string;
text?: string;
};

export type SourceMap = {
sources: string[];
sections?: SourceSection[];
Expand All @@ -31,4 +39,5 @@ export type WriteThirdPartyNoticesOptions = {
ignoreModules?: string[];
preambleText?: string[];
additionalText?: string[];
fullLicenseText?: boolean;
};
17 changes: 12 additions & 5 deletions packages/third-party-notices/src/write-third-party-notices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ export async function writeThirdPartyNotices(
options: WriteThirdPartyNoticesOptions
): Promise<void> {
const fileOptions = { encoding: "utf-8" } as const;
const { additionalText, json, outputFile, preambleText, sourceMapFile } =
options;
const {
additionalText,
json,
outputFile,
preambleText,
sourceMapFile,
fullLicenseText,
} = options;

// Parse source map file
const sourceMapJson = fs.readFileSync(sourceMapFile, fileOptions);
Expand All @@ -53,7 +59,7 @@ export async function writeThirdPartyNotices(
);
const licenses = await extractLicenses(moduleNameToPathMap);
const outputText = json
? createLicenseJSON(licenses)
? createLicenseJSON(licenses, fullLicenseText)
: createLicenseFileContents(licenses, preambleText, additionalText);

if (outputFile) {
Expand All @@ -67,12 +73,13 @@ export async function writeThirdPartyNoticesFromMap(
options: WriteThirdPartyNoticesOptions,
moduleNameToPathMap: Map<string, string>
): Promise<void> {
const { additionalText, json, preambleText, sourceMapFile } = options;
const { additionalText, json, preambleText, sourceMapFile, fullLicenseText } =
options;
let { outputFile } = options;

const licenses = await extractLicenses(moduleNameToPathMap);
const outputText = json
? createLicenseJSON(licenses)
? createLicenseJSON(licenses, fullLicenseText)
: createLicenseFileContents(licenses, preambleText, additionalText);

if (!outputFile) {
Expand Down
34 changes: 34 additions & 0 deletions packages/third-party-notices/test/licence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,38 @@ describe("license", () => {
],
});
});

test("createLicenseJSON with fullLicenseText", async () => {
const licenses = await getSampleLicenseData();
// escape \n so JSON.parse won't transform \n into actual newlines
const licenseText = createLicenseJSON(licenses, true).replace(
/\\n/g,
"\\\\n"
);

expect(JSON.parse(licenseText)).toEqual({
packages: [
{
copyright: "Microsoft Open Source",
license: "MIT",
name: "@rnx-kit/console",
version: "1.2.3-fixedVersionForTesting",
},
{
copyright:
"Copyright 2010 James Halliday ([email protected]); Modified work Copyright 2014 Contributors ([email protected])",
license: "MIT",
name: "yargs",
version: "1.2.3-fixedVersionForTesting",
text: 'MIT License\\n\\nCopyright 2010 James Halliday ([email protected]); Modified work Copyright 2014 Contributors ([email protected])\\n\\nPermission is hereby granted, free of charge, to any person obtaining a copy\\nof this software and associated documentation files (the "Software"), to deal\\nin the Software without restriction, including without limitation the rights\\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\\ncopies of the Software, and to permit persons to whom the Software is\\nfurnished to do so, subject to the following conditions:\\n\\nThe above copyright notice and this permission notice shall be included in\\nall copies or substantial portions of the Software.\\n\\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\\nTHE SOFTWARE.',
},
{
copyright: "Microsoft Open Source",
license: "Unlicensed",
name: "private-package",
version: "1.0.0",
},
],
});
});
});

0 comments on commit 758cc29

Please sign in to comment.