Skip to content

Instantly share code, notes, and snippets.

@sullemanhossam
Created July 30, 2024 17:09
Show Gist options
  • Save sullemanhossam/8927bbef2652f5701fe5bc6f978b4106 to your computer and use it in GitHub Desktop.
Save sullemanhossam/8927bbef2652f5701fe5bc6f978b4106 to your computer and use it in GitHub Desktop.
import React, {useState, useEffect} from 'react';
import {render} from 'ink';
import fs from 'node:fs';
import path from 'node:path';
import stripAnsi from 'strip-ansi';
import childProcess from 'node:child_process';
import {MainThread} from './progress'; // Adjust this import based on your actual file structure
import {CreatePackageJson} from './create-pkg-json';
/**================================================================================================
*? Execution block
the main thread should only concerned with the steps
first it will
* checks if a package json already exists
- if a package json exists then it will prompt the user to the creation of a new package json
* of a subprocess. In a real-world scenario, you'd likely want to pass the subprocess
* command and any necessary arguments to this component.
*
* The output and error states are managed in this component, and the MainThread component
* is responsible for rendering them.
*
*================================================================================================**/
function usePackageDetails() {
const [output, setOutput] = useState('');
const [error, setError] = useState('');
const [fileExists, setFileExists] = useState(false);
useEffect(() => {
// Path to the package.json file
const filePath = path.join(process.cwd(), 'package.json');
// Check if the package.json file exists
fs.access(filePath, fs.constants.F_OK, err => {
if (err) {
setFileExists(false);
} else {
setFileExists(true);
// // If the file exists, start the subprocess
// const subProcess = childProcess.spawn('npx', ['lerna', 'init']);
// // Handle stdout
// subProcess.stdout.on('data', newOutput => {
// const lines = stripAnsi(newOutput.toString('utf8')).split('\n');
// setOutput(
// prevOutput => `${prevOutput}\n${lines.slice(-5).join('\n')}`,
// );
// });
// // Handle stderr
// subProcess.stderr.on('data', errOutput => {
// setError(
// prevError =>
// `${prevError}\n${stripAnsi(errOutput.toString('utf8'))}`,
// );
// });
// // Handle errors
// subProcess.on('error', err => {
// setError(prevError => `${prevError}\nError: ${err.message}`);
// });
// // Cleanup on unmount
// return () => {
// subProcess.kill();
// };
}
});
}, []);
return {output, error, fileExists};
}
class RunningProcess {
constructor(description) {
this.elapsed = 0;
this.description = description;
this.payload = null;
this.skip = false;
}
static takeTimings() {
// Implement timing logic if needed
}
}
const App = () => {
const {output, error, fileExists} = usePackageDetails();
if (!fileExists) {
return CreatePackageJson;
}
return MainThread;
// return (
// <Box flexDirection="column" padding={1}>
// <Text>Command output:</Text>
// <Box marginTop={1}>
// <Text>{fileExists ? output : 'package.json does not exist.'}</Text>
// </Box>
// {error && (
// <Box marginTop={1}>
// <Text color="red">Error:</Text>
// <Text color="red">{error}</Text>
// </Box>
// )}
// <MainThread commands={[new RunningProcess('Initializing Lerna...')]} />
// </Box>
// );
};
render(<App />);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment