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

NPM testing #4

Merged
merged 7 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions prompts/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Building

```sh
#docker:command=build
docker build -t vonwig/prompts -f Dockerfile .
#docker:command=builds
docker build -t vonwig/prompts -f prompts/Dockerfile prompts
```

```sh
Expand All @@ -16,7 +16,7 @@ To run this project, use the following run command:

```sh
#docker:command=run
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock vonwig/prompts /Users/slim/docker/labs-make-runbook jimclark106 darwin npm
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock vonwig/prompts $PWD my_docker_username darwin npm
```

The four arguments are `project root dir`, `docker username`, `platform`, and a top-level prompt folder.
Expand Down
60 changes: 27 additions & 33 deletions prompts/npm/010_system_prompt.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
You are an assistant who specializes in making runbooks for NPM projects,
allowing any developer to quickly run a docker project locally for development.
You are an assistant who specializes in making runbooks for NPM projects, allowing any developer to quickly run a docker project locally for development.

Since you are an expert and know about their project, be definitive about recommendations.

A runbook for an npm project contains the following steps:
A runbook for an npm project contains the following:

# Setup:

NVM:
Check for NVM or install it with the system's package manager.
Example:
```sh
brew install nvm
```

Node and NPM:
Prepare node using nvm, and select the correct package manager

Run Package Manager:
Depending on npm vs yarn, run an install

# Run:
Analyze package.json for scripts.
Then, for each node root, you need to do the following:

# Node Root

{{#project.node_roots}}
The project has a node root package.json at {{path}} with the contents {{content}}.
Because there is already a root, the project does not need to be converted to npm.
{{/project.node_roots}}
{{^project.node_roots}}
The project does not have a node root, so the user should run `npm init`
{{/project.node_roots}}
Add a block to cd into the node root

```sh
cd $node_root
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how these are actionable without an underlying kernel that can maintain this between code blocks. Maybe this is just something we have to track as potential feedback and look for signals that users want us to solve this?

