Skip to content

Commit

Permalink
fedify init: Create VS Code settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jul 21, 2024
1 parent 3ef9e02 commit f396dc9
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"examples/hono-sample"
],
"deno.unstable": true,
"editor.detectIndentation": false,
"editor.indentSize": 2,
"editor.insertSpaces": true,
"files.eol": "\n",
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
Expand Down
240 changes: 209 additions & 31 deletions cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Select } from "@cliffy/prompt";
import { getLogger } from "@logtape/logtape";
import { dirname, join } from "@std/path";
import { format, greaterThan, parse } from "@std/semver";
import { l } from "../../../../.cache/deno/npm/registry.npmjs.org/@shikijs/core/1.6.5/dist/chunk-index.d.mts";
import metadata from "./deno.json" with { type: "json" };

type Runtime = "deno" | "bun" | "node";
Expand Down Expand Up @@ -198,7 +199,7 @@ export default app;
`,
"src/index.ts": runtime === "node"
? `\
import { serve } from "@hono/node-server"
import { serve } from "@hono/node-server";
import app from "./app";
serve(
Expand Down Expand Up @@ -523,7 +524,7 @@ export const command = new Command()
files: {
"main.ts": runtime === "node"
? `\
import { serve } from '@hono/node-server'
import { serve } from "@hono/node-server";
import federation from "./federation";
serve(
Expand Down Expand Up @@ -694,45 +695,186 @@ export default federation;
dir,
dependencies,
);
if (runtime !== "deno") {
const devDependencies: Record<string, string> = {
"@biomejs/biome": "^1.8.3",
};
await addDependencies(
runtime,
packageManager,
dir,
devDependencies,
true,
);
}
for (const [filename, content] of Object.entries(files)) {
const path = join(dir, filename);
const dirName = dirname(path);
await Deno.mkdir(dirName, { recursive: true });
await Deno.writeTextFile(path, content);
}
if (runtime === "deno") {
const cfgPath = join(dir, "deno.json");
const cfg = JSON.parse(await Deno.readTextFile(cfgPath));
await Deno.writeTextFile(
cfgPath,
JSON.stringify(
{
...cfg,
unstable: [
"temporal",
...kvStoreDesc.denoUnstable ?? [],
...mqDesc.denoUnstable ?? [],
],
tasks: { ...cfg.tasks, ...initializer.tasks },
await rewriteJsonFile(
join(dir, "deno.json"),
{},
(cfg) => ({
...cfg,
unstable: [
"temporal",
...kvStoreDesc.denoUnstable ?? [],
...mqDesc.denoUnstable ?? [],
],
tasks: { ...cfg.tasks, ...initializer.tasks },
}),
);
await rewriteJsonFile(
join(dir, ".vscode", "settings.json"),
{},
(vsCodeSettings) => ({
"deno.enable": true,
"deno.unstable": true,
"editor.detectIndentation": false,
"editor.indentSize": 2,
"editor.insertSpaces": true,
"[javascript]": {
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.sortImports": "always",
},
},
"[javascriptreact]": {
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.sortImports": "always",
},
},
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features",
"editor.formatOnSave": true,
},
null,
2,
),
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features",
"editor.formatOnSave": true,
},
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.sortImports": "always",
},
},
"[typescriptreact]": {
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.sortImports": "always",
},
},
...vsCodeSettings,
}),
);
await rewriteJsonFile(
join(dir, ".vscode", "extensions.json"),
{},
(vsCodeExtensions) => ({
recommendations: uniqueArray([
"denoland.vscode-deno",
...vsCodeExtensions.recommendations ?? [],
]),
...vsCodeExtensions,
}),
);
} else {
const cfgPath = join(dir, "package.json");
const cfg = JSON.parse(await Deno.readTextFile(cfgPath));
await Deno.writeTextFile(
cfgPath,
JSON.stringify(
{
type: "module",
...cfg,
scripts: { ...cfg.scripts, ...initializer.tasks },
await rewriteJsonFile(
join(dir, "package.json"),
{},
(cfg) => ({
type: "module",
...cfg,
scripts: { ...cfg.scripts, ...initializer.tasks },
}),
);
await rewriteJsonFile(
join(dir, ".vscode", "settings.json"),
{},
(vsCodeSettings) => ({
"editor.detectIndentation": false,
"editor.indentSize": 2,
"editor.insertSpaces": true,
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports.biome": "always",
},
},
"[javascriptreact]": {
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports.biome": "always",
},
},
"[json]": {
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
},
"[jsonc]": {
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports.biome": "always",
},
},
null,
2,
),
"[typescriptreact]": {
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports.biome": "always",
},
},
...vsCodeSettings,
}),
);
await rewriteJsonFile(
join(dir, ".vscode", "extensions.json"),
{},
(vsCodeExtensions) => ({
recommendations: uniqueArray([
"biomejs.biome",
...vsCodeExtensions.recommendations ?? [],
]),
...vsCodeExtensions,
}),
);
await rewriteJsonFile(
join(dir, "biome.json"),
{},
(cfg) => ({
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
...cfg,
organizeImports: {
...cfg.organizeImports,
enabled: true,
},
formatter: {
...cfg.formatter,
enabled: true,
indentStyle: "space",
indentWidth: 2,
},
linter: {
...cfg.linter,
enabled: true,
rules: { recommended: true },
},
}),
);
}
console.error(initializer.instruction);
Expand Down Expand Up @@ -788,6 +930,7 @@ async function addDependencies(
pm: PackageManager,
dir: string,
dependencies: Record<string, string>,
dev: boolean = false,
): Promise<void> {
const deps = Object.entries(dependencies)
.map(([name, version]) =>
Expand All @@ -799,7 +942,13 @@ async function addDependencies(
const cmd = new Deno.Command(
runtime === "node" ? pm : runtime,
{
args: ["add", ...deps],
args: [
"add",
...(dev
? [runtime === "bun" || pm === "yarn" ? "--dev" : "--save-dev"]
: []),
...uniqueArray(deps),
],
cwd: dir,
stdin: "inherit",
stdout: "inherit",
Expand Down Expand Up @@ -828,3 +977,32 @@ async function getLatestFedifyVersion(version: string): Promise<string> {
}
return format(maxVersion);
}

async function rewriteJsonFile(
path: string,
// deno-lint-ignore no-explicit-any
empty: any,
// deno-lint-ignore no-explicit-any
rewriter: (json: any) => any,
): Promise<void> {
let jsonText: string | null = null;
try {
jsonText = await Deno.readTextFile(path);
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) throw e;
}
let json = jsonText == null ? empty : JSON.parse(jsonText);
json = rewriter(json);
await Deno.mkdir(dirname(path), { recursive: true });
await Deno.writeTextFile(path, JSON.stringify(json, null, 2));
}

function uniqueArray<T extends boolean | number | string>(a: T[]): T[] {
const result: T[] = [];
for (const v of a) {
if (!result.includes(v)) result.push(v);
}
return result;
}

// cSpell: ignore denoland biomejs

0 comments on commit f396dc9

Please sign in to comment.