Skip to content

Commit 18da28c

Browse files
committed
feat: basic history command
1 parent fde1ce8 commit 18da28c

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

README.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ P.S. These commands consider the current directory as the base path for every op
123123
#### User Information
124124
- **Get User Info**: Display user information.
125125
```bash
126-
puter whoami
126+
puter> whoami
127127
```
128128

129129
#### Disk Usage
130130
- **Check Disk Usage**: Display disk usage information.
131131
```bash
132-
puter df
132+
puter> df
133133
```
134134
- **Get Usage Info**: Fetch usage information for services.
135135
```bash
136-
puter usage
136+
puter> usage
137137
```
138138

139139
#### Application Management
@@ -142,25 +142,25 @@ The **Application** are sepcial type of hosted web app, they're served from the
142142

143143
- **List Applications**: List all applications.
144144
```bash
145-
puter apps [period]
145+
puter> apps [period]
146146
```
147147
P.S. Please check the help command `help apps` for more details about any argument.
148148

149149
- **Create Application**: Create a new application.
150150
```bash
151-
puter app:create <name> [<directory>] [--description="My App Description"] [--url=<url>]
151+
puter> app:create <name> [<directory>] [--description="My App Description"] [--url=<url>]
152152
```
153153
P.S. By default a new `index.html` with basic content will be created, but you can set a directory when you create a new application as follows: `app:create nameOfApp ./appDir`, so all files will be copied to the `AppData` directoy, you can then update your app using `app:update <name> <remote_dir>`. This command will attempt to create a subdomain with a random `uid` prefixed with the name of the app.
154154

155155
- **Update Application**: Update an application.
156156
```bash
157-
puter app:update <name> <remote_dir>
157+
puter> app:update <name> <remote_dir>
158158
```
159159
**IMPORTANT** All existing files will be overwritten, new files are copied, other files are just ignored.
160160

161161
- **Delete Application**: Delete an application.
162162
```bash
163-
puter app:delete [-f] <name>
163+
puter> app:delete [-f] <name>
164164
```
165165
P.S. This command will lookup for the allocated `subdomain` and attempt to delete it if it exists.
166166

@@ -170,20 +170,31 @@ The static sites are served from the selected directory (or the current director
170170

171171
- **Deploy Site**: Deploy a static website from a directory.
172172
```bash
173-
puter site:create <app_name> [<dir>] [--subdomain=<name>]
173+
puter> site:create <app_name> [<dir>] [--subdomain=<name>]
174174
```
175175
P.S. If the subdomain already exists, it will generate a new random one can set your own subdomain using `--subdomain` argument.
176176

177177
- **List Sites**: List all hosted sites.
178178
```bash
179-
puter sites
179+
puter> sites
180180
```
181181
- **Delete Site**: Delete a hosted site.
182182
```bash
183-
puter site:delete <uid>
183+
puter> site:delete <uid>
184184
```
185185
P.S. You can find the `<uid>` in the list of `sites`.
186186

187+
#### Commands history
188+
189+
- **Display history**: Display the history executed commands
190+
```bash
191+
puter> history
192+
```
193+
- **Copy command from history**: Copy a previous command from history by line number
194+
```bash
195+
puter> history <line_number>
196+
```
197+
187198
#### Interactive Shell
188199
- **Start Shell**: Launch an interactive shell.
189200
```bash
@@ -198,10 +209,14 @@ or just type (you'll need to login):
198209
- **General Help**: Display a list of available commands.
199210
```bash
200211
puter help
212+
# or inside the interactive shell:
213+
puter> help
201214
```
202215
- **Command Help**: Display detailed help for a specific command.
203216
```bash
204217
puter help <command>
218+
# or inside the interactive shell:
219+
puter> help <command>
205220
```
206221

207222
---

src/commands/shell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { PROJECT_NAME } from '../commons.js';
77

88
const config = new Conf({ projectName: PROJECT_NAME });
99

10-
const rl = readline.createInterface({
10+
export const rl = readline.createInterface({
1111
input: process.stdin,
1212
output: process.stdout,
1313
prompt: null

src/executor.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import { PROJECT_NAME, API_BASE, getHeaders } from './commons.js';
1111
import inquirer from 'inquirer';
1212
import { exec } from 'node:child_process';
1313
import { parseArgs } from './utils.js';
14+
import { rl } from './commands/shell.js';
1415

1516
const config = new Conf({ projectName: PROJECT_NAME });
1617

18+
// History of commands
19+
const commandHistory = [];
20+
1721
/**
1822
* Update the prompt function
1923
* @returns The current prompt
@@ -37,6 +41,26 @@ const commands = {
3741
});
3842
},
3943
app: appInfo,
44+
history: async (args) => {
45+
const lineNumber = parseInt(args[0]);
46+
47+
if (isNaN(lineNumber)) {
48+
// Display full history
49+
commandHistory.forEach((command, index) => {
50+
console.log(chalk.cyan(`${index + 1}: ${command}`));
51+
});
52+
} else {
53+
// Copy the command at the specified line number
54+
if (lineNumber < 1 || lineNumber > commandHistory.length) {
55+
console.error(chalk.red(`Invalid line number. History has ${commandHistory.length} entries.`));
56+
return;
57+
}
58+
59+
const commandToCopy = commandHistory[lineNumber - 1];
60+
// Simulate typing the command in the shell
61+
rl.write(commandToCopy);
62+
}
63+
},
4064
'app:create': async (rawArgs) => {
4165
try {
4266
const args = parseArgs(rawArgs.join(' '));
@@ -119,7 +143,7 @@ const commands = {
119143
sites: listSites,
120144
site: infoSite,
121145
'site:delete': deleteSite,
122-
'site:create': createSite
146+
'site:create': createSite,
123147
};
124148

125149
/**
@@ -129,6 +153,12 @@ const commands = {
129153
export async function execCommand(input) {
130154
const [cmd, ...args] = input.split(' ');
131155

156+
157+
// Add the command to history (skip the "history" command itself)
158+
if (cmd !== 'history') {
159+
commandHistory.push(input);
160+
}
161+
132162
if (cmd === 'help') {
133163
// Handle help command
134164
const command = args[0];
@@ -312,6 +342,11 @@ function showHelp(command) {
312342
Execute a command on the host machine.
313343
Example: !ls -la
314344
`,
345+
'history [line]': `
346+
${chalk.cyan('history [line]')}
347+
Display history of commands or copy command by line number
348+
Example: history 2
349+
`,
315350
};
316351

317352
if (command && commandHelp[command]) {

0 commit comments

Comments
 (0)