@matterlabs/hardhat-zksync-deploy
Version:
Hardhat plugin to deploy smart contracts into the ZKsync network
167 lines • 6.85 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScriptManager = void 0;
const fs_1 = require("fs");
const path = __importStar(require("path"));
const glob_1 = require("glob");
const errors_1 = require("./errors");
const constants_1 = require("./constants");
class ScriptManager {
constructor(_hre) {
this._hre = _hre;
this.deployPaths = _hre.network.deployPaths;
this.funcByFilePath = {};
this.filePaths = [];
}
async findAllDeployScripts() {
let files = [];
for (const dir of this.deployPaths) {
if (!(0, fs_1.existsSync)(dir)) {
throw new errors_1.ZkSyncDeployPluginError(`Deploy folder '${dir}' not found.`);
}
const [tsFilesInDir, jsFilesInDir] = await Promise.all([
await (0, glob_1.glob)(path.join(dir, '**', '*.ts').replace(/\\/g, '/'), {}),
await (0, glob_1.glob)(path.join(dir, '**', '*.js').replace(/\\/g, '/'), {}),
]);
const filesInDir = tsFilesInDir.concat(jsFilesInDir);
filesInDir.sort();
files = files.concat(filesInDir);
}
return files;
}
findDeployScript(script) {
for (const dir of this.deployPaths) {
if (!(0, fs_1.existsSync)(dir)) {
continue;
}
const matchedFiles = glob_1.glob.sync(path.join(dir, '**', script).replace(/\\/g, '/'));
if (matchedFiles.length) {
return matchedFiles[0];
}
}
throw new errors_1.ZkSyncDeployPluginError(`Deploy script '${script}' not found, in deploy folders:\n${this.deployPaths.join(',\n')}.`);
}
async callDeployScripts(targetScript, tags) {
let scripts = [];
if (targetScript === '') {
scripts = await this.findAllDeployScripts();
}
else {
scripts = [this.findDeployScript(targetScript)];
}
const filePathsByTag = await this.collectTags(scripts, tags);
const scriptsToRun = await this.getScriptsToRun(filePathsByTag);
for (const script of scriptsToRun) {
await this._runScript(script);
}
}
async _runScript(script) {
const deployFn = await this._getDeployFunc(script);
await deployFn(this._hre);
}
async _getDeployFunc(script) {
delete require.cache[script];
let deployFn = require(script);
if (typeof deployFn.default === 'function') {
deployFn = deployFn.default;
}
if (typeof deployFn !== 'function') {
throw new errors_1.ZkSyncDeployPluginError('Deploy function does not exist or exported invalidly.');
}
return deployFn;
}
async collectTags(scripts, tags) {
const filePathsByTag = {};
// Clear state every time collecting tags is executed
this.filePaths = [];
this.funcByFilePath = [];
for (const script of scripts) {
const filePath = path.resolve(script);
const deployFn = await this._getDeployFunc(filePath);
this.funcByFilePath[filePath] = deployFn;
let scriptTags = deployFn.tags;
if (scriptTags !== undefined) {
if (typeof scriptTags === 'string') {
scriptTags = [scriptTags];
}
}
else {
scriptTags = ['default'];
}
for (const tag of scriptTags) {
if (tag.includes(',')) {
throw new errors_1.ZkSyncDeployPluginError('Tag cannot contains commas.');
}
const tagFilePaths = filePathsByTag[tag] || [];
filePathsByTag[tag] = tagFilePaths;
tagFilePaths.push(filePath);
}
if (tags !== undefined) {
const filteredTags = tags.filter((value) => scriptTags.includes(value));
if (filteredTags.length) {
this.filePaths.push({ priority: deployFn.priority ?? constants_1.SCRIPT_DEFAULT_PRIORITY, path: filePath });
}
}
else {
this.filePaths.push({ priority: deployFn.priority ?? constants_1.SCRIPT_DEFAULT_PRIORITY, path: filePath });
}
}
return filePathsByTag;
}
async getScriptsToRun(filePathsByTag) {
const filePathRegistered = {};
const scriptsToRun = [];
const recurseDependencies = (filePath) => {
if (filePathRegistered[filePath])
return;
const deployFn = this.funcByFilePath[filePath];
if (deployFn.dependencies) {
for (const dependency of deployFn.dependencies) {
const tagFilePaths = filePathsByTag[dependency];
if (!tagFilePaths) {
throw new errors_1.ZkSyncDeployPluginError(`Not found tag at script: ${filePath} for dependency: ${dependency}`);
}
if (tagFilePaths.length) {
for (const tagFilePath of tagFilePaths) {
recurseDependencies(tagFilePath);
}
}
}
}
if (!filePathRegistered[filePath]) {
scriptsToRun.push(filePath);
filePathRegistered[filePath] = true;
}
};
const sortedFiles = this.filePaths.sort((a, b) => b.priority - a.priority).flatMap((a) => a.path);
for (const filePath of sortedFiles) {
recurseDependencies(filePath);
}
return scriptsToRun;
}
}
exports.ScriptManager = ScriptManager;
//# sourceMappingURL=script-manager.js.map