```

Node and NPM:
Prepare node using nvm, and select the correct package manager.
Example:
```sh
nvm use 20
```

{{#project.version_artifacts}}
The project has a version declaration file {{path}} with the contents {{content}}
{{/project.version_artifacts}}
{{^project.version_artifacts}}
The project does not have any version artifacts, so default to latest node LTS
{{/project.version_artifacts}}

The user has the following top level project files:

{{#project.files}}
{{.}}
{{/project.files}}

If there is a yarn.lock, use `yarn` in place of npm commands.
Run Package Manager:
Depending on npm vs yarn, run an install
Example
```sh
yarn install
```

Run `npm install`
Run scripts

27 changes: 26 additions & 1 deletion prompts/npm/020_user_prompt.md
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
I would like to run this node project.
I have the following project open:

--- Project ---

My project has these files:
{{#project.files}}
{{.}}
{{/project.files}}

If you see that I have a yarn.lock, please use `yarn` in place of npm commands.

{{#project.node_roots}}
--- Node Root ---
My project has a node root package.json at {{path}} and uses node version {{version}}.
The node root has the following scripts
{{scripts}}
-----------------
{{/project.node_roots}}
{{^project.node_roots}}
The project does not have a node root, so help me run `npm init`
{{/project.node_roots}}

Please generate a runbook for me.



22 changes: 12 additions & 10 deletions prompts/npm/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
FROM ubuntu:20.04
FROM alpine:3.20

WORKDIR /app
# Give me bash
RUN apk add --no-cache bash

RUN <<EOF
cat > /app/run.sh <<'EOF2'
#!/bin/bash
VOLUME /project

echo '{"project": {"node_roots": [{"path": "package.json", "content": "some stuff"}]}}'
EOF2
chmod 755 /app/run.sh
EOF
WORKDIR /

COPY ./scripts/build-node-roots.sh /build-node-roots.sh

# MAke the thing executable
RUN chmod +x build-node-roots.sh

COPY ./scripts/payload.json /payload.json
# when the container is running the project is mounted at /project read-only
ENTRYPOINT ["/app/run.sh"]
ENTRYPOINT ["/build-node-roots.sh"]
2 changes: 1 addition & 1 deletion prompts/npm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ It relies on an image to extract some additional facts about the project

```sh
#docker:command=build-npm-extractor
docker build -t vonwig/node-extractor -f ./npm/Dockerfile ./npm
docker build -t vonwig/node-extractor -f ./prompts/npm/Dockerfile ./prompts/npm
```

## Running the extraction image
Expand Down
7 changes: 7 additions & 0 deletions prompts/npm/scripts/build-node-roots.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
# Echo JSON with project.node_roots[]

cat payload.json

# Use JQ to get scripts
# jq -r '.project.node_roots[]' payload.json
45 changes: 45 additions & 0 deletions prompts/npm/scripts/payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
ColinMcNeil marked this conversation as resolved.
Show resolved Hide resolved
"project": {
"node_roots": [
{
"path": "client/package.json",
"version": "18",
"scripts": {
"vscode:prepublish": "yarn run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "yarn run compile && yarn run lint",
"lint": "eslint src --ext ts",
"test": "vscode-test",
"package": "vsce package"
}
},
{
"path": "client/desktop-ui/package.json",
"version": "18",
"scripts": {
"vscode:prepublish": "yarn run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "yarn run compile && yarn run lint",
"lint": "eslint src --ext ts",
"test": "vscode-test",
"package": "vsce package"
}
},
{
"path": "client/desktop/package.json",
"version": "20",
"scripts": {
"vscode:prepublish": "yarn run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "yarn run compile && yarn run lint",
"lint": "eslint src --ext ts",
"test": "vscode-test",
"package": "vsce package"
}
}
]
}
}
63 changes: 26 additions & 37 deletions src/commands/runHotCommand.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
import * as vscode from "vscode";
import { workspaceCommands } from "../extension";

export const runHotCommand = async () => {
if (!vscode.workspace.workspaceFolders) {
return vscode.window.showErrorMessage("No workspace open.");
}

let workspace = vscode.workspace.workspaceFolders[0];

if (vscode.window.activeTextEditor) {
workspace = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri) || workspace;
const groupCommands = (blocks: (typeof workspaceCommands)[string]) => blocks.reduce((acc, { command, script }) => {
if (!acc[command]) {
acc[command] = '';
}
acc[command] += script;
return acc;
}, {} as Record<string, string>);

const commands: { command: string; script: string }[] =
workspaceCommands[`docker-run-${workspace.uri.fsPath}`] || [];

if (!commands || commands.length === 0) {
vscode.window.showErrorMessage("No commands bound to workspace");
return;
export const runHotCommand = async () => {
const quickPicks = [];
if (Object.keys(workspaceCommands).length === 0) {
return vscode.window.showErrorMessage('No runbooks found in workspace');
}

// Combines commands with mathcing names
const combinedCommands = commands.reduce(
(acc: { [key: string]: string }, { command, script }) => {
if (acc[command]) {
acc[command] += `\n${script}`;
} else {
acc[command] = script;
}
return acc;
},
{}
) as { [key: string]: string };

const quickPicks = Object.entries(combinedCommands).map(
([tag, command]) => ({
label: tag,
description: `${command.split(/\s+/).splice(0,3).join(" ")} ...`,
detail: command,
})
);
for (const [runbookFSPath, blocks] of Object.entries(workspaceCommands)) {
quickPicks.push({
label: runbookFSPath,
description: ``,
detail: ``,
kind: vscode.QuickPickItemKind.Separator,
});

const groupedBlocks = groupCommands(blocks);

quickPicks.push(...Object.entries(groupedBlocks).map(([command, script]) => ({
label: command,
detail: script,
})));
};

void vscode.window.showQuickPick(quickPicks).then((tag) => {
if (!tag) {
return;
}

const terminalIdentifier = vscode.workspace.workspaceFolders!.length > 1 ? `[${workspace.name}]-${tag.label}` : tag.label;
const terminalIdentifier = tag.label;

const existingTerminal = vscode.window.terminals.find(
terminal => terminal.name === terminalIdentifier
Expand Down
6 changes: 2 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ export async function activate(context: vscode.ExtensionContext) {
}) => {
const blocks = args.blocks;

const workspace = vscode.workspace.getWorkspaceFolder(vscode.Uri.parse(args.uri));
const runbookURI = vscode.Uri.parse(args.uri);

const id = `docker-run-${workspace?.uri.fsPath}`;

workspaceCommands[id] = blocks;
workspaceCommands[runbookURI.fsPath] = blocks;
});

dockerLSP.onNotification("$terminal/run", async (args: { content: string }) => {
Expand Down