-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from kepler-inc/admin-cli-workflow
Admin cli workflow
- Loading branch information
Showing
5 changed files
with
339 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}; | ||
`; | ||
} |
Oops, something went wrong.