hardhat-spdx-license-identifier
Version:
Prepend local Solidity source files with an SPDX License Identifier
45 lines (44 loc) • 2.25 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const package_json_1 = require("../../package.json");
const fs_1 = __importDefault(require("fs"));
const task_names_1 = require("hardhat/builtin-tasks/task-names");
const config_1 = require("hardhat/config");
const plugins_1 = require("hardhat/plugins");
(0, config_1.task)('prepend-spdx-license', 'Prepends SPDX License identifier to local source files', async function (args, hre) {
const config = hre.config.spdxLicenseIdentifier;
const { license } = JSON.parse(fs_1.default.readFileSync(`${hre.config.paths.root}/package.json`, 'utf8'));
if (!license) {
throw new plugins_1.HardhatPluginError(package_json_1.name, 'no license specified in package.json, unable to add SPDX License Identifier to sources');
}
// TODO: forward args from compile task if applicable
const sourcePaths = await hre.run(task_names_1.TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS, args);
const headerBase = '// SPDX-License-Identifier:';
const regexp = new RegExp(`(${headerBase}.*\n*)?`);
const header = `${headerBase} ${license}\n`;
let count = 0;
await Promise.all(sourcePaths.map(async (sourcePath) => {
if (config.only.length && !config.only.some((m) => sourcePath.match(m)))
return;
if (config.except.length &&
config.except.some((m) => sourcePath.match(m)))
return;
// content is read from disk for preprocessor compatibility
const content = fs_1.default.readFileSync(sourcePath).toString();
const partialMatch = content.startsWith(headerBase);
const exactMatch = content.startsWith(header);
if (exactMatch)
return;
if (partialMatch && !hre.config.spdxLicenseIdentifier.overwrite)
return;
const padding = partialMatch ? '' : '\n';
fs_1.default.writeFileSync(sourcePath, content.replace(regexp, header + padding));
count++;
}));
if (count > 0) {
console.log(`Prepended SPDX License Identifier "${license}" to ${count} sources.`);
}
});
;