create-tezos-smart-contract
Version:
Node.js toolset to write, test and deploy Tezos smart contracts
73 lines (72 loc) • 3.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bundle = exports.createBundle = exports.listFiles = exports.mkdir = exports.readJSONFile = exports.writeJSONFile = exports.readTextFile = exports.writeTextFile = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const writeTextFile = async (path, data, encoding = 'utf-8') => new Promise((resolve, reject) => {
fs_1.default.writeFile(path, data, { encoding }, (err) => err ? reject(err) : resolve(true));
});
exports.writeTextFile = writeTextFile;
const readTextFile = async (path, encoding = 'utf-8') => new Promise((resolve, reject) => {
fs_1.default.readFile(path, { encoding }, (err, data) => err ? reject(err) : resolve(data));
});
exports.readTextFile = readTextFile;
const writeJSONFile = async (path, data, beautify = true) => (0, exports.writeTextFile)(path, beautify ? JSON.stringify(data, null, 2) : JSON.stringify(data));
exports.writeJSONFile = writeJSONFile;
const readJSONFile = async (path) => JSON.parse(await (0, exports.readTextFile)(path));
exports.readJSONFile = readJSONFile;
const mkdir = async (path) => new Promise((resolve, reject) => {
fs_1.default.mkdir(path, (err) => err ? reject(err) : resolve(true));
});
exports.mkdir = mkdir;
const listFiles = async (path, options) => new Promise((resolve, reject) => {
fs_1.default.readdir(path, options, (err, files) => {
err ? reject(err) : resolve(files);
});
});
exports.listFiles = listFiles;
const createBundle = async (folder, basePath) => {
if (!fs_1.default.existsSync(basePath))
throw new Error("Invalid base path provided for creating Bundle: folder does not exist.");
const bundleBasePath = path_1.default.join(basePath, folder);
if (fs_1.default.existsSync(bundleBasePath))
throw new Error("A folder with that name already exists!");
await (0, exports.mkdir)(bundleBasePath);
return new Bundle(bundleBasePath);
};
exports.createBundle = createBundle;
class Bundle {
constructor(basePath = process.cwd()) {
this.basePath = basePath;
if (!fs_1.default.existsSync(basePath))
throw new Error("Invalid base path provided to Bundle: folder does not exist.");
}
getPath(...name) {
return path_1.default.resolve.apply(this, [this.basePath, ...name]);
}
async writeTextFile(filePath, data, encoding = 'utf-8') {
return (0, exports.writeTextFile)(this.getPath(filePath), data, encoding);
}
async readTextFile(filePath, encoding = 'utf-8') {
return (0, exports.readTextFile)(this.getPath(filePath), encoding);
}
async writeJSONFile(filePath, data, beautify = true) {
return (0, exports.writeJSONFile)(this.getPath(filePath), data, beautify);
}
async readJSONFile(filePath) {
return (0, exports.readJSONFile)(this.getPath(filePath));
}
exists(path) {
return fs_1.default.existsSync(this.getPath(path));
}
async makeDir(folderPath) {
return (0, exports.mkdir)(this.getPath(folderPath));
}
async listFiles(folderPath) {
return (0, exports.listFiles)(this.getPath(folderPath));
}
}
exports.Bundle = Bundle;