Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/steps/gen-ext-scaffolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ function assertUnreachable(_x: never): never {
throw new Error("Didn't expect to get here");
}

/**
* The SPDX text for a licence, or an empty string when it isn't a known SPDX
* identifier (e.g. "proprietary" or a free-form "© Some Company").
*/
export function licenseTextFor(licenseType: unknown): string {
if (typeof licenseType !== 'string' || !licenseType) return '';

const id = [...(spdxLicenseListSimple as Set<string>)].find((l) => l.toLowerCase() === licenseType.toLowerCase());

return id ? require(`spdx-license-list/licenses/${id}`).licenseText : '';
}

export const EXTENSION_PARAMS = [
'packageName',
'packageDescription',
Expand Down Expand Up @@ -170,8 +182,7 @@ function paramNamesToDef(name: ExtensionParams): TemplateParam<string, Extension
return {
name,
uses: ['licenseType'],
compute: async (_paths, licenseType: string) =>
licenseType && licenseType !== 'proprietary' ? require(`spdx-license-list/licenses/${licenseType}`).licenseText : '',
compute: async (_paths, licenseType: string) => licenseTextFor(licenseType),
};

case 'extensionId':
Expand Down
19 changes: 18 additions & 1 deletion test/steps/gen-ext-scaffolder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { genExtScaffolder } from '../../src/steps/gen-ext-scaffolder';
import { genExtScaffolder, licenseTextFor } from '../../src/steps/gen-ext-scaffolder';

describe('genExtScaffolder', function () {
it('validates', async function () {
Expand All @@ -7,3 +7,20 @@ describe('genExtScaffolder', function () {
await scaffolder.validate();
});
});

describe('licenseTextFor', function () {
it('returns the SPDX text for a known licence', function () {
expect(licenseTextFor('MIT')).toContain('Permission is hereby granted');
});

it('matches SPDX identifiers case-insensitively', function () {
expect(licenseTextFor('mit')).toBe(licenseTextFor('MIT'));
});

it.each(['proprietary', 'PROPRIETARY', '© Glowing Blue AG', '', undefined, ['MIT', 'GPL-3.0-only']])(
'returns an empty string for %p',
function (license) {
expect(licenseTextFor(license)).toBe('');
}
);
});
Loading