Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
refactor: self describing recorder in configuration-accessor
Browse files Browse the repository at this point in the history
It is less error-prone to add name and recursion on each recorder
instead of demanding `components/configuratution-accessor/index.mjs` to
assign this information.
  • Loading branch information
lachrist committed Dec 7, 2022
1 parent 2c48710 commit 979b781
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 62 deletions.
60 changes: 32 additions & 28 deletions components/configuration-accessor/default/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ import { matchSpecifier } from "../../specifier/index.mjs";
import { extendConfiguration } from "../../configuration/index.mjs";
import { resolveShell } from "./escape.mjs";
import * as MochaRecorder from "./mocha.mjs";
import * as JestRecorder from "./jest.mjs";
import * as ProcessRecorder from "./process.mjs";
import * as ProcessRecorderRecursive from "./process-recursive.mjs";
import * as RemoteRecorder from "./remote.mjs";
import * as RemoteRecorderRecursive from "./remote-recursive.mjs";

const { doesSupportTokens: isMochaTokens, doesSupportSource: isMochaSource } =
MochaRecorder;
export const recorders = [
MochaRecorder,
JestRecorder,
ProcessRecorder,
ProcessRecorderRecursive,
RemoteRecorder,
RemoteRecorderRecursive,
];

const {
String,
RegExp,
Object: { entries: toEntries },
JSON: { stringify: stringifyJSON },
Expand Down Expand Up @@ -74,16 +80,25 @@ export const resolveConfigurationAutomatedRecorder = (configuration) => {
"cannot resolve recorder because command is missing",
InternalAppmapError,
);
const method =
configuration.command.tokens === null
? "doesSupportSource"
: "doesSupportTokens";
const input =
configuration.command.tokens === null
? configuration.command.source
: configuration.command.tokens;
const { name } = recorders.find(
(recorder) =>
(recorder.recursive === null ||
recorder.recursive ===
configuration["recursive-process-recording"]) &&
recorder[method](input),
);
configuration = extendConfiguration(
configuration,
{
recorder: (
configuration.command.tokens === null
? isMochaSource(configuration.command.source)
: isMochaTokens(configuration.command.tokens)
)
? "mocha"
: "process",
recorder: name,
},
configuration.repository.directory,
);
Expand Down Expand Up @@ -191,21 +206,6 @@ export const getConfigurationScenarios = (configuration) => {
);
};

const recorders = {
process: {
true: ProcessRecorderRecursive,
false: ProcessRecorder,
},
remote: {
true: RemoteRecorderRecursive,
false: RemoteRecorder,
},
mocha: {
true: MochaRecorder,
false: MochaRecorder,
},
};

