@basictech/cli
Version:
Basic CLI for creating & managing your projects
111 lines • 4.74 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from 'react';
import { render, Box, Text } from 'ink';
import { InitForm } from '../components/InitForm.js';
import { Spinner } from '../components/Spinner.js';
import { AuthService } from '../lib/auth.js';
import { checkForExistingConfig, readExistingConfig } from '../lib/config-templates.js';
import { isOnline } from '../lib/platform.js';
import { MESSAGES } from '../lib/constants.js';
function InitApp({ options }) {
const [state, setState] = React.useState({
loading: true,
error: null,
success: false
});
const [initialData, setInitialData] = React.useState(null);
React.useEffect(() => {
async function initialize() {
try {
// Check if online
if (!(await isOnline())) {
setState({ loading: false, error: MESSAGES.OFFLINE, success: false });
return;
}
// Check authentication
const authService = AuthService.getInstance();
const token = await authService.getToken();
if (!token) {
setState({ loading: false, error: MESSAGES.LOGGED_OUT, success: false });
return;
}
// Check for existing config file
const existingConfigPath = await checkForExistingConfig();
if (existingConfigPath) {
const existingConfig = await readExistingConfig();
if (existingConfig?.projectId) {
setState({
loading: false,
error: `A basic.config already exists in this directory\nProject ID: ${existingConfig.projectId}\n\nTo reinitialize, please remove the existing config file first.`,
success: false
});
return;
}
}
// Parse CLI options into initial data
const parsedData = parseCliOptions(options);
setInitialData(parsedData);
setState({ loading: false, error: null, success: false });
}
catch (error) {
setState({
loading: false,
error: error instanceof Error ? error.message : 'Failed to initialize',
success: false
});
}
}
initialize();
}, [options]);
const handleSuccess = (result) => {
setState({ loading: false, error: null, success: true, result });
// Exit after showing success message
setTimeout(() => {
process.exit(0);
}, 2000);
};
const handleCancel = () => {
process.exit(0);
};
if (state.loading) {
return _jsx(Spinner, { text: "Initializing..." });
}
if (state.error) {
return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { color: "red", children: ["Error: ", state.error] }), _jsx(Text, { color: "gray", children: "Please resolve the issue and try again." })] }));
}
if (state.success && state.result) {
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: "green", children: "\u2705 Project setup complete!" }), _jsx(Text, {}), _jsxs(Text, { children: ["Project: ", state.result.projectName] }), _jsxs(Text, { children: ["Project ID: ", state.result.projectId] }), state.result.configPath && (_jsxs(Text, { children: ["Config file: ", state.result.configPath] })), _jsx(Text, {}), _jsx(Text, { color: "gray", children: "Visit https://docs.basic.tech for next steps." })] }));
}
return (_jsx(InitForm, { onSuccess: handleSuccess, onCancel: handleCancel, initialData: initialData || undefined }));
}
function parseCliOptions(options) {
const result = {};
// Determine source
if (options.new) {
result.source = 'new';
}
else if (options.existing) {
result.source = 'existing';
}
// Project name
if (options.name) {
result.projectName = options.name;
}
// Project ID (for existing projects)
if (options.project) {
result.projectId = options.project;
result.source = 'existing';
}
// Config template (priority order: specific frameworks > general > none)
if (options.ts) {
result.configTemplate = 'typescript';
}
else if (options.js) {
result.configTemplate = 'javascript';
}
return result;
}
export async function InitCommand(args = {}) {
render(_jsx(InitApp, { options: args }));
}
//# sourceMappingURL=init.js.map