2020-11-14 01:38:56 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
require('require-yaml');
|
|
|
|
require('colors');
|
|
|
|
const getopts = require('getopts');
|
|
|
|
const packageJson = require('./package.json');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const ini = require('ini');
|
|
|
|
const path = require('path');
|
|
|
|
const mysql = require('mysql2/promise');
|
|
|
|
const nodegit = require('nodegit');
|
2022-12-21 12:34:17 +00:00
|
|
|
const camelToSnake = require('./lib/util').camelToSnake;
|
2020-12-04 16:30:26 +00:00
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
const scriptRegex = /^[0-9]{2}-[a-zA-Z0-9_]+(?:\.(?!undo)([a-zA-Z0-9_]+))?(\.undo)?\.sql$/;
|
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
class Myt {
|
2022-12-29 09:15:02 +00:00
|
|
|
static usage = {
|
|
|
|
description: 'Utility for database versioning',
|
|
|
|
params: {
|
|
|
|
remote: 'Name of remote to use',
|
|
|
|
workspace: 'The base directory of the project',
|
|
|
|
debug: 'Wether to enable debug mode',
|
|
|
|
version: 'Display the version number and exit',
|
|
|
|
help: 'Display this help message'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static opts = {
|
|
|
|
alias: {
|
|
|
|
remote: 'r',
|
|
|
|
workspace: 'w',
|
|
|
|
debug: 'd',
|
|
|
|
version: 'v',
|
|
|
|
help: 'h'
|
|
|
|
},
|
|
|
|
boolean: [
|
|
|
|
'debug',
|
|
|
|
'version',
|
|
|
|
'help'
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2024-01-04 12:02:40 +00:00
|
|
|
async cli(Command) {
|
|
|
|
this.cliMode = true;
|
|
|
|
|
2022-12-21 12:34:17 +00:00
|
|
|
console.log(
|
2022-12-21 13:17:50 +00:00
|
|
|
'Myt'.green,
|
2022-12-21 12:34:17 +00:00
|
|
|
`v${packageJson.version}`.magenta
|
|
|
|
);
|
2022-12-29 16:58:40 +00:00
|
|
|
this.packageJson = packageJson;
|
2022-12-21 12:34:17 +00:00
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
let baseOpts = this.constructor.opts;
|
|
|
|
baseOpts.default = Object.assign(baseOpts.default || {}, {
|
|
|
|
workspace: process.cwd()
|
|
|
|
});
|
2021-10-25 13:38:07 +00:00
|
|
|
const opts = this.getopts(baseOpts);
|
2020-12-04 16:30:26 +00:00
|
|
|
|
2022-06-09 09:42:03 +00:00
|
|
|
if (opts.debug) {
|
|
|
|
console.warn('Debug mode enabled.'.yellow);
|
|
|
|
console.log('Global options:'.magenta, opts);
|
|
|
|
}
|
|
|
|
|
2022-02-07 11:46:14 +00:00
|
|
|
if (opts.version)
|
|
|
|
process.exit(0);
|
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
try {
|
2021-10-25 13:38:07 +00:00
|
|
|
const commandName = opts._[0];
|
2022-12-29 09:15:02 +00:00
|
|
|
if (!Command && commandName) {
|
2022-12-21 12:34:17 +00:00
|
|
|
if (!/^[a-z]+$/.test(commandName))
|
|
|
|
throw new Error (`Invalid command name '${commandName}'`);
|
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
const commandFile = path.join(__dirname, `myt-${commandName}.js`);
|
2022-12-21 12:34:17 +00:00
|
|
|
|
|
|
|
if (!await fs.pathExists(commandFile))
|
2020-12-04 16:30:26 +00:00
|
|
|
throw new Error (`Unknown command '${commandName}'`);
|
2022-12-29 09:15:02 +00:00
|
|
|
Command = require(commandFile);
|
2020-12-04 16:30:26 +00:00
|
|
|
}
|
2021-10-25 13:38:07 +00:00
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
if (!Command) {
|
|
|
|
this.showHelp(baseOpts, this.constructor.usage);
|
2021-10-25 13:38:07 +00:00
|
|
|
process.exit(0);
|
|
|
|
}
|
2022-02-07 10:46:13 +00:00
|
|
|
|
2022-04-04 18:53:18 +00:00
|
|
|
const allOpts = Object.assign({}, baseOpts);
|
2022-04-04 19:14:04 +00:00
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
if (Command.opts)
|
|
|
|
for (const key in Command.opts) {
|
2022-04-04 18:53:18 +00:00
|
|
|
const baseValue = baseOpts[key];
|
2022-12-29 09:15:02 +00:00
|
|
|
const cmdValue = Command.opts[key];
|
2022-04-04 18:53:18 +00:00
|
|
|
if (Array.isArray(baseValue))
|
|
|
|
allOpts[key] = baseValue.concat(cmdValue);
|
|
|
|
else if (typeof baseValue == 'object')
|
|
|
|
allOpts[key] = Object.assign({}, baseValue, cmdValue);
|
|
|
|
else
|
|
|
|
allOpts[key] = cmdValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const commandOpts = this.getopts(allOpts);
|
2022-06-09 09:42:03 +00:00
|
|
|
if (opts.debug)
|
|
|
|
console.log('Command options:'.magenta, commandOpts);
|
2021-10-25 13:38:07 +00:00
|
|
|
Object.assign(opts, commandOpts);
|
2021-10-23 13:20:35 +00:00
|
|
|
|
2022-12-29 09:15:02 +00:00
|
|
|
const operandToOpt = Command.usage.operand;
|
2021-10-23 13:20:35 +00:00
|
|
|
if (opts._.length >= 2 && operandToOpt)
|
|
|
|
opts[operandToOpt] = opts._[1];
|
2021-10-25 13:38:07 +00:00
|
|
|
|
2022-06-09 09:42:03 +00:00
|
|
|
if (opts.debug)
|
|
|
|
console.log('Final options:'.magenta, opts);
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
if (opts.help) {
|
2022-12-29 09:15:02 +00:00
|
|
|
this.showHelp(Command.opts, Command.usage, commandName);
|
2021-10-25 13:38:07 +00:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check version
|
|
|
|
|
|
|
|
let depVersion;
|
|
|
|
const versionRegex = /^[^~]?([0-9]+)\.([0-9]+).([0-9]+)$/;
|
|
|
|
const wsPackageFile = path.join(opts.workspace, 'package.json');
|
|
|
|
|
|
|
|
if (await fs.pathExists(wsPackageFile)) {
|
|
|
|
const wsPackageJson = require(wsPackageFile);
|
|
|
|
try {
|
|
|
|
depVersion = wsPackageJson
|
2023-03-15 17:13:01 +00:00
|
|
|
.dependencies[packageJson.name]
|
|
|
|
.match(versionRegex);
|
2021-10-25 13:38:07 +00:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depVersion) {
|
|
|
|
const myVersion = packageJson.version.match(versionRegex);
|
2022-01-31 13:07:08 +00:00
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
const isSameVersion =
|
|
|
|
depVersion[1] === myVersion[1] &&
|
|
|
|
depVersion[2] === myVersion[2];
|
|
|
|
if (!isSameVersion)
|
2022-12-21 13:17:50 +00:00
|
|
|
throw new Error(`Myt version differs a lot from package.json, please run 'npm i' first to install the proper version.`);
|
2022-01-31 13:07:08 +00:00
|
|
|
|
|
|
|
const isSameMinor = depVersion[3] === myVersion[3];
|
|
|
|
if (!isSameMinor)
|
2022-12-21 13:17:50 +00:00
|
|
|
console.warn(`Warning! Myt minor version differs from package.json, maybe you shoud run 'npm i' to install the proper version.`.yellow);
|
2021-10-25 13:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load method
|
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
parameter('Workspace:', opts.workspace);
|
|
|
|
parameter('Remote:', opts.remote || 'local');
|
|
|
|
|
2024-01-04 12:02:40 +00:00
|
|
|
await this.init(opts);
|
|
|
|
await this.run(Command, opts);
|
|
|
|
await this.deinit();
|
2020-12-04 16:30:26 +00:00
|
|
|
} catch (err) {
|
2021-10-23 17:14:09 +00:00
|
|
|
if (err.name == 'Error' && !opts.debug) {
|
2020-12-04 16:30:26 +00:00
|
|
|
console.error('Error:'.gray, err.message.red);
|
2022-06-09 09:42:03 +00:00
|
|
|
console.log(`You can get more details about the error by passing the 'debug' option.`.yellow);
|
2021-10-23 17:14:09 +00:00
|
|
|
} else
|
2022-06-09 09:42:03 +00:00
|
|
|
console.log(err.stack.magenta);
|
2022-06-13 07:13:23 +00:00
|
|
|
|
|
|
|
process.exit(1);
|
2020-12-04 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function parameter(parameter, value) {
|
|
|
|
console.log(parameter.gray, (value || 'null').blue);
|
|
|
|
}
|
|
|
|
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
2024-01-04 12:02:40 +00:00
|
|
|
async run(Command, opts) {
|
2024-01-05 09:28:05 +00:00
|
|
|
if (!opts) opts = this.opts;
|
2022-12-29 09:15:02 +00:00
|
|
|
const command = new Command(this, opts);
|
2024-01-04 12:02:40 +00:00
|
|
|
if (this.cliMode)
|
|
|
|
return await command.cli(this, opts);
|
|
|
|
else
|
|
|
|
return await command.run(this, opts);
|
2022-12-21 12:34:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 12:02:40 +00:00
|
|
|
async init(opts) {
|
2020-12-04 16:30:26 +00:00
|
|
|
// Configuration file
|
|
|
|
|
2023-08-11 13:41:03 +00:00
|
|
|
const defaultConfig = require(`${__dirname}/assets/myt.default.yml`);
|
|
|
|
const config = Object.assign({}, defaultConfig);
|
2023-09-22 08:25:06 +00:00
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
const configFile = 'myt.config.yml';
|
2020-12-04 16:30:26 +00:00
|
|
|
const configPath = path.join(opts.workspace, configFile);
|
2023-08-11 13:41:03 +00:00
|
|
|
|
|
|
|
if (await fs.pathExists(configPath)) {
|
|
|
|
const mergeKeys = new Set([
|
|
|
|
'privileges'
|
|
|
|
]);
|
|
|
|
|
|
|
|
const wsConfig = require(configPath);
|
|
|
|
for (const key in wsConfig) {
|
|
|
|
if (!mergeKeys.has(key)) {
|
|
|
|
config[key] = wsConfig[key];
|
|
|
|
} else {
|
|
|
|
config[key] = Object.assign({},
|
|
|
|
config[key],
|
|
|
|
wsConfig[key]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 16:30:26 +00:00
|
|
|
|
|
|
|
Object.assign(opts, config);
|
|
|
|
opts.configFile = configFile;
|
2021-10-25 13:38:07 +00:00
|
|
|
|
2024-01-25 18:28:51 +00:00
|
|
|
const subdir = opts.subdir || '';
|
2022-12-21 13:17:50 +00:00
|
|
|
if (!opts.mytDir)
|
2024-01-25 18:28:51 +00:00
|
|
|
opts.mytDir = path.join(opts.workspace, subdir);
|
2021-10-25 13:38:07 +00:00
|
|
|
|
2024-01-25 18:28:51 +00:00
|
|
|
const routinesBaseRegex = subdir
|
|
|
|
? `${subdir}\/routines`
|
|
|
|
: 'routines';
|
|
|
|
|
|
|
|
Object.assign(opts, {
|
|
|
|
routinesRegex: new RegExp(`^${routinesBaseRegex}\/(.+)\.sql$`),
|
|
|
|
routinesDir: path.join(opts.mytDir, 'routines'),
|
|
|
|
versionsDir: path.join(opts.mytDir, 'versions'),
|
|
|
|
dumpDir: path.join(opts.mytDir, 'dump')
|
|
|
|
});
|
2022-12-21 12:34:17 +00:00
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
// Database configuration
|
2023-09-22 08:25:06 +00:00
|
|
|
|
2022-12-21 12:34:17 +00:00
|
|
|
let iniDir = path.join(__dirname, 'assets');
|
2021-10-25 13:38:07 +00:00
|
|
|
let iniFile = 'db.ini';
|
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
if (opts.remote) {
|
2022-12-21 13:17:50 +00:00
|
|
|
iniDir = `${opts.mytDir}/remotes`;
|
2021-10-25 13:38:07 +00:00
|
|
|
iniFile = `${opts.remote}.ini`;
|
2020-12-04 16:30:26 +00:00
|
|
|
}
|
|
|
|
const iniPath = path.join(iniDir, iniFile);
|
2023-09-22 08:25:06 +00:00
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
if (!await fs.pathExists(iniPath))
|
2021-10-25 13:38:07 +00:00
|
|
|
throw new Error(`Database config file not found: ${iniPath}`);
|
2023-09-22 08:25:06 +00:00
|
|
|
|
2022-06-09 09:42:03 +00:00
|
|
|
let dbConfig;
|
|
|
|
try {
|
2023-03-17 12:13:48 +00:00
|
|
|
const iniData = ini.parse(await fs.readFile(iniPath, 'utf8')).client;
|
|
|
|
const iniConfig = {};
|
|
|
|
for (const key in iniData) {
|
|
|
|
const value = iniData[key];
|
|
|
|
const newKey = key.replace(/-/g, '_');
|
|
|
|
iniConfig[newKey] = value !== undefined ? value : true;
|
|
|
|
}
|
2022-06-09 09:42:03 +00:00
|
|
|
dbConfig = {
|
|
|
|
host: iniConfig.host,
|
|
|
|
port: iniConfig.port,
|
|
|
|
user: iniConfig.user,
|
|
|
|
password: iniConfig.password,
|
2023-03-17 12:13:48 +00:00
|
|
|
multipleStatements: true
|
|
|
|
};
|
|
|
|
if (iniConfig.enable_cleartext_plugin) {
|
|
|
|
dbConfig.authPlugins = {
|
2022-06-09 09:42:03 +00:00
|
|
|
mysql_clear_password() {
|
|
|
|
return () => iniConfig.password + '\0';
|
|
|
|
}
|
2023-03-17 12:13:48 +00:00
|
|
|
};
|
|
|
|
}
|
2022-06-09 09:42:03 +00:00
|
|
|
if (iniConfig.ssl_ca) {
|
|
|
|
dbConfig.ssl = {
|
2022-12-21 13:17:50 +00:00
|
|
|
ca: await fs.readFile(`${opts.mytDir}/${iniConfig.ssl_ca}`),
|
2022-06-09 09:42:03 +00:00
|
|
|
rejectUnauthorized: iniConfig.ssl_verify_server_cert != undefined
|
2020-12-04 16:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 12:13:48 +00:00
|
|
|
if (iniConfig.socket) {
|
|
|
|
dbConfig.socketPath = iniConfig.socket;
|
|
|
|
}
|
2022-06-09 09:42:03 +00:00
|
|
|
} catch(err) {
|
|
|
|
const newErr = Error(`Cannot process the ini file, check that the syntax is correct: ${iniPath}`);
|
|
|
|
newErr.stack += `\nCaused by: ${err.stack}`;
|
|
|
|
throw newErr;
|
2020-12-04 16:30:26 +00:00
|
|
|
}
|
2022-06-09 09:42:03 +00:00
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
Object.assign(opts, {
|
|
|
|
iniFile,
|
|
|
|
dbConfig
|
|
|
|
});
|
|
|
|
this.opts = opts;
|
|
|
|
}
|
|
|
|
|
2024-01-04 12:02:40 +00:00
|
|
|
async deinit() {
|
2021-10-23 13:20:35 +00:00
|
|
|
if (this.conn)
|
|
|
|
await this.conn.end();
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
getopts(opts) {
|
|
|
|
const argv = process.argv.slice(2);
|
|
|
|
const values = getopts(argv, opts);
|
|
|
|
const cleanValues = {};
|
|
|
|
for (const opt in values)
|
|
|
|
if (opt.length > 1 || opt == '_')
|
|
|
|
cleanValues[opt] = values[opt];
|
|
|
|
return cleanValues;
|
|
|
|
}
|
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
async dbConnect() {
|
2021-10-25 13:38:07 +00:00
|
|
|
const {opts} = this;
|
|
|
|
|
|
|
|
if (!this.conn) {
|
|
|
|
const conn = this.conn = await this.createConnection();
|
|
|
|
|
|
|
|
const [[schema]] = await conn.query(
|
|
|
|
`SHOW DATABASES LIKE ?`, [opts.versionSchema]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!schema)
|
|
|
|
await conn.query(`CREATE DATABASE ??`, [opts.versionSchema]);
|
2022-04-04 18:53:18 +00:00
|
|
|
await conn.query(`USE ??`, [opts.versionSchema]);
|
2021-10-25 13:38:07 +00:00
|
|
|
|
|
|
|
const [[res]] = await conn.query(
|
|
|
|
`SELECT COUNT(*) > 0 tableExists
|
|
|
|
FROM information_schema.tables
|
|
|
|
WHERE TABLE_SCHEMA = ?
|
|
|
|
AND TABLE_NAME = 'version'`,
|
|
|
|
[opts.versionSchema]
|
|
|
|
);
|
2023-09-22 08:25:06 +00:00
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
if (!res.tableExists) {
|
2022-06-09 09:42:03 +00:00
|
|
|
const structure = await fs.readFile(
|
2022-12-21 12:34:17 +00:00
|
|
|
`${__dirname}/assets/structure.sql`, 'utf8');
|
2021-10-25 13:38:07 +00:00
|
|
|
await conn.query(structure);
|
|
|
|
}
|
2024-01-14 14:49:49 +00:00
|
|
|
|
|
|
|
const [[realm]] = await conn.query(
|
|
|
|
`SELECT realm FROM versionConfig`
|
|
|
|
);
|
|
|
|
this.realm = realm;
|
2021-10-25 13:38:07 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
return this.conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
async createConnection() {
|
|
|
|
return await mysql.createConnection(this.opts.dbConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchDbVersion() {
|
|
|
|
const [[version]] = await this.conn.query(
|
|
|
|
`SELECT number, gitCommit
|
|
|
|
FROM version WHERE code = ?`,
|
2021-10-25 13:38:07 +00:00
|
|
|
[this.opts.code]
|
2020-12-04 16:30:26 +00:00
|
|
|
);
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2022-04-30 00:06:56 +00:00
|
|
|
parseVersionDir(versionDir) {
|
|
|
|
const match = versionDir.match(/^([0-9]+)-([a-zA-Z0-9]+)?$/);
|
|
|
|
if (!match) return null;
|
|
|
|
return {
|
|
|
|
number: match[1],
|
|
|
|
name: match[2]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-14 14:49:49 +00:00
|
|
|
async loadVersion(versionDir) {
|
|
|
|
const {opts} = this;
|
|
|
|
|
|
|
|
const info = this.parseVersionDir(versionDir);
|
|
|
|
if (!info) return null;
|
|
|
|
|
|
|
|
const versionsDir = opts.versionsDir;
|
|
|
|
const scriptsDir = `${versionsDir}/${versionDir}`;
|
|
|
|
const scriptList = await fs.readdir(scriptsDir);
|
|
|
|
|
|
|
|
const [res] = await this.conn.query(
|
|
|
|
`SELECT file, errorNumber IS NOT NULL hasError
|
|
|
|
FROM versionLog
|
|
|
|
WHERE code = ?
|
|
|
|
AND number = ?`,
|
|
|
|
[opts.code, info.number]
|
|
|
|
);
|
|
|
|
const versionLog = new Map();
|
|
|
|
res.map(x => versionLog.set(x.file, x));
|
|
|
|
|
|
|
|
let applyVersion = false;
|
|
|
|
const scripts = [];
|
|
|
|
|
|
|
|
for (const file of scriptList) {
|
|
|
|
const match = file.match(scriptRegex);
|
|
|
|
if (match) {
|
|
|
|
const scriptRealm = match[1];
|
|
|
|
const isUndo = !!match[2];
|
|
|
|
|
|
|
|
if ((scriptRealm && scriptRealm !== this.realm) || isUndo)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const logInfo = versionLog.get(file);
|
|
|
|
const apply = !logInfo || logInfo.hasError;
|
|
|
|
if (apply) applyVersion = true;
|
|
|
|
|
|
|
|
scripts.push({
|
|
|
|
file,
|
|
|
|
matchRegex: !!match,
|
|
|
|
apply
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
number: info.number,
|
|
|
|
name: info.name,
|
|
|
|
scripts,
|
|
|
|
apply: applyVersion
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:11:03 +00:00
|
|
|
async openRepo() {
|
|
|
|
const {opts} = this;
|
2020-12-18 23:53:21 +00:00
|
|
|
|
2021-10-22 14:11:03 +00:00
|
|
|
if (!await fs.pathExists(`${opts.workspace}/.git`))
|
|
|
|
throw new Error ('Git not initialized');
|
|
|
|
|
|
|
|
return await nodegit.Repository.open(opts.workspace);
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
showHelp(opts, usage, command) {
|
2022-12-21 13:17:50 +00:00
|
|
|
const prefix = `${'Usage:'.gray} [npx] myt`;
|
2021-10-25 13:38:07 +00:00
|
|
|
|
|
|
|
if (command) {
|
|
|
|
let log = [prefix, command.blue];
|
|
|
|
if (usage.operand) log.push(`[<${usage.operand}>]`);
|
|
|
|
if (opts) log.push('[<options>]');
|
|
|
|
console.log(log.join(' '))
|
|
|
|
} else
|
|
|
|
console.log(`${prefix} [<options>] ${'<command>'.blue} [<args>]`);
|
|
|
|
|
|
|
|
if (usage.description)
|
|
|
|
console.log(`${'Description:'.gray} ${usage.description}`);
|
|
|
|
|
2022-04-04 19:14:04 +00:00
|
|
|
if (opts && opts.alias) {
|
|
|
|
const alias = opts.alias;
|
|
|
|
const boolean = opts.boolean || [];
|
2021-10-25 13:38:07 +00:00
|
|
|
|
2022-04-04 19:14:04 +00:00
|
|
|
console.log('Options:'.gray);
|
|
|
|
for (const opt in alias) {
|
|
|
|
const paramDescription = usage.params[opt] || '';
|
|
|
|
let longOpt = opt;
|
|
|
|
if (boolean.indexOf(longOpt) === -1)
|
|
|
|
longOpt += ` <string>`;
|
|
|
|
longOpt = camelToSnake(longOpt).padEnd(22, ' ')
|
|
|
|
console.log(` -${alias[opt]}, --${longOpt} ${paramDescription}`);
|
|
|
|
}
|
2021-10-25 13:38:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
module.exports = Myt;
|
2020-12-04 16:30:26 +00:00
|
|
|
|
|
|
|
if (require.main === module)
|
2024-01-04 12:02:40 +00:00
|
|
|
new Myt().cli();
|