783
*/
784
export function interpolateHookPluginRoot(
786
>
json: unknown,
787
>
pluginUri: URI,
788
>
workspaceRoot: URI | undefined,
789
>
userHome: URI,
790
>
token: string,
791
>
envVar: string,
792
>
): IParsedHookGroup[] {
793
>
const fsPath = pluginUri.fsPath;
794
>
const typedJson = json as { hooks?: Record<string, unknown[]> };
795
>
796
>
const mutateHookCommand = (hook: Record<string, unknown>): void => {
797
>
for (const field of ['command', 'windows', 'linux', 'osx'] as const) {
798
>
if (typeof hook[field] === 'string') {
799
>
hook[field] = shellQuotePluginRootInCommand(hook[field] as string, fsPath, token);
800
>
}
801
>
}
802
>
803
>
if (!hook.env || typeof hook.env !== 'object') {
804
>
hook.env = {};
805
>
}
806
>
(hook.env as Record<string, string>)[envVar] = fsPath;
807
>
};
808
>
809
>
for (const lifecycle of Object.values(typedJson.hooks ?? {})) {
810
>
if (!Array.isArray(lifecycle)) {
811
continue;
812
}
814
>
if (!lifecycleEntry || typeof lifecycleEntry !== 'object') {
815
continue;
816
}
817
>
const entry = lifecycleEntry as { hooks?: Record<string, unknown>[] } & Record<string, unknown>;
pluginParsers.ts
818
>
if (Array.isArray(entry.hooks)) {
819
>
for (const hook of entry.hooks) {
820
>
mutateHookCommand(hook);
821
>
}
822
>
} else {
823
mutateHookCommand(entry);
824
}
826
>
}
827
>
828
>
const replacer = (v: unknown): unknown => {
829
>
return typeof v === 'string'
830
>
? v.replaceAll(token, pluginUri.fsPath)
831
>
: undefined;
832
>
};
833
>
834
>
return parseHooksJson(hookUri, cloneAndChange(json, replacer), workspaceRoot, userHome);
835
>
}
836
837
// ---------------------------------------------------------------------------