export const compileConfigurationCommand = (configuration, env) => {
assert(
configuration.agent !== null,
Expand All @@ -219,8 +219,6 @@ export const compileConfigurationCommand = (configuration, env) => {
);
const {
agent: { directory },
recorder,
"recursive-process-recording": recursive,
command: { source, tokens },
"command-options": options,
} = configuration;
Expand All @@ -230,7 +228,13 @@ export const compileConfigurationCommand = (configuration, env) => {
APPMAP_CONFIGURATION: stringifyJSON(configuration),
};
const { hookCommandSource, hookCommandTokens, hookEnvironment } =
recorders[recorder][String(recursive)];
recorders.find(
(recorder) =>
(recorder.recursive === null ||
recorder.recursive ===
configuration["recursive-process-recording"]) &&
recorder.name === configuration.recorder,
);
const [exec, ...argv] =
tokens === null
? hookCommandSource(source, resolveShell(options.shell, env), directory)
Expand Down
3 changes: 3 additions & 0 deletions components/configuration-accessor/default/mocha.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { generateParseSource, generateSplitTokens } from "./package.mjs";
const { canParseSource, parseSource } = generateParseSource("mocha");
const { canSplitTokens, splitTokens } = generateSplitTokens("mocha");

export const name = "mocha";
export const recursive = null;

export const doesSupportSource = canParseSource;

export const doesSupportTokens = canSplitTokens;
Expand Down
10 changes: 7 additions & 3 deletions components/configuration-accessor/default/node-recursive.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { toAbsoluteUrl } from "../../url/index.mjs";
import { convertFileUrlToPath } from "../../path/index.mjs";
import { escapeNodeOption } from "./escape.mjs";

export const doesSupportSource = constant(true);
const doesSupportSource = constant(true);

export const doesSupportTokens = constant(true);
const doesSupportTokens = constant(true);

export const generateNodeHook = (recorder) => ({
export const generateNodeRecorder = (recorder) => ({
name: recorder,
recursive: true,
doesSupportSource,
doesSupportTokens,
hookCommandSource: (source, _shell, _base) => [source],
hookCommandTokens: (tokens, _base) => tokens,
hookEnvironment: (env, base) => ({
Expand Down
19 changes: 13 additions & 6 deletions components/configuration-accessor/default/node-recursive.test.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { fileURLToPath } from "node:url";
import { assertEqual, assertDeepEqual } from "../../__fixture__.mjs";
import {
import { generateNodeRecorder } from "./node-recursive.mjs";

const {
name,
recursive,
doesSupportSource,
doesSupportTokens,
generateNodeHook,
} from "./node-recursive.mjs";

const { hookCommandSource, hookCommandTokens, hookEnvironment } =
generateNodeHook("process");
hookCommandSource,
hookCommandTokens,
hookEnvironment,
} = generateNodeRecorder("process");

const base = "file:///A:/base/";
const path = fileURLToPath("file:///A:/base/lib/node/recorder-process.mjs");

assertEqual(name, "process");

assertEqual(recursive, true);

assertEqual(doesSupportSource("source"), true);

assertDeepEqual(hookCommandSource("source", "/bin/sh", base), ["source"]);
Expand Down
10 changes: 7 additions & 3 deletions components/configuration-accessor/default/node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { escapeShell } from "./escape.mjs";

const regexp = /^(?<before>\s*\S*node(.[a-zA-Z]+)?)(?<after>($|\s[\s\S]*$))$/u;

export const doesSupportSource = (command) => regexp.test(command);
const doesSupportSource = (command) => regexp.test(command);

export const doesSupportTokens = (tokens) =>
const doesSupportTokens = (tokens) =>
tokens.length > 0 && tokens[0].startsWith("node");

const splitNodeCommand = (tokens) => {
Expand Down Expand Up @@ -42,7 +42,11 @@ const parseNodeCommand = (source) => {
return result.groups;
};

export const generateNodeHook = (recorder) => ({
export const generateNodeRecorder = (recorder) => ({
doesSupportSource,
doesSupportTokens,
recursive: false,
name: recorder,
hookCommandSource: (source, shell, base) => {
const groups = parseNodeCommand(source);
return [
Expand Down
18 changes: 12 additions & 6 deletions components/configuration-accessor/default/node.test.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { fileURLToPath } from "node:url";
import { assertEqual, assertDeepEqual } from "../../__fixture__.mjs";
import {
import { generateNodeRecorder } from "./node.mjs";

const {
name,
recursive,
doesSupportSource,
doesSupportTokens,
generateNodeHook,
} from "./node.mjs";

const { hookCommandSource, hookCommandTokens, hookEnvironment } =
generateNodeHook("process");
hookCommandSource,
hookCommandTokens,
hookEnvironment,
} = generateNodeRecorder("process");

const base = "file:///A:/base/";
const recorder_path = fileURLToPath(
"file:///A:/base/lib/node/recorder-process.mjs",
);

assertEqual(name, "process");
assertEqual(recursive, false);

assertEqual(doesSupportSource("node.ext main.mjs"), true);
assertDeepEqual(hookCommandSource("node.ext main.mjs", "/bin/sh", base), [
`node.ext --experimental-loader ${recorder_path} main.mjs`,
Expand Down
14 changes: 10 additions & 4 deletions components/configuration-accessor/default/process-recursive.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { generateNodeHook } from "./node-recursive.mjs";
export { doesSupportSource, doesSupportTokens } from "./node-recursive.mjs";
export const { hookCommandSource, hookCommandTokens, hookEnvironment } =
generateNodeHook("process");
import { generateNodeRecorder } from "./node-recursive.mjs";
export const {
name,
recursive,
doesSupportSource,
doesSupportTokens,
hookCommandSource,
hookCommandTokens,
hookEnvironment,
} = generateNodeRecorder("process");
14 changes: 10 additions & 4 deletions components/configuration-accessor/default/process.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { generateNodeHook } from "./node.mjs";
export { doesSupportSource, doesSupportTokens } from "./node.mjs";
export const { hookCommandSource, hookCommandTokens, hookEnvironment } =
generateNodeHook("process");
import { generateNodeRecorder } from "./node.mjs";
export const {
name,
recursive,
doesSupportSource,
doesSupportTokens,
hookCommandSource,
hookCommandTokens,
hookEnvironment,
} = generateNodeRecorder("process");
14 changes: 10 additions & 4 deletions components/configuration-accessor/default/remote-recursive.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { generateNodeHook } from "./node-recursive.mjs";
export { doesSupportSource, doesSupportTokens } from "./node-recursive.mjs";
export const { hookCommandSource, hookCommandTokens, hookEnvironment } =
generateNodeHook("remote");
import { generateNodeRecorder } from "./node-recursive.mjs";
export const {
name,
recursive,
doesSupportSource,
doesSupportTokens,
hookCommandSource,
hookCommandTokens,
hookEnvironment,
} = generateNodeRecorder("remote");
14 changes: 10 additions & 4 deletions components/configuration-accessor/default/remote.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { generateNodeHook } from "./node.mjs";
export { doesSupportSource, doesSupportTokens } from "./node.mjs";
export const { hookCommandSource, hookCommandTokens, hookEnvironment } =
generateNodeHook("remote");
import { generateNodeRecorder } from "./node.mjs";
export const {
name,
recursive,
doesSupportSource,
doesSupportTokens,
hookCommandSource,
hookCommandTokens,
hookEnvironment,
} = generateNodeRecorder("remote");

0 comments on commit 979b781

Please sign in to comment.