Skip to content

Commit 9c4f7bd

Browse files
committed
feat(core): Added resolveModule function to utils built-in
1 parent 22b8a35 commit 9c4f7bd

11 files changed

Lines changed: 1130 additions & 98 deletions

File tree

packages/core/src/components/utils-builtin.tsx

Lines changed: 907 additions & 2 deletions
Large diffs are not rendered by default.

packages/core/src/plugin.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,11 @@ export interface GlobalOptions {
514514
`Final command input list: \n${this.inputs
515515
.map(
516516
command =>
517-
` - ${command.id}: ${replacePath(
518-
command.entry.file,
519-
this.commandsPath
520-
)}${command.isVirtual ? " (virtual)" : ""}`
517+
` - ${command.id}: ${
518+
isParentPath(command.entry.file, this.commandsPath)
519+
? replacePath(command.entry.file, this.commandsPath)
520+
: replacePath(command.entry.file, this.config.root)
521+
}${command.isVirtual ? " (virtual)" : ""}`
521522
)
522523
.join("\n")}`
523524
);

packages/core/src/resolver/resolve.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async function initialize<TContext extends Context = Context>(
8686
}
8787

8888
context.debug(
89-
`Adding reflection for user-defined command: ${command.id} (file: ${
89+
`Adding reflection for application command: ${command.id} (file: ${
9090
command.entry.input.file
9191
})`
9292
);
@@ -95,6 +95,7 @@ async function initialize<TContext extends Context = Context>(
9595
context,
9696
command.entry.input,
9797
{
98+
name: `${command.title} Command Module`,
9899
plugins: [
99100
esbuildPlugin(context, {
100101
reflection: "default",

packages/nx/tsconfig.lib.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
"types": ["node"],
66
"outDir": "./out-tsc"
77
},
8-
"references": [{ "path": "../core/tsconfig.lib.json" }],
8+
"references": [
9+
{
10+
"path": "../core/tsconfig.lib.json"
11+
}
12+
],
913
"include": [
1014
"src/**/*.ts",
1115
"src/**/*.d.ts",

packages/plugin-upgrade/src/components/upgrade-builtin.tsx

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,126 @@ export function CheckForUpdatesFunctionDeclaration() {
959959
);
960960
}
961961

962+
/**
963+
* The `getChangelog` handler function declaration code for the Shell Shock project.
964+
*/
965+
export function GetChangelogFunctionDeclaration() {
966+
const context = usePowerlines<UpgradePluginContext>();
967+
968+
return (
969+
<>
970+
<InterfaceDeclaration
971+
export
972+
name="GetChangelogOptions"
973+
doc="Options for the `getChangelog` handler function.">
974+
<InterfaceMember
975+
name="start"
976+
optional
977+
type="string"
978+
doc="The starting version for the changelog."
979+
/>
980+
<hbr />
981+
<InterfaceMember
982+
name="end"
983+
optional
984+
type="string"
985+
doc="The ending version for the changelog."
986+
/>
987+
<hbr />
988+
</InterfaceDeclaration>
989+
<Spacing />
990+
<TSDoc heading="Get the changelog for the application dependencies.">
991+
<TSDocRemarks>
992+
{`This function is used to get the changelog for the application dependencies. It can be used in the CLI upgrade command to view the changes between versions.`}
993+
</TSDocRemarks>
994+
<Spacing />
995+
<TSDocParam name="options">
996+
{`The options for the \`getChangelog\` function. Currently, there are no options available, but this parameter is included for future extensibility.`}
997+
</TSDocParam>
998+
<TSDocReturns>
999+
{`A promise that resolves when the changelog is retrieved or undefined if the changelog was not retrieved.`}
1000+
</TSDocReturns>
1001+
</TSDoc>
1002+
<FunctionDeclaration
1003+
export
1004+
async
1005+
name="getChangelog"
1006+
parameters={[
1007+
{
1008+
name: "options",
1009+
type: "GetChangelogOptions",
1010+
default: "{}"
1011+
}
1012+
]}
1013+
returnType="Promise<string | undefined>">
1014+
{code`
1015+
try {
1016+
const resolved = await resolveModule("${context.packageJson.name}");
1017+
if (resolved && existsSync(join(resolved, "CHANGELOG.md"))) {
1018+
const markdown = await readFile(join(resolved, "CHANGELOG.md"), "utf8");
1019+
1020+
const versionPattern = /^#{1,2}\\s+\\[?(\\d+\\.\\d+\\.\\d+[^\\]\\s]*)\\]?/;
1021+
const lines = markdown.split("\\n");
1022+
const sections: { version: string; content: string }[] = [];
1023+
let current: { version: string; lines: string[] } | undefined;
1024+
1025+
for (const line of lines) {
1026+
const match = line.match(versionPattern);
1027+
if (match) {
1028+
if (current) {
1029+
sections.push({ version: current.version, content: current.lines.join("\\n").trim() });
1030+
}
1031+
current = { version: match[1], lines: [line] };
1032+
} else if (current) {
1033+
current.lines.push(line);
1034+
}
1035+
}
1036+
if (current) {
1037+
sections.push({ version: current.version, content: current.lines.join("\\n").trim() });
1038+
}
1039+
1040+
if (sections.length === 0) {
1041+
return undefined;
1042+
}
1043+
1044+
const endVersion = options.end ?? sections[0].version;
1045+
const startVersion = options.start;
1046+
1047+
const compareSemver = (a: string, b: string): number => {
1048+
const pa = a.replace(/^v/, "").split(".").map(Number);
1049+
const pb = b.replace(/^v/, "").split(".").map(Number);
1050+
for (let i = 0; i < 3; i++) {
1051+
if ((pa[i] ?? 0) !== (pb[i] ?? 0)) {
1052+
return (pa[i] ?? 0) - (pb[i] ?? 0);
1053+
}
1054+
}
1055+
return 0;
1056+
};
1057+
1058+
const filtered = sections.filter(s => {
1059+
const cmpEnd = compareSemver(s.version, endVersion);
1060+
if (cmpEnd > 0) return false;
1061+
if (startVersion) {
1062+
const cmpStart = compareSemver(s.version, startVersion);
1063+
if (cmpStart < 0) return false;
1064+
}
1065+
return true;
1066+
});
1067+
1068+
if (filtered.length > 0) {
1069+
return filtered.map(s => s.content).join("\\n\\n");
1070+
}
1071+
}
1072+
} catch {
1073+
// Ignore errors and return undefined
1074+
}
1075+
1076+
return undefined; `}
1077+
</FunctionDeclaration>
1078+
</>
1079+
);
1080+
}
1081+
9621082
export interface UpgradeBuiltinProps extends Omit<
9631083
BuiltinFileProps,
9641084
"id" | "description"
@@ -985,7 +1105,7 @@ export function UpgradeBuiltin(props: UpgradeBuiltinProps) {
9851105
builtinImports={defu(rest.builtinImports ?? {}, {
9861106
console: ["error", "verbose", "writeLine"],
9871107
env: ["paths", "isWindows", "isCI", "env"],
988-
utils: ["isColorSupported", "isInteractive", "spawn"]
1108+
utils: ["isColorSupported", "isInteractive", "spawn", "resolveModule"]
9891109
})}>
9901110
<VarDeclaration
9911111
const

0 commit comments

Comments
 (0)