@truffle/compile-solidity
Version:
Compiler helper and artifact manager for Solidity files
110 lines • 4.77 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;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cache = void 0;
const config_1 = __importDefault(require("@truffle/config"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
// @ts-ignore
const fsPromises = __importStar(require("fs/promises"));
const fileNotFoundMessage = "no such file or directory";
class Cache {
constructor() {
const compilersDir = path_1.default.resolve(config_1.default.getTruffleDataDirectory(), "compilers");
const compilerCachePath = path_1.default.resolve(compilersDir, "node_modules"); // because babel binds to require & does weird things
if (!fs_1.default.existsSync(compilersDir))
fs_1.default.mkdirSync(compilersDir);
if (!fs_1.default.existsSync(compilerCachePath))
fs_1.default.mkdirSync(compilerCachePath); // for 5.0.8 users
this.compilerCachePath = compilerCachePath;
this.memoizedCompilers = new Map();
}
list() {
return __awaiter(this, void 0, void 0, function* () {
return yield fsPromises.readdir(this.compilerCachePath);
});
}
add(code, fileName) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = this.resolve(fileName);
yield fsPromises.writeFile(filePath, code);
this.memoizedCompilers.set(filePath, code);
});
}
has(fileName) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = this.resolve(fileName);
try {
yield fsPromises.stat(filePath);
return true;
}
catch (error) {
// only throw if the error is due to something other than it not existing
if (!error.message.includes(fileNotFoundMessage)) {
throw error;
}
return false;
}
});
}
loadFile(fileName) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = this.resolve(fileName);
if (this.memoizedCompilers.has(filePath)) {
return this.memoizedCompilers.get(filePath);
}
try {
const compiler = (yield fsPromises.readFile(filePath)).toString();
this.memoizedCompilers.set(filePath, compiler);
return compiler;
}
catch (error) {
if (!error.message.includes(fileNotFoundMessage)) {
throw error;
}
else {
throw new Error("The specified file could not be found.");
}
}
});
}
resolve(fileName) {
return path_1.default.resolve(this.compilerCachePath, fileName);
}
}
exports.Cache = Cache;
//# sourceMappingURL=Cache.js.map