-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeps-helpers.ts
152 lines (146 loc) · 4.65 KB
/
deps-helpers.ts
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
146
147
148
149
150
151
152
import * as d from "./fs/discover.ts";
import * as gh from "./task/github.ts";
import * as dt from "./task/doctor.ts";
export async function srcDepsMutator<OrgRepo extends string>(
args:
& {
orgRepo: OrgRepo; // only "org/name" like "netspective-labs/aide"
srcRelPathSupplier: (src: Required<d.DiscoverPathResult>) => string;
onSrcNotFound?: {
gitHub?: {
prepareSandbox: (
d: d.DiscoverPathResult,
depsTs: string,
) => Promise<void>;
prepareRemote: (
d: d.DiscoverPathResult,
depsTs: string,
) => Promise<void>;
};
};
}
& ({
src: Required<d.DiscoverPathResult>;
} | {
discoverSrcPath?: string;
srcDiscoveryStartPath: string;
}),
) {
const src = "src" in args ? args.src : await d.discoverGlob(
`**/${args.discoverSrcPath ?? args.orgRepo}`,
args.srcDiscoveryStartPath,
);
const srcRelPath = src.found
? args.srcRelPathSupplier(src as Required<d.DiscoverPathResult>)
: undefined;
const result = {
args,
src,
srcRelPath,
isSandbox: (depsTs: string) => {
// Test to see if any of the imports in deps.ts contains relative paths
// URIs such as ../netspective-labs/aide/ or ../github.com/netspective-labs/aide/.
// If so, it means that the deps.ts refers to "sandbox" or "local"
// Resource Factory modules.
const origDepsTs = Deno.readTextFileSync(depsTs);
const relRegExp = new RegExp(`["'].*?\\.\\.\\/.*?${args.orgRepo}\\/`);
return relRegExp.test(origDepsTs);
},
doctor: (label: string, depsTs: string) => {
// deno-lint-ignore require-await
return async (report: dt.DoctorReporter) => {
report({
test: () => !result.isSandbox(depsTs),
pass: `${depsTs} using remote ${label} URLs`,
fail: `${depsTs} using sandbox ${label} files`,
});
};
},
gitHub: {
remoteTag: async (defaultTag = "main") => {
return await gh.latestGitHubRepoTag(
{ repo: args.orgRepo },
defaultTag,
);
},
remoteRegExp: new RegExp(
`https:\/\/raw.githubusercontent.com\/${args.orgRepo}\/.*?\/`,
"g",
),
prepareSandbox: async (
depsTs: string,
options?: { onNoMutations: (lookedFor: RegExp) => string },
) => {
if (!src.found) {
args.onSrcNotFound?.gitHub?.prepareSandbox(src, depsTs);
return;
}
const origDepsTs = Deno.readTextFileSync(depsTs);
const mutatedDepsTs = origDepsTs.replaceAll(
result.gitHub.remoteRegExp,
`${srcRelPath}/`,
);
if (mutatedDepsTs != origDepsTs) {
await Deno.writeTextFile(depsTs, mutatedDepsTs);
} else {
options?.onNoMutations?.(result.gitHub.remoteRegExp);
}
},
prepareRemote: async (
depsTs: string,
options?: { onNoMutations: (lookedFor: string) => string },
) => {
if (!src.found) {
args.onSrcNotFound?.gitHub?.prepareRemote(src, depsTs);
return;
}
const origDepsTs = Deno.readTextFileSync(depsTs);
const lookFor = `"${srcRelPath}/`;
const mutatedDepsTs = origDepsTs.replaceAll(
lookFor,
`"https://raw.githubusercontent.com/${args.orgRepo}/${await result
.gitHub.remoteTag()}/`,
);
if (mutatedDepsTs != origDepsTs) {
await Deno.writeTextFile(depsTs, mutatedDepsTs);
} else {
options?.onNoMutations?.(lookFor);
}
},
},
};
return result;
}
/**
* Instead of using multiple import maps, mutate the local deps.ts to point to
* an appropriate set of https://github.com/netspective-labs/aide modules.
* When local (mGit path conventions): ../../../github.com/netspective-labs/aide*
* when remote (latest): https://raw.githubusercontent.com/netspective-labs/aide/main*
* when remote (pinned): https://raw.githubusercontent.com/netspective-labs/aide/${tag}*
*/
export async function nlAideDepsMutator(
srcDiscoveryStartPath: string,
srcRelPathSupplier: (src: Required<d.DiscoverPathResult>) => string,
options?: {
onSrcNotFound?: {
gitHub?: {
prepareSandbox: (
d: d.DiscoverPathResult,
depsTs: string,
) => Promise<void>;
prepareRemote: (
d: d.DiscoverPathResult,
depsTs: string,
) => Promise<void>;
};
};
},
) {
const mutator = await srcDepsMutator({
orgRepo: "netspective-labs/aide",
srcDiscoveryStartPath,
srcRelPathSupplier,
onSrcNotFound: options?.onSrcNotFound,
});
return mutator;
}