Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Generate LICENCE.txt on build
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin MOUTY committed Oct 30, 2024
1 parent 044a046 commit 49c9bc5
Show file tree
Hide file tree
Showing 4 changed files with 405 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .config/webpack/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import ESLintPlugin from 'eslint-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import LiveReloadPlugin from 'webpack-livereload-plugin';
import path from 'path';
import WebpackHookPlugin from 'webpack-hook-plugin';
import ReplaceInFileWebpackPlugin from 'replace-in-file-webpack-plugin';
import { Configuration } from 'webpack';

import { Configuration } from 'webpack';
import { getPackageJson, getPluginJson, hasReadme, getEntries, isWSL } from './utils';
import { SOURCE_DIR, DIST_DIR } from './constants';

Expand Down Expand Up @@ -195,6 +196,9 @@ const config = async (env): Promise<Configuration> => {
}),
]
: []),
new WebpackHookPlugin({
onBuildEnd: ['node generate-licenses.js']
})
],

resolve: {
Expand Down
33 changes: 33 additions & 0 deletions generate-licenses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

// Run license-checker to generate a JSON file
execSync('license-checker --json --production > licenses.json', { stdio: 'inherit' });

// Read the licenses.json file
const licensesFilePath = path.join(__dirname, 'licenses.json');
const licensesData = JSON.parse(fs.readFileSync(licensesFilePath, 'utf8'));

// Extract unique licenses and packages
const licensesSet = new Set();
const packages = [];

for (const [packageName, licenseInfo] of Object.entries(licensesData)) {
const license = licenseInfo.licenses || 'Unknown';
licensesSet.add(license);
packages.push(`${packageName}; ${license}`);
}

// Convert sets to arrays and sort them
const licenses = Array.from(licensesSet).sort();
const sortedPackages = packages.sort();

// Generate the output string
const output = `Licences:\n${licenses.join('\n')}\n\nPackages:\n${sortedPackages.join('\n')}`;

// Write the output to a file
const outputFilePath = path.join(__dirname, 'dist/LICENCES.txt');
fs.writeFileSync(outputFilePath, output, 'utf8');

console.log('License information has been written to licenses-output.txt');
Loading

0 comments on commit 49c9bc5

Please sign in to comment.