-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.ts
More file actions
145 lines (124 loc) · 4.3 KB
/
helpers.ts
File metadata and controls
145 lines (124 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import * as path from "node:path";
import { ux } from "@oclif/core";
import * as fs from "fs-extra";
import { DirContext } from "@/lib/helpers/fs";
import { isPathArg, resolvePathArg } from "@/lib/helpers/path";
import {
ProjectConfig,
resolveResourceDir,
} from "@/lib/helpers/project-config";
import { PartialDirContext, RunContext } from "@/lib/run-context";
import { PARTIAL_JSON } from "./processor.isomorphic";
export const partialJsonPath = (partialDirCtx: PartialDirContext): string =>
path.resolve(partialDirCtx.abspath, PARTIAL_JSON);
/*
* Check for partial.json file and return the file path if present.
*/
export const lsPartialJson = async (
dirPath: string,
): Promise<string | undefined> => {
const partialJsonPath = path.resolve(dirPath, PARTIAL_JSON);
const exists = await fs.pathExists(partialJsonPath);
return exists ? partialJsonPath : undefined;
};
/*
* Evaluates whether the given directory path is a partial directory, by
* checking for the presence of partial.json file.
*/
export const isPartialDir = async (dirPath: string): Promise<boolean> =>
Boolean(await lsPartialJson(dirPath));
/*
* Validate the provided args and flags with the current run context, to first
* ensure the invoked command makes sense, and return the target context.
*/
type CommandTargetProps = {
flags: {
all: boolean | undefined;
"partials-dir": DirContext | undefined;
};
args: {
partialKey: string | undefined;
};
};
type PartialDirTarget = {
type: "partialDir";
context: PartialDirContext;
};
type PartialsIndexDirTarget = {
type: "partialsIndexDir";
context: DirContext;
};
export type PartialCommandTarget = PartialDirTarget | PartialsIndexDirTarget;
export const ensureValidCommandTarget = async (
props: CommandTargetProps,
runContext: RunContext,
projectConfig?: ProjectConfig,
): Promise<PartialCommandTarget> => {
const { args, flags } = props;
const { commandId, resourceDir: resourceDirCtx, cwd: runCwd } = runContext;
// If the target resource is a different type than the current resource dir
// type, error out.
if (resourceDirCtx && resourceDirCtx.type !== "partial") {
return ux.error(
`Cannot run ${commandId} inside a ${resourceDirCtx.type} directory`,
);
}
// Cannot accept both partial key arg and --all flag.
if (flags.all && args.partialKey) {
return ux.error(
`partialKey arg \`${args.partialKey}\` cannot also be provided when using --all`,
);
}
// Default to knock project config first if present, otherwise cwd.
const partialsIndexDirCtx = await resolveResourceDir(
projectConfig,
"partial",
runCwd,
);
// --all flag is given, which means no partial key arg.
if (flags.all) {
// If --all flag used inside a partial directory, then require a partials
// dir path.
if (resourceDirCtx && !flags["partials-dir"]) {
return ux.error("Missing required flag partials-dir");
}
return {
type: "partialsIndexDir",
context: flags["partials-dir"] || partialsIndexDirCtx,
};
}
// Partial key arg is given, which means no --all flag.
if (args.partialKey) {
if (isPathArg(args.partialKey)) {
const { key, abspath } = resolvePathArg(args.partialKey);
const partialDirCtx: PartialDirContext = {
type: "partial",
key,
abspath,
exists: await isPartialDir(abspath),
};
return { type: "partialDir", context: partialDirCtx };
}
if (resourceDirCtx && resourceDirCtx.key !== args.partialKey) {
return ux.error(
`Cannot run ${commandId} \`${args.partialKey}\` inside another partial directory:\n${resourceDirCtx.key}`,
);
}
const targetDirPath = resourceDirCtx
? resourceDirCtx.abspath
: path.resolve(partialsIndexDirCtx.abspath, args.partialKey);
const partialDirCtx: PartialDirContext = {
type: "partial",
key: args.partialKey,
abspath: targetDirPath,
exists: await isPartialDir(targetDirPath),
};
return { type: "partialDir", context: partialDirCtx };
}
// From this point on, we have neither a partial key arg nor --all flag.
// If running inside a partial directory, then use that partial directory.
if (resourceDirCtx) {
return { type: "partialDir", context: resourceDirCtx };
}
return ux.error("Missing 1 required arg:\npartialKey");
};