Skip to content

Commit 92610a9

Browse files
authored
feat: support include & exclude in config (#75)
1 parent 4918786 commit 92610a9

12 files changed

Lines changed: 334 additions & 525 deletions

File tree

Cargo.lock

Lines changed: 13 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ members = [
66

77
[workspace.dependencies]
88
anyhow = "1.0.57"
9-
deno_config = "0.73.0"
9+
deno_config = "0.87.0"
1010
deno_path_util = "=0.6.4"
1111
js-sys = "=0.3.77"
1212
serde = "1.0.149"

config.ts

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@ import {
66
promptSelect,
77
} from "@std/cli/unstable-prompt-select";
88
import { fromFileUrl, join, resolve } from "@std/path";
9-
import {
10-
applyEdits as applyJSONCEdits,
11-
modify as modifyJSONC,
12-
parse as parseJSONC,
13-
} from "jsonc-parser";
14-
import {
15-
resolve_config,
16-
resolve_config_with_deploy_config,
17-
} from "./lib/rs_lib.js";
9+
import { parse as parseJSONC } from "@david/jsonc-morph";
10+
import { resolve_config } from "./lib/rs_lib.js";
1811
import { ValidationError } from "@cliffy/command";
1912
import { createFlow } from "./deploy/create/flow.ts";
2013
import { createApp } from "./deploy/create/mod.ts";
@@ -130,12 +123,12 @@ export async function getApp(
130123
}
131124

132125
if (selectedApp.value === null) {
133-
const data = await createFlow(context, rootPath!);
126+
const data = await createFlow(context, rootPath!, org);
134127
await createApp(
135128
context,
129+
config,
136130
data,
137131
rootPath!,
138-
false,
139132
true,
140133
);
141134
config.org = data.org;
@@ -159,6 +152,7 @@ export async function getApp(
159152
export interface ConfigContext {
160153
org: undefined | string;
161154
app: undefined | string;
155+
files: string[];
162156
configSaved: boolean;
163157
doNotCreate: boolean;
164158
save(): Promise<void>;
@@ -184,6 +178,8 @@ export function actionHandler<
184178
const config = await readConfig(
185179
rootPath?.(...args) ?? Deno.cwd(),
186180
context.config,
181+
context.ignore ?? [],
182+
context.allowNodeModules ?? false,
187183
);
188184
const configContext: ConfigContext = {
189185
...getAppFromConfig(config),
@@ -230,95 +226,84 @@ export function actionHandler<
230226
}
231227

232228
interface Config {
233-
path: string;
234-
content: string;
229+
config?: {
230+
path: string;
231+
content: string;
232+
};
233+
files: string[];
235234
}
236235

237236
async function readConfig(
238237
rootPath: string,
239238
maybeConfigPath: string | undefined,
240-
): Promise<Config | null> {
241-
rootPath = resolve(rootPath);
242-
if (maybeConfigPath) {
243-
const content = await Deno.readTextFile(maybeConfigPath);
244-
return { path: maybeConfigPath, content };
245-
}
246-
247-
// we prefer the configs with the deploy key. then we fallback to a general
248-
// config, so when we set the values, it uses existing config files instead
249-
// of trying to create a new one (which will still happen if no config file is found)
250-
251-
const configUrl = resolve_config_with_deploy_config(rootPath);
252-
253-
if (configUrl) {
254-
const path = fromFileUrl(configUrl);
255-
const content = await Deno.readTextFile(path);
256-
return { path, content };
257-
}
258-
259-
const configUrlWithoutDeployConfig = resolve_config(rootPath);
239+
ignorePaths: string[],
240+
allowNodeModules: boolean,
241+
): Promise<Config> {
242+
const config = resolve_config(
243+
resolve(maybeConfigPath || rootPath),
244+
ignorePaths,
245+
allowNodeModules,
246+
);
260247

261-
if (configUrlWithoutDeployConfig) {
262-
const path = fromFileUrl(configUrlWithoutDeployConfig);
248+
if (config.path) {
249+
const path = fromFileUrl(config.path);
263250
const content = await Deno.readTextFile(path);
264-
return { path, content };
251+
return { config: { path, content }, files: config.files };
265252
}
266253

267-
return null;
254+
return { files: config.files };
268255
}
269256

270257
function getAppFromConfig(
271-
configContent: Config | null,
272-
): { org: undefined | string; app: undefined | string } {
273-
if (configContent) {
274-
const config = parseJSONC(configContent.content);
275-
if (
276-
typeof config === "object" && config !== null && "deploy" in config &&
277-
typeof config.deploy === "object" && config.deploy !== null &&
278-
!Array.isArray(config.deploy)
279-
) {
258+
configContent: Config,
259+
): { org: undefined | string; app: undefined | string; files: string[] } {
260+
if (configContent.config) {
261+
const config = parseJSONC(configContent.config.content);
262+
const deployObj = config.asObject()?.getIfObject("deploy");
263+
264+
if (deployObj) {
280265
return {
281-
org: config.deploy.org,
282-
app: config.deploy.app,
266+
org: deployObj.get("org")?.value()?.asString(),
267+
app: deployObj.get("app")?.value()?.asString(),
268+
files: configContent.files,
283269
};
284270
}
285271
}
286272

287273
return {
288274
org: undefined,
289275
app: undefined,
276+
files: configContent.files,
290277
};
291278
}
292279

293280
async function writeConfig(
294-
configContent: Config | null,
281+
configContent: Config,
295282
{ org, app }: { org: undefined | string; app: undefined | string },
296283
) {
297284
if (!org) {
298285
return;
299286
}
300287

301-
const content = configContent?.content ?? "{}\n";
288+
const content = configContent.config?.content ?? "{}\n";
302289

303290
const newConfig: Record<string, string> = { org };
304291

305292
if (app) {
306293
newConfig.app = app;
307294
}
308295

309-
const edits = modifyJSONC(content, ["deploy"], newConfig, {
310-
formattingOptions: {
311-
insertSpaces: true,
312-
tabSize: 2,
313-
},
314-
});
315-
const out = applyJSONCEdits(content, edits);
296+
const config = parseJSONC(content);
297+
const deployObj = config.asObjectOrForce().getIfObjectOrForce("deploy");
298+
deployObj.replaceWith(newConfig);
299+
deployObj.ensureMultiline();
300+
316301
await Deno.writeTextFile(
317-
configContent?.path ?? join(Deno.cwd(), "deno.jsonc"),
318-
out,
302+
configContent.config?.path ?? join(Deno.cwd(), "deno.jsonc"),
303+
config.toString() + "\n",
319304
);
320305

321-
if (!configContent) {
306+
if (!configContent.config) {
322307
console.log(
323308
`Created configuration file at '${join(Deno.cwd(), "deno.jsonc")}'`,
324309
);

deno.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
},
1616
"exports": "./main.ts",
1717
"imports": {
18-
"@cfa/gitignore-parser": "jsr:@cfa/gitignore-parser@^0.1.4",
1918
"@cliffy/command": "jsr:@cliffy/command@^1.0.0",
19+
"@david/jsonc-morph": "jsr:@david/jsonc-morph@^0.3.1",
2020
"@deno/sandbox": "jsr:@deno/sandbox@^0.10.0",
2121
"@std/assert": "jsr:@std/assert@^1.0.16",
2222
"@std/async": "jsr:@std/async@^1.1.0",
@@ -25,15 +25,13 @@
2525
"@std/encoding": "jsr:@std/encoding@^1.0.10",
2626
"@std/fmt": "jsr:@std/fmt@^1.0.8",
2727
"@std/fs": "jsr:@std/fs@^1.0.19",
28-
"@std/jsonc": "jsr:@std/jsonc@^1.0.2",
2928
"@std/path": "jsr:@std/path@^1.1.2",
3029
"@std/semver": "jsr:@std/semver@^1.0.8",
3130
"@std/tar": "jsr:@std/tar@^0.1.9",
3231
"@trpc/client": "npm:@trpc/client@^11.0.2",
3332
"@trpc/server": "npm:@trpc/server@^11.0.2",
3433
"@types/prompts": "npm:@types/prompts@2.4.9",
3534
"event-source-polyfill": "npm:event-source-polyfill@^1.0.31",
36-
"jsonc-parser": "npm:jsonc-parser@^3.3.1",
3735
"open": "npm:open@^10.1.0",
3836
"superjson": "npm:superjson@^2.2.2",
3937
"@deno/framework-detect": "jsr:@deno/framework-detect@^0.3",

0 commit comments

Comments
 (0)