Skip to content

Commit 17c371d

Browse files
authored
Merge pull request #74 from bholmesdev/feat/snippet-prompt
query: prompt for snippets
2 parents 0051e5c + 3b01231 commit 17c371d

File tree

5 files changed

+4551
-3151
lines changed

5 files changed

+4551
-3151
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"simple-stack-query": patch
3+
---
4+
5+
Add CLI prompt to add VS Code snippets in development.

packages/query/fixtures/basic/astro.config.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ import { defineConfig } from "astro/config";
22
import simpleStackQuery from "simple-stack-query";
33

44
export default defineConfig({
5-
integrations: [simpleStackQuery()],
5+
integrations: [
6+
simpleStackQuery({
7+
bypassSnippetsPrompt: true,
8+
}),
9+
],
610
});

packages/query/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"directory": "packages/query"
2222
},
2323
"dependencies": {
24+
"@clack/prompts": "^0.7.0",
25+
"kleur": "^4.1.5",
2426
"vite-plugin-simple-scope": "workspace:*"
2527
},
2628
"engines": {

packages/query/src/index.ts

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1+
import { mkdir, writeFile } from "node:fs/promises";
12
import type { AstroConfig, AstroIntegration } from "astro";
3+
import { cyan } from "kleur/colors";
24
import vitePluginSimpleScope from "vite-plugin-simple-scope";
35

46
import "../ambient.d.ts";
7+
import { existsSync } from "node:fs";
58

69
type VitePlugin = Required<AstroConfig["vite"]>["plugins"][number];
710

8-
export default function simpleStackQueryIntegration(): AstroIntegration {
11+
type Options = {
12+
bypassSnippetsPrompt?: boolean;
13+
};
14+
15+
export default function simpleStackQueryIntegration(
16+
opts?: Options,
17+
): AstroIntegration {
18+
let root: URL;
19+
let command: "dev" | "build" | "preview";
920
return {
1021
name: "simple-stack-query",
1122
hooks: {
12-
"astro:config:setup"({ updateConfig }) {
13-
updateConfig({
23+
"astro:server:start": async () => {
24+
if (command === "dev" && !opts?.bypassSnippetsPrompt) {
25+
setTimeout(() => {
26+
addSnippets({ root });
27+
}, 100);
28+
}
29+
},
30+
async "astro:config:setup"(params) {
31+
root = params.config.root;
32+
command = params.command;
33+
34+
params.updateConfig({
1435
vite: {
1536
plugins: [vitePlugin(), vitePluginSimpleScope()],
1637
},
@@ -46,3 +67,53 @@ function vitePlugin(): VitePlugin {
4667
},
4768
};
4869
}
70+
71+
async function addSnippets({ root }: { root: URL }) {
72+
const dotAstroDir = new URL(".astro/", root);
73+
const snippetsResponseFile = new URL(
74+
"simple-query-snippets-response",
75+
dotAstroDir,
76+
);
77+
if (existsSync(snippetsResponseFile)) return;
78+
79+
const { confirm, isCancel, outro } = await import("@clack/prompts");
80+
const shouldAddSnippets = await confirm({
81+
message:
82+
"Simple query offers snippets for VS Code. Would you like to add them?",
83+
});
84+
85+
if (isCancel(shouldAddSnippets)) process.exit(0);
86+
87+
await mkdir(dotAstroDir, { recursive: true });
88+
await writeFile(snippetsResponseFile, shouldAddSnippets ? "true" : "false");
89+
90+
if (!shouldAddSnippets) {
91+
outro(`No problem! Won't ask again. ${cyan("Dev server running.")}`);
92+
return;
93+
}
94+
95+
const vsCodeDir = new URL(".vscode/", root);
96+
const vsCodeSnippetsFile = new URL("simple-query.code-snippets", vsCodeDir);
97+
98+
await mkdir(vsCodeDir, { recursive: true });
99+
await writeFile(
100+
vsCodeSnippetsFile,
101+
JSON.stringify(
102+
{
103+
"query target": {
104+
scope: "typescriptreact,javascriptreact",
105+
prefix: "$:target",
106+
body: ["data-target={$('$1')}"],
107+
},
108+
"query ready block": {
109+
scope: "typescript,javascript",
110+
prefix: "$:ready",
111+
body: ["$.ready(async () => {", " $1", "});"],
112+
},
113+
},
114+
null,
115+
2,
116+
),
117+
);
118+
outro(`Snippets added ✔️\n ${cyan("Dev server running.")}`);
119+
}

0 commit comments

Comments
 (0)