Get started on CLI

This commit is contained in:
rijkvanzanten
2020-07-10 18:10:56 -04:00
parent decdad7caf
commit 531992f9b3
10 changed files with 897 additions and 600 deletions

13
src/cli/create/drivers.ts Normal file
View File

@@ -0,0 +1,13 @@
export const drivers = {
sqlite3: 'SQLite',
mysql: 'MySQL (/ MariaDB / Aurora)',
pg: 'PostgreSQL (/ Amazon Redshift)',
oracledb: 'Oracle Database',
mssql: 'Microsoft SQL Server',
};
export function getDriverForClient(client: string) {
for (const [key, value] of Object.entries(drivers)) {
if (value === client) return key;
}
}

66
src/cli/create/index.ts Normal file
View File

@@ -0,0 +1,66 @@
import fse from 'fs-extra';
import chalk from 'chalk';
import inquirer from 'inquirer';
import { resolve } from 'path';
import { databaseQuestions } from './questions';
import { drivers, getDriverForClient } from './drivers';
export default async function create(directory: string, options: Record<string, any>) {
const path = resolve(directory);
checkRequirements();
if (await fse.pathExists(path)) {
const stat = await fse.stat(path);
if (stat.isDirectory() === false) {
console.error(
`Destination '${chalk.red(directory)}' already exists and is not a directory.`
);
process.exit(1);
}
const files = await fse.readdir(path);
if (files.length > 0) {
console.error(
`Destination '${chalk.red(
directory
)}' already exists and is not an empty directory.`
);
process.exit(1);
}
}
let { client } = await inquirer.prompt([
{
type: 'list',
name: 'client',
message: 'Choose your database client',
choices: Object.values(drivers),
},
]);
client = getDriverForClient(client);
const responses = await inquirer.prompt(
databaseQuestions[client].map((question) => question({ client }))
);
/** @todo
* - See if you can connect to DB
* - Install Directus system stuff into DB
* - Start the Node API
*/
}
function checkRequirements() {
const nodeVersion = process.versions.node;
const major = +nodeVersion.split('.')[0];
if (major < 12) {
console.error(`You are running Node ${nodeVersion}.`);
console.error('Directus requires Node 12 and up.');
console.error('Please update your Node version and try again.');
process.exit(1);
}
}

View File

@@ -0,0 +1,50 @@
const filename = () => ({
type: 'input',
name: 'filename',
message: 'Filename:',
default: './data.db',
});
const host = () => ({
type: 'input',
name: 'host',
message: 'Host:',
default: '127.0.0.1',
});
const port = ({ client }) => ({
type: 'input',
name: 'port',
message: 'Port:',
default() {
const ports = {
pg: 5432,
mysql: 3306,
oracledb: 1521,
mssql: 1433,
};
return ports[client];
},
});
const username = () => ({
type: 'input',
name: 'username',
message: 'Username:',
});
const password = () => ({
type: 'password',
name: 'password',
message: 'Password:',
mask: '*',
});
export const databaseQuestions = {
sqlite3: [filename],
mysql: [host, port, username, password],
pg: [host, port, username, password],
oracledb: [host, port, username, password],
mssql: [host, port, username, password],
};

18
src/cli/index.ts Normal file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env node
import program from 'commander';
const pkg = require('../../package.json');
import start from './start';
import create from './create';
program.version(pkg.version, '-v, --version');
program.name('directus').usage('[command] [options]');
program.command('create <directory>').description('Create a new Directus Project').action(create);
program.command('start').description('Start the Directus API').action(start);
program.parseAsync(process.argv);

14
src/cli/start.ts Normal file
View File

@@ -0,0 +1,14 @@
import app from '../app';
import logger from '../logger';
import getPort from 'get-port';
import clear from 'clear';
export default async function start() {
clear();
const port = process.env.PORT || (await getPort({ port: 3000 }));
app.listen(port, () => {
logger.info(`Server started at port ${port}`);
});
}