|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Retrieve the list of released dependencies specified in the project's package.json file. |
| 6 | + * |
| 7 | + * @return {string[]} An array of dependency names defined in the "dependencies" section of the package.json file. |
| 8 | + */ |
| 9 | +function getReleasedDependencies(): string[] { |
| 10 | + const packageJsonPath = path.resolve(__dirname, '../../package.json'); |
| 11 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
| 12 | + |
| 13 | + return Object.keys(packageJson.dependencies); |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Retrieve the copyright banner defined in the esbuild configuration file for the Tasks plugin. |
| 18 | + * |
| 19 | + * The method reads the esbuild configuration file, extracts the banner content using a regular expression, |
| 20 | + * and returns it as a string. Throws an error if the banner is not found. |
| 21 | + * |
| 22 | + * @return {string} The copyright banner extracted from the esbuild configuration file. |
| 23 | + */ |
| 24 | +function getTasksPluginCopyrightBanner(): string { |
| 25 | + const packageEsbuildConfigPath = path.resolve(__dirname, '../../esbuild.config.mjs'); |
| 26 | + const packageEsbuildConfig = fs.readFileSync(packageEsbuildConfigPath, 'utf8'); |
| 27 | + |
| 28 | + // Regular expression to extract the banner content |
| 29 | + const bannerRegex = /const\s+banner\s*=\s*`([\s\S]*?)`;/; |
| 30 | + const match = packageEsbuildConfig.match(bannerRegex); |
| 31 | + |
| 32 | + if (!match) { |
| 33 | + throw new Error('No banner found in esbuild.config.mjs'); |
| 34 | + } |
| 35 | + |
| 36 | + return match[1]; |
| 37 | +} |
| 38 | + |
| 39 | +describe('Check esbuild.config.mjs', () => { |
| 40 | + const bannerContent = getTasksPluginCopyrightBanner(); |
| 41 | + it('check licensing', () => { |
| 42 | + expect(bannerContent).toContain('License obsidian-tasks:\nMIT License'); |
| 43 | + expect(bannerContent).toContain('Clare Macrae, Ilyas Landikov and Martin Schenck'); |
| 44 | + }); |
| 45 | + |
| 46 | + // Check that every released dependency will its licence included in main.js |
| 47 | + it.each(getReleasedDependencies())('check dependency is in banner: "%s"', (filter: string) => { |
| 48 | + // For files that we embed in main.js, we need to honour the project license, |
| 49 | + // which we do by pasting it in to esbuild.config.mjs. |
| 50 | + const expectedLine = `Licence ${filter} (included library):`; |
| 51 | + expect(bannerContent).toContain(expectedLine); |
| 52 | + |
| 53 | + // If this fails, you need to go to: |
| 54 | + // 1. Go to https://classic.yarnpkg.com/lang/en/ |
| 55 | + // 2. Search for the failed package name. |
| 56 | + // 3. Navigate to its GitHub repo. |
| 57 | + // 4. View its LICENSE file. |
| 58 | + // 5. Copy the contents of the LICENSE file. |
| 59 | + // 6. Create a new comment block inside the banner variable in esbuild.config.mjs |
| 60 | + // 7. Paste in a new block, including the library name and the full licence text. |
| 61 | + }); |
| 62 | +}); |
0 commit comments