From 5c6ecd79b31a109f6406c00b945afa6666d73d27 Mon Sep 17 00:00:00 2001 From: joshwooding <12938082+joshwooding@users.noreply.github.com> Date: Sat, 20 Aug 2022 14:06:38 +0100 Subject: [PATCH] Copy LICENSE files when building packages --- .changeset/slow-buckets-wash.md | 6 ++++ .../src/__tests__/build.test.ts | 3 ++ .../src/__tests__/index.test.ts | 6 ++++ .../src/build/buildPackage/index.ts | 3 ++ .../src/build/buildPackage/makeBundle.ts | 1 + .../buildPackage/maybeCopyRootLicense.ts | 35 +++++++++++++++++++ 6 files changed, 54 insertions(+) create mode 100644 .changeset/slow-buckets-wash.md create mode 100644 packages/modular-scripts/src/build/buildPackage/maybeCopyRootLicense.ts diff --git a/.changeset/slow-buckets-wash.md b/.changeset/slow-buckets-wash.md new file mode 100644 index 0000000000..45729be9bb --- /dev/null +++ b/.changeset/slow-buckets-wash.md @@ -0,0 +1,6 @@ +--- +'modular-scripts': minor +--- + +- Fix prefixed logger debug method logging as info +- Copy LICENSE files when building packages diff --git a/packages/modular-scripts/src/__tests__/build.test.ts b/packages/modular-scripts/src/__tests__/build.test.ts index 2e262795cb..935de1d903 100644 --- a/packages/modular-scripts/src/__tests__/build.test.ts +++ b/packages/modular-scripts/src/__tests__/build.test.ts @@ -33,6 +33,7 @@ describe('WHEN building with preserve modules', () => { "dist-es", "dist-types", "README.md", + "LICENSE", ], "main": "dist-cjs/index.js", "modular": Object { @@ -51,6 +52,7 @@ describe('WHEN building with preserve modules', () => { expect(tree(path.join(modularRoot, 'dist', packageName))) .toMatchInlineSnapshot(` "sample-async-package + ├─ LICENSE #1gat5ri ├─ dist-cjs │ ├─ index.js #p1m6x9 │ ├─ index.js.map #16jes1h @@ -168,6 +170,7 @@ describe('WHEN building packages with private cross-package dependencies', () => expect(tree(path.join(modularRoot, 'dist', dependentPackage))) .toMatchInlineSnapshot(` "sample-depending-package + ├─ LICENSE #1gat5ri ├─ dist-cjs │ ├─ index.js #1m9v9ya │ ├─ index.js.map #79ot9r diff --git a/packages/modular-scripts/src/__tests__/index.test.ts b/packages/modular-scripts/src/__tests__/index.test.ts index c5044def9b..c806ab9459 100644 --- a/packages/modular-scripts/src/__tests__/index.test.ts +++ b/packages/modular-scripts/src/__tests__/index.test.ts @@ -210,6 +210,7 @@ describe('modular-scripts', () => { "dist-es", "dist-types", "README.md", + "LICENSE", ], "main": "dist-cjs/index.js", "modular": Object { @@ -288,6 +289,7 @@ describe('modular-scripts', () => { expect(tree(path.join(modularRoot, 'dist', 'sample-view'))) .toMatchInlineSnapshot(` "sample-view + ├─ LICENSE #1gat5ri ├─ dist-cjs │ ├─ index.js #p1m6x9 │ ├─ index.js.map #16jes1h @@ -357,6 +359,7 @@ describe('modular-scripts', () => { "dist-es", "dist-types", "README.md", + "LICENSE", ], "main": "dist-cjs/index.js", "modular": Object { @@ -375,6 +378,7 @@ describe('modular-scripts', () => { expect(tree(path.join(modularRoot, 'dist', 'sample-package'))) .toMatchInlineSnapshot(` "sample-package + ├─ LICENSE #1gat5ri ├─ dist-cjs │ ├─ index.js #p1m6x9 │ ├─ index.js.map #16jes1h @@ -433,6 +437,7 @@ describe('modular-scripts', () => { "dist-es", "dist-types", "README.md", + "LICENSE", ], "main": "dist-cjs/nested-sample-package.cjs.js", "modular": Object { @@ -451,6 +456,7 @@ describe('modular-scripts', () => { expect(tree(path.join(modularRoot, 'dist', 'nested-sample-package'))) .toMatchInlineSnapshot(` "nested-sample-package + ├─ LICENSE #1gat5ri ├─ dist-cjs │ ├─ nested-sample-package.cjs.js #kv2xzp │ └─ nested-sample-package.cjs.js.map #bgpzsg diff --git a/packages/modular-scripts/src/build/buildPackage/index.ts b/packages/modular-scripts/src/build/buildPackage/index.ts index 2df5145784..a070f8a8f1 100644 --- a/packages/modular-scripts/src/build/buildPackage/index.ts +++ b/packages/modular-scripts/src/build/buildPackage/index.ts @@ -16,6 +16,7 @@ import getModularRoot from '../../utils/getModularRoot'; import { makeBundle } from './makeBundle'; import { makeTypings } from './makeTypings'; import getRelativeLocation from '../../utils/getRelativeLocation'; +import { maybeCopyRootLicense } from './maybeCopyRootLicense'; const outputDirectory = 'dist'; @@ -79,6 +80,8 @@ export async function buildPackage( ); }); + await maybeCopyRootLicense(target, targetOutputDirectory); + /// and... that's it logger.log(`built ${target} in ${targetOutputDirectory}`); } diff --git a/packages/modular-scripts/src/build/buildPackage/makeBundle.ts b/packages/modular-scripts/src/build/buildPackage/makeBundle.ts index 1acd735f27..6b74c6fc94 100644 --- a/packages/modular-scripts/src/build/buildPackage/makeBundle.ts +++ b/packages/modular-scripts/src/build/buildPackage/makeBundle.ts @@ -313,6 +313,7 @@ export async function makeBundle( 'dist-es', 'dist-types', 'README.md', + 'LICENSE', ]), }; } diff --git a/packages/modular-scripts/src/build/buildPackage/maybeCopyRootLicense.ts b/packages/modular-scripts/src/build/buildPackage/maybeCopyRootLicense.ts new file mode 100644 index 0000000000..656283dfc7 --- /dev/null +++ b/packages/modular-scripts/src/build/buildPackage/maybeCopyRootLicense.ts @@ -0,0 +1,35 @@ +import * as fs from 'fs-extra'; +import * as path from 'path'; +import globby from 'globby'; +import getPrefixedLogger from '../../utils/getPrefixedLogger'; +import getModularRoot from '../../utils/getModularRoot'; + +const licenseGlob = 'LICEN@(C|S)E*'; + +export async function maybeCopyRootLicense( + target: string, + targetOutputDirectory: string, +) { + const logger = getPrefixedLogger(target); + const modularRoot = getModularRoot(); + const matches = globby.sync(path.join(targetOutputDirectory, licenseGlob), { + cwd: modularRoot, + onlyFiles: true, + }); + if (matches.length === 0) { + logger.debug( + `No license found in ${targetOutputDirectory}. Looking for root license.`, + ); + const rootLicenses = globby.sync(path.join(modularRoot, licenseGlob), { + cwd: modularRoot, + onlyFiles: true, + }); + if (rootLicenses.length > 0) { + rootLicenses.forEach((license) => { + const filename = path.basename(license); + logger.log(`Copying ${filename} found in ${modularRoot}`); + fs.copyFileSync(license, path.join(targetOutputDirectory, filename)); + }); + } + } +}