mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Finish create command in cli
This commit is contained in:
@@ -35,7 +35,7 @@ STORAGE_DIGITALOCEAN_REGION="ams3"
|
||||
####################################################################################################
|
||||
# Security
|
||||
|
||||
UUID="xxxxxxx-xxxxxx-xxxxxxxx-xxxxxxxxxx"
|
||||
KEY="xxxxxxx-xxxxxx-xxxxxxxx-xxxxxxxxxx"
|
||||
SECRET="abcdef"
|
||||
ACCESS_TOKEN_TTL="15m"
|
||||
REFRESH_TOKEN_TTL="7d"
|
||||
|
||||
21
package-lock.json
generated
21
package-lock.json
generated
@@ -1612,7 +1612,6 @@
|
||||
"version": "7.0.3",
|
||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
|
||||
"integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"path-key": "^3.1.0",
|
||||
"shebang-command": "^2.0.0",
|
||||
@@ -2144,7 +2143,6 @@
|
||||
"version": "4.0.3",
|
||||
"resolved": "https://registry.npmjs.org/execa/-/execa-4.0.3.tgz",
|
||||
"integrity": "sha512-WFDXGHckXPWZX19t1kCsXzOpqX9LWYNqn4C+HqZlk/V0imTkzJZqf87ZBhvpHaftERYknpk0fjSylnXVlVgI0A==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"cross-spawn": "^7.0.0",
|
||||
"get-stream": "^5.0.0",
|
||||
@@ -2962,7 +2960,6 @@
|
||||
"version": "5.1.0",
|
||||
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.1.0.tgz",
|
||||
"integrity": "sha512-EXr1FOzrzTfGeL0gQdeFEvOMm2mzMOglyiOXSTpPC+iAjAKftbr3jpCMWynogwYnM+eSj9sHGc6wjIcDvYiygw==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"pump": "^3.0.0"
|
||||
}
|
||||
@@ -3351,8 +3348,7 @@
|
||||
"human-signals": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/human-signals/-/human-signals-1.1.1.tgz",
|
||||
"integrity": "sha512-SEQu7vl8KjNL2eoGBLF3+wAjpsNfA9XMlXAYj/3EdaNfAlxKthD1xjEQfGOUhllCGGJVNY34bRr6lPINhNjyZw==",
|
||||
"dev": true
|
||||
"integrity": "sha512-SEQu7vl8KjNL2eoGBLF3+wAjpsNfA9XMlXAYj/3EdaNfAlxKthD1xjEQfGOUhllCGGJVNY34bRr6lPINhNjyZw=="
|
||||
},
|
||||
"husky": {
|
||||
"version": "4.2.5",
|
||||
@@ -4385,8 +4381,7 @@
|
||||
"merge-stream": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz",
|
||||
"integrity": "sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==",
|
||||
"dev": true
|
||||
"integrity": "sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w=="
|
||||
},
|
||||
"methods": {
|
||||
"version": "1.1.2",
|
||||
@@ -4845,7 +4840,6 @@
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-4.0.1.tgz",
|
||||
"integrity": "sha512-S48WzZW777zhNIrn7gxOlISNAqi9ZC/uQFnRdbeIHhZhCA6UqpkOT8T1G7BvfdgP4Er8gF4sUbaS0i7QvIfCWw==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"path-key": "^3.0.0"
|
||||
}
|
||||
@@ -5134,8 +5128,7 @@
|
||||
"path-key": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
|
||||
"integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==",
|
||||
"dev": true
|
||||
"integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q=="
|
||||
},
|
||||
"path-parse": {
|
||||
"version": "1.0.6",
|
||||
@@ -6122,7 +6115,6 @@
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
|
||||
"integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"shebang-regex": "^3.0.0"
|
||||
}
|
||||
@@ -6130,8 +6122,7 @@
|
||||
"shebang-regex": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
|
||||
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
|
||||
"dev": true
|
||||
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A=="
|
||||
},
|
||||
"shellwords": {
|
||||
"version": "0.1.1",
|
||||
@@ -6587,8 +6578,7 @@
|
||||
"strip-final-newline": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/strip-final-newline/-/strip-final-newline-2.0.0.tgz",
|
||||
"integrity": "sha512-BrpvfNAE3dcvq7ll3xVumzjKjZQ5tI1sEUIKr3Uoks0XUl45St3FlatVqef9prk4jRDzhW6WZg+3bk93y6pLjA==",
|
||||
"dev": true
|
||||
"integrity": "sha512-BrpvfNAE3dcvq7ll3xVumzjKjZQ5tI1sEUIKr3Uoks0XUl45St3FlatVqef9prk4jRDzhW6WZg+3bk93y6pLjA=="
|
||||
},
|
||||
"strip-indent": {
|
||||
"version": "1.0.1",
|
||||
@@ -7284,7 +7274,6 @@
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
|
||||
"integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"isexe": "^2.0.0"
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
"commander": "^5.1.0",
|
||||
"cookie-parser": "^1.4.5",
|
||||
"dotenv": "^8.2.0",
|
||||
"execa": "^4.0.3",
|
||||
"exif-reader": "^1.0.3",
|
||||
"express": "^4.17.1",
|
||||
"express-async-handler": "^1.1.4",
|
||||
|
||||
@@ -3,21 +3,19 @@ import chalk from 'chalk';
|
||||
import inquirer from 'inquirer';
|
||||
import { resolve } from 'path';
|
||||
import { databaseQuestions } from './questions';
|
||||
import { drivers, getDriverForClient } from './drivers';
|
||||
import childProcess from 'child_process';
|
||||
import { promisify } from 'util';
|
||||
import createEnv from './env';
|
||||
import { drivers, getDriverForClient } from '../../utils/drivers';
|
||||
import createEnv from '../../utils/create-env';
|
||||
import execa from 'execa';
|
||||
import path from 'path';
|
||||
|
||||
const exec = promisify(childProcess.exec);
|
||||
|
||||
import installDB, { Credentials } from './install-db';
|
||||
import installDB, { Credentials } from '../../utils/install-db';
|
||||
|
||||
export default async function create(directory: string, options: Record<string, any>) {
|
||||
const path = resolve(directory);
|
||||
const rootPath = resolve(directory);
|
||||
checkRequirements();
|
||||
|
||||
if (await fse.pathExists(path)) {
|
||||
const stat = await fse.stat(path);
|
||||
if (await fse.pathExists(rootPath)) {
|
||||
const stat = await fse.stat(rootPath);
|
||||
|
||||
if (stat.isDirectory() === false) {
|
||||
console.error(
|
||||
@@ -26,7 +24,7 @@ export default async function create(directory: string, options: Record<string,
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const files = await fse.readdir(path);
|
||||
const files = await fse.readdir(rootPath);
|
||||
|
||||
if (files.length > 0) {
|
||||
console.error(
|
||||
@@ -38,9 +36,19 @@ export default async function create(directory: string, options: Record<string,
|
||||
}
|
||||
}
|
||||
|
||||
await fse.mkdir(path);
|
||||
await fse.mkdir(rootPath);
|
||||
await fse.mkdir(path.join(rootPath, 'uploads'));
|
||||
await fse.mkdir(path.join(rootPath, 'extensions'));
|
||||
|
||||
// await exec(`cd ${path} && npm init -y && npm install directus@preview`);
|
||||
await execa('npm', ['init', '-y'], {
|
||||
cwd: rootPath,
|
||||
stdin: 'ignore',
|
||||
});
|
||||
|
||||
await execa('npm', ['install', 'directus@preview', '--production', '--no-optional'], {
|
||||
cwd: rootPath,
|
||||
stdin: 'ignore',
|
||||
});
|
||||
|
||||
let { client } = await inquirer.prompt([
|
||||
{
|
||||
@@ -57,15 +65,22 @@ export default async function create(directory: string, options: Record<string,
|
||||
(databaseQuestions[dbClient] as any[]).map((question: Function) => question({ client }))
|
||||
);
|
||||
|
||||
// try {
|
||||
// await installDB(client, credentials);
|
||||
// } catch (error) {
|
||||
// console.log(`${chalk.red('Database Error')}: Couln't install the database:`);
|
||||
// console.log(error.message);
|
||||
// }
|
||||
try {
|
||||
await installDB(client, credentials);
|
||||
} catch (error) {
|
||||
console.log(`${chalk.red('Database Error')}: Couln't install the database:`);
|
||||
console.log(error.message);
|
||||
}
|
||||
|
||||
await createEnv(client, credentials);
|
||||
// await exec(`cd && directus start`);
|
||||
await createEnv(client, credentials, rootPath);
|
||||
|
||||
console.log(`
|
||||
Your project has been created at ${chalk.green(rootPath)}.
|
||||
|
||||
Start Directus by running:
|
||||
${chalk.blue('cd')} ${rootPath}
|
||||
${chalk.blue('npx directus')} start
|
||||
`);
|
||||
}
|
||||
|
||||
function checkRequirements() {
|
||||
@@ -12,12 +12,12 @@ const host = () => ({
|
||||
default: '127.0.0.1',
|
||||
});
|
||||
|
||||
const port = ({ client }) => ({
|
||||
const port = ({ client }: { client: string }) => ({
|
||||
type: 'input',
|
||||
name: 'port',
|
||||
message: 'Port:',
|
||||
default() {
|
||||
const ports = {
|
||||
const ports: Record<string, number> = {
|
||||
pg: 5432,
|
||||
mysql: 3306,
|
||||
oracledb: 1521,
|
||||
@@ -1,7 +1,10 @@
|
||||
import app from '../app';
|
||||
import logger from '../logger';
|
||||
import app from '../../app';
|
||||
import logger from '../../logger';
|
||||
import getPort from 'get-port';
|
||||
import clear from 'clear';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
export default async function start() {
|
||||
clear();
|
||||
@@ -1,52 +0,0 @@
|
||||
import { drivers } from './drivers';
|
||||
import { Credentials } from './install-db';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
const defaults = {
|
||||
general: {
|
||||
port: 3000,
|
||||
publicUrl: '/',
|
||||
},
|
||||
storage: {
|
||||
storageLocations: 'local',
|
||||
storageLocalPublicUrl: '/uploads',
|
||||
storageLocalDriver: 'local',
|
||||
storageLocalRoot: './uploads',
|
||||
},
|
||||
security: {
|
||||
uuid: uuidv4(),
|
||||
secret: nanoid(32),
|
||||
accessTokenTtl: '15m',
|
||||
refreshTokenTtl: '7d',
|
||||
refreshTokenCookieSecure: false,
|
||||
refreshTokenCookieSameSite: 'lax',
|
||||
},
|
||||
oauth: {
|
||||
oauthProviders: '',
|
||||
},
|
||||
extensions: {
|
||||
extensionsPath: './extensions',
|
||||
},
|
||||
email: {
|
||||
emailFrom: 'no-reply@directus.io',
|
||||
emailTransport: 'sendmail',
|
||||
emailSendmailNewLine: 'unix',
|
||||
emailSendmailPath: '/usr/sbin/sendmail',
|
||||
},
|
||||
};
|
||||
|
||||
export default function createEnv(client: keyof typeof drivers, credentials: Credentials) {
|
||||
const config: Record<string, any> = {
|
||||
...defaults,
|
||||
database: {
|
||||
dbClient: client,
|
||||
},
|
||||
};
|
||||
|
||||
for (const [key, value] of Object.entries(credentials)) {
|
||||
config.database[`db_${key}`] = value;
|
||||
}
|
||||
|
||||
console.log(config);
|
||||
}
|
||||
@@ -4,8 +4,8 @@ import program from 'commander';
|
||||
|
||||
const pkg = require('../../package.json');
|
||||
|
||||
import start from './start';
|
||||
import create from './create';
|
||||
import start from './commands/start';
|
||||
import create from './commands/create';
|
||||
|
||||
program.version(pkg.version, '-v, --version');
|
||||
|
||||
|
||||
79
src/cli/utils/create-env/index.ts
Normal file
79
src/cli/utils/create-env/index.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { drivers } from '../drivers';
|
||||
import { Credentials } from '../install-db';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { Liquid } from 'liquidjs';
|
||||
import fs from 'fs';
|
||||
import { promisify } from 'util';
|
||||
import path from 'path';
|
||||
|
||||
const readFile = promisify(fs.readFile);
|
||||
const writeFile = promisify(fs.writeFile);
|
||||
|
||||
const liquidEngine = new Liquid({
|
||||
extname: '.liquid',
|
||||
});
|
||||
|
||||
const defaults = {
|
||||
general: {
|
||||
PORT: 3000,
|
||||
PUBLIC_URL: '/',
|
||||
},
|
||||
storage: {
|
||||
STORAGE_LOCATIONS: 'local',
|
||||
STORAGE_LOCAL_PUBLIC_URL: '/uploads',
|
||||
STORAGE_LOCAL_DRIVER: 'local',
|
||||
STORAGE_LOCAL_ROOT: './uploads',
|
||||
},
|
||||
security: {
|
||||
UUID: uuidv4(),
|
||||
SECRET: nanoid(32),
|
||||
ACCESS_TOKEN_TTL: '15m',
|
||||
REFRESH_TOKEN_TTL: '7d',
|
||||
REFRESH_TOKEN_COOKIE_SECURE: false,
|
||||
REFRESH_TOKEN_COOKIE_SAME_SITE: 'lax',
|
||||
},
|
||||
oauth: {
|
||||
OAUTH_PROVIDERS: '',
|
||||
},
|
||||
extensions: {
|
||||
EXTENSIONS_PATH: './extensions',
|
||||
},
|
||||
email: {
|
||||
EMAIL_FROM: 'no-reply@directus.io',
|
||||
EMAIL_TRANSPORT: 'sendmail',
|
||||
EMAIL_SENDMAIL_NEW_LINE: 'unix',
|
||||
EMAIL_SENDMAIL_PATH: '/usr/sbin/sendmail',
|
||||
},
|
||||
};
|
||||
|
||||
export default async function createEnv(
|
||||
client: keyof typeof drivers,
|
||||
credentials: Credentials,
|
||||
directory: string
|
||||
) {
|
||||
const config: Record<string, any> = {
|
||||
...defaults,
|
||||
database: {
|
||||
DB_CLIENT: client,
|
||||
},
|
||||
};
|
||||
|
||||
for (const [key, value] of Object.entries(credentials)) {
|
||||
config.database[`DB_${key.toUpperCase()}`] = value;
|
||||
}
|
||||
|
||||
const configAsStrings: any = {};
|
||||
|
||||
for (const [key, value] of Object.entries(config)) {
|
||||
configAsStrings[key] = '';
|
||||
|
||||
for (const [envKey, envValue] of Object.entries(value)) {
|
||||
configAsStrings[key] += `${envKey}="${envValue}"\n`;
|
||||
}
|
||||
}
|
||||
|
||||
const templateString = await readFile(path.join(__dirname, 'env-stub.liquid'), 'utf8');
|
||||
const text = await liquidEngine.parseAndRender(templateString, configAsStrings);
|
||||
await writeFile(path.join(directory, '.env'), text);
|
||||
}
|
||||
Reference in New Issue
Block a user