@haechi-labs/henesis-cli
Version:
🚀 Command Line Interface tool to Utilize henesis
94 lines • 3.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const command_1 = require("@oclif/command");
const fs_1 = require("fs");
const path_1 = require("path");
const base_1 = tslib_1.__importDefault(require("../common/base"));
const promise_1 = tslib_1.__importDefault(require("simple-git/promise"));
const TEMPLATE_DIR = path_1.join(__dirname, '..', '..', 'templates');
class Init extends base_1.default {
async initTemplate(destinationPath, withForce = false, insideDir) {
await this.initConfiguration(destinationPath, withForce);
if (!insideDir) {
if (!fs_1.existsSync(`${destinationPath}/contracts`)) {
fs_1.mkdirSync(`${destinationPath}/contracts`);
}
fs_1.writeFileSync(path_1.join(`${destinationPath}/contracts`, 'example.sol'), withForce ? fs_1.constants.COPYFILE_FICLONE : undefined);
}
}
async initConfiguration(destinationPath, withForce = false) {
fs_1.copyFileSync(path_1.join(`${TEMPLATE_DIR}`, 'henesis.yaml'), path_1.join(`${destinationPath}`, 'henesis.yaml'), withForce ? fs_1.constants.COPYFILE_FICLONE : undefined);
}
async gitInit(url, destinationPath) {
await promise_1.default().clone(url, destinationPath);
}
setDirectoryName(currentPath, flags) {
let name;
if (typeof flags.git !== 'undefined') {
const words = flags.git.split('/').reverse();
name = words[0].replace(/.git/gi, '');
}
else if (typeof flags.name !== 'undefined') {
name = flags.name;
}
else if (typeof flags.name === 'undefined' &&
typeof flags.git === 'undefined') {
name = 'henesis';
}
return `${currentPath}/${name}`;
}
async run() {
const currentPath = process.cwd() || '.';
const contractsPath = `${currentPath}/contracts`;
const { flags } = this.parse(Init);
if (fs_1.existsSync(contractsPath)) {
if (fs_1.existsSync(`${currentPath}/henesis.yaml`) && flags.force === false) {
this.error(`The henesis.yaml configuration file already exists at the current path.`, { code: '361' });
}
await this.initTemplate(currentPath, flags.force, true);
}
else {
const destinationPath = this.setDirectoryName(currentPath, flags);
if (fs_1.existsSync(destinationPath) && flags.force === false) {
this.error(`The henesis directory already exists at the current path.`, { code: '362' });
}
if (!fs_1.existsSync(destinationPath)) {
fs_1.mkdirSync(`${destinationPath}`);
}
if (typeof flags.git !== 'undefined') {
await this.gitInit(flags.git, destinationPath);
}
else {
await this.initTemplate(destinationPath, flags.force, false);
}
}
this.log(`henesis initialization has been completed.`);
}
}
exports.default = Init;
Init.description = 'set up configuration file required for your project';
Init.examples = [
`$ henesis init
`,
];
Init.flags = {
help: command_1.flags.help({ char: 'h' }),
// flag with a value (-n, --name=VALUE)
name: command_1.flags.string({
char: 'n',
description: 'name to project',
}),
// flag with no value (-f, --force)
force: command_1.flags.boolean({
char: 'f',
description: 'Delete existing configuration file and create new one.',
default: false,
}),
git: command_1.flags.string({
char: 'g',
description: 'Generates a template through the passed git address. required git client',
}),
};
Init.args = [];
//# sourceMappingURL=init.js.map