Skip to content

Commit

Permalink
Copy LICENSE files when building packages
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwooding committed Oct 25, 2022
1 parent b81a293 commit 5c6ecd7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/slow-buckets-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'modular-scripts': minor
---

- Fix prefixed logger debug method logging as info
- Copy LICENSE files when building packages
3 changes: 3 additions & 0 deletions packages/modular-scripts/src/__tests__/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('WHEN building with preserve modules', () => {
"dist-es",
"dist-types",
"README.md",
"LICENSE",
],
"main": "dist-cjs/index.js",
"modular": Object {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions packages/modular-scripts/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ describe('modular-scripts', () => {
"dist-es",
"dist-types",
"README.md",
"LICENSE",
],
"main": "dist-cjs/index.js",
"modular": Object {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -357,6 +359,7 @@ describe('modular-scripts', () => {
"dist-es",
"dist-types",
"README.md",
"LICENSE",
],
"main": "dist-cjs/index.js",
"modular": Object {
Expand All @@ -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
Expand Down Expand Up @@ -433,6 +437,7 @@ describe('modular-scripts', () => {
"dist-es",
"dist-types",
"README.md",
"LICENSE",
],
"main": "dist-cjs/nested-sample-package.cjs.js",
"modular": Object {
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions packages/modular-scripts/src/build/buildPackage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -79,6 +80,8 @@ export async function buildPackage(
);
});

await maybeCopyRootLicense(target, targetOutputDirectory);

/// and... that's it
logger.log(`built ${target} in ${targetOutputDirectory}`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ export async function makeBundle(
'dist-es',
'dist-types',
'README.md',
'LICENSE',
]),
};
}
Original file line number Diff line number Diff line change
@@ -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));
});
}
}
}

0 comments on commit 5c6ecd7

Please sign in to comment.