Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin cli workflow #47

Merged
merged 10 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/admin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "admin",
"version": "0.1.0",
"private": true,
"name": "@arkw/admin",
"version": "0.1.1",
"scripts": {
"dev": "next dev",
"build": "next build",
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bellefond",
"version": "0.0.2",
"name": "arkw",
"version": "0.0.3",
"license": "MIT",
"bin": "dist/cli.js",
"type": "module",
Expand All @@ -16,7 +16,10 @@
"dist"
],
"dependencies": {
"@arkw/admin": "^0.1.1",
"@types/fs-extra": "^11.0.4",
"core": "workspace:*",
"execa": "^9.3.1",
"fs-extra": "^11.2.0",
"ink": "^4.1.0",
"pastel": "^2.0.0",
Expand Down
65 changes: 62 additions & 3 deletions packages/cli/source/commands/dev.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
import React from 'react';
import {Text} from 'ink';

export default function Login() {
return <Text>This should open up the Next.js dev server on port 3456</Text>;
}
import {execa, ExecaError} from 'execa';

async function startNextDevServer() {
function log(message: any, isError = false) {
const timestamp = new Date().toISOString();
const formattedMessage = `[${timestamp}] ${message}\n`;

if (isError) {
process.stderr.write(formattedMessage);
} else {
process.stdout.write(formattedMessage);
}
}

log('Starting Next.js dev server...');

try {
// TODO: get output re-colorized
// TODO: fix cwd so it works from project directory, not just from the cli directory
const nextServer = execa(
`PROJECT_DIRECTORY=${process.cwd()} npm run dev -- -p 3454`,
{
cwd: './node_modules/@arkw/admin',
all: true,
buffer: false,
env: process.env,
shell: true,
},
);

nextServer.all.on('data', (data: any) => {
const output = data.toString().trim();
log(output);

if (output.includes('compiled successfully')) {
log('Next.js dev server is ready!');
}
});

process.on('SIGINT', async () => {
log('Stopping Next.js dev server...');
await nextServer.kill();
process.exit();
});

await nextServer;
} catch (error: any) {
if (error instanceof ExecaError) {
console.log(error);
}
log(`Error: ${error.message}`, true);
if (error.stderr) {
log(`stderr: ${error.stderr}`, true);
}
} finally {
}
}

export default function Dev() {
startNextDevServer().catch(console.error);
return <Text>This should open up the Next.js dev server on port 3456</Text>;
}
132 changes: 122 additions & 10 deletions packages/cli/source/commands/init.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,136 @@
import fs from 'fs-extra'
import path from 'node:path'
import process from 'process';
import fs from 'fs-extra';
import path from 'node:path';
import process from 'process';

import React from 'react';
import {Text} from 'ink';
import {execa, ExecaError} from 'execa';

async function init (f: any) {
function init(configFilePath: string) {
try {
await fs.outputJson(f, {config: ''})
// Check to make sure a package.json file exists..
const packageJsonPath = path.join(process.cwd(), 'package.json');
if (!fs.pathExistsSync(packageJsonPath)) {
console.log('No package.json file found in the current directory');
return false;
}

// const data = await fs.readJson(f)
// Check to make sure `@arkw/core` is installed.
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
if (!packageJson.dependencies || !packageJson.dependencies['core']) {
console.log(
'Please install @arkw/core before running this command (npm install @arkw/core)',
);
return false;
}

console.log(`writing bfconfig.json`) // => JP
// Check if a config file already exists.
// const configFilePath = path.join(process.cwd(), 'arkw.config.ts');
if (fs.pathExistsSync(configFilePath)) {
console.log(
'Arkwright configuration file (arkw.config.ts) already exists',
);
return false;
}

const config = generateBoilerplateConfig();
fs.outputFileSync(configFilePath, config);

return config;
} catch (err) {
console.error(err)
console.error(err);
return false;
}
}

async function migrate(createOnly = false) {
function log(message: any, isError = false) {
const timestamp = new Date().toISOString();
const formattedMessage = `[${timestamp}] ${message}\n`;

if (isError) {
process.stderr.write(formattedMessage);
} else {
process.stdout.write(formattedMessage);
}
}
console.log('Migrating database...');
try {
// TODO: get this working in project dir rather than cli dir
// TODO: prompt user for db URL or create sqllite db

const INJECT_DB_URL = `FUTURE_DATABASE_URL="postgresql://postgres:postgres@127.0.0.1:54322/arkwright?schema=public"`;

const PRISMA_BIN = `./node_modules/core/node_modules/prisma/node_modules/.bin`;

const PRISMA_SCHEMA = `node_modules/core/src/prisma/schema.prisma`;

const CREATE_ONLY = createOnly ? `--create-only` : ``;

const migrateCommand = execa(
`${INJECT_DB_URL} ${PRISMA_BIN}/prisma migrate dev ${CREATE_ONLY} --schema=${PRISMA_SCHEMA} --name initial_migration`,
{
env: process.env,
shell: true,
all: true,
},
);

migrateCommand.all.on('data', (data: any) => {
const output = data.toString().trim();
log(output);
});

await migrateCommand;
} catch (error: any) {
if (error instanceof ExecaError) {
console.log(error);
}
log(`Error: ${error.message}`, true);
if (error.stderr) {
log(`stderr: ${error.stderr}`, true);
}
} finally {
}
}

export default function Init() {
init(path.join(process.cwd(), 'bfconfig.json'))
return <Text>This should write to bfconfig.json</Text>;
const config = init(path.join(process.cwd(), 'arkw.config.ts'));

// TODO: merge output from migrate better with config file text results
migrate();

if (config) {
return <Text>Arkwright configuration file written to arkw.config.ts</Text>;
}

return <Text>An Error occurred</Text>;
}

// TODO: Move this to core
function generateBoilerplateConfig() {
return `
import { Config, createFramework } from 'core';
import { MailchimpIntegration } from 'future-mailchimp'

export const config: Config = {
name: 'kepler',
//logConfig: {}, // TODO: Add this
systemActions: [],
systemEvents: [],
plugins: [
new MailchimpIntegration({
config: {
CLIENT_ID: process.env.MAILCHIMP_CLIENT_ID!,
CLIENT_SECRET: process.env.MAILCHIMP_CLIENT_SECRET!,
REDIRECT_URI: ''
}
}),
],
db: {
provider: 'postgres',
uri: 'postgresql://postgres:postgres@127.0.0.1:54322/postgres?schema=future',
},
};
`;
}
Loading