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

Commit

Permalink
fix: lint lib
Browse files Browse the repository at this point in the history
  • Loading branch information
lachrist committed Apr 3, 2021
1 parent 4361823 commit 3b6a05b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 80 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
root: true
extends: [airbnb-base, prettier]
rules:
lines-between-class-members: "off"
lines-between-class-members: off
no-param-reassign: off
spaced-comment: off
max-classes-per-file: off
no-unused-vars:
Expand Down
54 changes: 30 additions & 24 deletions lib/server/appmap.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import * as Path from 'path';
import { writeFileSync } from 'fs';
import { readFileSync, writeFileSync } from 'fs';
import { fileURLtoPath } from 'url';
import logger from './logger.mjs';
import Namespace from "./namespace.mjs";
import git from "./git.mjs";
import File from "./file.mjs";
import instrument from "./instrument/index.mjs";

import Logger from './logger.mjs';

const APPMAP_VERSION = '1.4';

const client = FileSystem.readFileSync(
const client = readFileSync(
Path.join(
Path.dirname(fileURLtoPath(import.meta)),
'..',
Expand All @@ -18,39 +22,39 @@ const client = FileSystem.readFileSync(

export default (class Appmap {
constructor (recorder, conf, init) {
this.state = null
},
this.state = null;
}
initialize (recorder, config, init) {
if (this.state !== null) {
logger.error("Cannot initialize the appmap: ignoring the initialization >> the appmap is not idle");
logger.error("Appmap cannot be initialized because it is not idle");
} else {
this.state = {
config: config.extendWithEnv(init.env),
namespace: new Namespace(config.getEscapePrefix());
namespace: new Namespace(config.getEscapePrefix()),
appmap: {
version: APPMAP_VERSION,
metadata: {
name: conf.getMapName(),
name: config.getMapName(),
labels: init.labels,
app: conf.getAppName(),
app: config.getAppName(),
feature: init.feature,
feature_group: init.feature_group,
language: {
name: 'javascript',
engine: init.engine,
version: conf.getLanguageVersion(),
version: config.getLanguageVersion(),
},
frameworks: init.frameworks,
client: {
name:
url:
version:
}
name: client.name,
url: client.repository.url,
version: client.version
},
recorder: {
name: recorder,
},
recording: init.recording,
git: git(conf.getGitDir()),
git: git(config.getGitDir()),
},
classMap: [],
events: []
Expand All @@ -60,39 +64,41 @@ export default (class Appmap {
}
instrument (source, path, content) {
if (this.state === null) {
logger.error("Cannot use appmap to instrument: returning the original code >> the appmap is idle");
logger.error("Appmap cannot instrument code because it is idle");
return content;
}
logger.info("Instrument %s %s", source, path);
logger.info("Appmap instrument %s at %s", source, path);
return instrument(
new File(this.state.config.getLanguageVersion(), source, path, content),
this.namespace,
(entity) => {
logger.info("Receive code entity: %j", entity);
logger.info("Appmap register code entity: %j", entity);
this.state.appmap.classMap.push(entity);
}
);
}
emit (event) {
if (this.state === null) {
logger.error("Cannot emit event to appmap: ignoring the event >> the appmap is idle")
logger.error("Appmap cannot register event because it is idle; ignoring the event")
} else {
logger.info("Receive event: %j", event);
logger.info("Appmap save event: %j", event);
this.state.appmap.event.push(event);
}
}
terminate (reason) {
if (this.state === null) {
logger.error("Cannot terminate appmap: ignoring the termination >> the appmap is idle")
logger.error("Appmap cannot be terminated because it is idle");
} else {
logger.info("Terminate with: %j", reason);
logger.info("Appmap terminate with: %j", reason);
const path = Path.join(this.state.config.getOutputDir(), `${this.state.config.getMapName()}.appmap.json`);
const content = JSON.stringify(this.state.appmap);
try {
writeFileSync(path, content, "utf8");
} catch (error) {
logger.error()
logger.error("Appmap cannot be saved at %s because %s; outputing to stdout instead", path, error.message);
process.stdout.write(content, "utf8");
process.stdout.write("\n", "utf8");
}
this.state.appmap.event.push(event);
}
}
});
89 changes: 48 additions & 41 deletions lib/server/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ const identity = (any) => any;

const makeEscapePrefixSanitizer = (location) => (string) => {
if (!/^[a-zA-Z_$][a-zA-Z_$0-9]+$/.test(string)) {
logger.warning("Invalid %s, defaulting to %s >> it should match /^[a-zA-Z_$][a-zA-Z_$0-9]+$/ and got: %s", location, DEFAULT_ESCAPE_PREFIX, string);
logger.warning("Invalid %s, defaulting to %s >> expected matching /^[a-zA-Z_$][a-zA-Z_$0-9]+$/ and got: %s", location, DEFAULT_ESCAPE_PREFIX, string);
return DEFAULT_ESCAPE_PREFIX;
}
return string;
};

const language_version_array = [
const ecmas = [
"5",
"5.1",
"2015",
Expand All @@ -45,33 +45,35 @@ const language_version_array = [
];

const makeLanguageVersionSanitizer = (location) => (string) => {
if (!language_version_array.includes(string)) {
logger.warning("Invalid %s env argument, defaulting to %s >> it should be one of %j and got: %s", language_version_array, DEFAULT_LANGUAGE_VERSION, string);
if (!ecmas.includes(string)) {
logger.warning("Invalid %s env argument, defaulting to %s >> expected one of %j and got: %s", ecmas, DEFAULT_LANGUAGE_VERSION, string);
return DEFAULT_LANGUAGE_VERSION;
}
return string;
};

const makeTypeSanitizer = (type, location, json1) => (json2) => {
/* eslint-disable valid-typeof */
if (typeof json2 !== type) {
logger.warning("Invalid %s, defaulting to %j >> it should be a %s and got: %j", location, json1, type, json2);
logger.warning("Invalid %s, defaulting to %j >> expected a %s and got: %j", location, json1, type, json2);
return json1;
}
/* eslint-enable valid-typeof */
return json2
};

const makeBooleanStringSanitizer = (location, boolean) => (string) => {
string = string.toLowerCase();
if (string !== "true" && string !== "false") {
logger.warning("Invalid %s, defaulting to %b. It should be either 'true' or 'false' (case insensitive) and got: %s", location, boolean string);
logger.warning("Invalid %s, defaulting to %b >> expected 'true' or 'false' (case insensitive) and got: %s", location, boolean, string);
return boolean;
}
return string === "true";
};

const makeArraySanitizer = (location, sanitizer) => (json) => {
if (!Array.isArray(json)) {
logger.warning("Invalid %s, defaulting to []. It should be an array and got: %j", location, json);
logger.warning("Invalid %s, defaulting to [] >> expected an array and got: %j", location, json);
return [];
}
return json.map(sanitizer);
Expand Down Expand Up @@ -110,7 +112,7 @@ const mappings = {
APPMAP_GIT_DIR: {
name: 'git_dir',
sanitize: identity
}
},
APPMAP_EXCLUDE: {
name: 'exclusions',
sanitize: (string) => string.split(",").map(trim)
Expand Down Expand Up @@ -160,18 +162,18 @@ const mappings = {
}
if (typeof json === "object" && json !== null) {
if (Reflect.getOwnPropertyDescriptor(json, "name") === undefined) {
logger.warning(`Invalid packages[%i] value >> missing name field and got %s`, index, json);
logger.warning("Invalid packages[%i] value >> missing name field and got %s", index, json);
return null
}
if (typeof json.name !== "string") {
logger.warning(`Invalid packages[%i].name value >> expected a string and got %s`, index, json.name);
logger.warning("Invalid packages[%i].name value >> expected a string and got %s", index, json.name);
return null;
}
return {
name: json.name
};
}
logger.warning(`Invalid packages[%i] value >> expected either a string or an object and got %s`, index, json);
logger.warning("Invalid packages[%i] value >> expected either a string or an object and got %s", index, json);
return null;
}
)
Expand All @@ -183,7 +185,7 @@ const mappings = {
"exclude conf value",
(json, index) => {
if (typeof json !== "string") {
logger.warning(`Invalid exclude[%i] conf value >> expected a string and got %j`, index, json);
logger.warning("Invalid exclude[%i] conf value >> expected a string and got %j", index, json);
return null;
}
return json;
Expand Down Expand Up @@ -218,61 +220,65 @@ const mergers = {

const extend = (mapping, conf, object) => {
conf = {...conf};
for (key in object) {
if (key in mappings[kind]) {
/* eslint-disable no-restricted-syntax */
for (const key in mapping) {
if (key in object) {
const {name, sanitize} = mapping(key);
conf[name] = mergers[name](conf[name], sanitize(object[key]));
}
}
/* eslint-enable no-restricted-syntax */
return conf
};

const extendWithJson = (conf, json) => {
if (json === null || typeof json !== "object") {
logger.warning(`Invalid top-level format >> expected an object and got: %j`, json);
return conf;
}
if (Reflect.getOwnPropertyDescriptor(json, "extend") !== undefined) {
if (typeof json.extend !== "string") {
logger.warning(`Invalid extend value, ignored >> expected a string and got: %j`, json.extend);
} else {
conf = extendWithPath(conf, json.extend);
}
}
return extend(mapping.json, conf, json);
};

const extendWithEnv = (conf, env) => {
if (Reflect.getOwnPropertyDescriptor(env, "APPMAP_CONFIG") !== undefined) {
conf = extendWithPath(conf, env.APPMAP_CONFIG);
}
extend(mapping.env, conf, env);
};

const extendWithPath = (conf, path) => {
let parse;
if (path.endsWith(".json")) {
parse = JSON.parse;
} else if (path.endsWith(".yml") {
} else if (path.endsWith(".yml")) {
parse = parseYaml;
} else {
logger.warning(`Invalid conf file extension expected '.yml' or '.json', got: %s`, conf);
logger.warning("Invalid conf file extension >> expected '.yml' or '.json', got: %s", conf);
return this;
}
let content;
try {
content = readFileSync(path, "utf8");
} catch (error) {
logger.warning(`Failed to read conf file %s, reason: %s`, path, error.message);
logger.warning("Failed to read conf file at %s >> %s", path, error.message);
return this;
}
let json;
try {
json = parse(content);
} catch (error) {
logger.warning(`Failed to parse conf file, reason: %s`, error.message);
logger.warning("Failed to parse conf file >> %s", error.message);
}
/* eslint-disable no-use-before-define */
return extendWithJson(conf, json);
/* eslint-enable no-use-before-define */
};

const extendWithJson = (conf, json) => {
if (json === null || typeof json !== "object") {
logger.warning("Invalid top-level format >> expected an object and got: %j", json);
return conf;
}
if (Reflect.getOwnPropertyDescriptor(json, "extend") !== undefined) {
if (typeof json.extend !== "string") {
logger.warning("Invalid extend value >> expected a string and got: %j", json.extend);
} else {
conf = extendWithPath(conf, json.extend);
}
}
return extend(mappings.json, conf, json);
};

const extendWithEnv = (conf, env) => {
if (Reflect.getOwnPropertyDescriptor(env, "APPMAP_CONFIG") !== undefined) {
conf = extendWithPath(conf, env.APPMAP_CONFIG);
}
extend(mappings.env, conf, env);
};

////////////
Expand Down Expand Up @@ -310,11 +316,12 @@ const config = new Config({
enabled: DEFAULT_ENABLED,
app_name: DEFAULT_APP_NAME,
map_name: DEFAULT_MAP_NAME,
git_dir: DEFAULT_GIT_DIR,
language_version: DEFAULT_LANGUAGE_VERSION,
escape_prefix: DEFAULT_ESCAPE_PREFIX,
output_dir: DEFAULT_OUTPUT_DIR,
packages: [],
exclude: []
});

export const getDefaultConfig () => config;
export const getDefaultConfig = () => config;
6 changes: 3 additions & 3 deletions lib/server/inline.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

import {getDefaultConfiguration} from './config.mjs';
import {getDefaultConfig} from './config.mjs';
import Appmap from './appmap.mjs';

const RECORDER_NAME = "node-inline";

export default (env) => {
let appmap = null;
const appmap = new Appmap();
return {
initialize: (init) => {
appmap.initialize(RECORDER_NAME, getDefaultConfiguration(), init);
appmap.initialize(RECORDER_NAME, getDefaultConfig(), init);
},
terminate: (reason) => {
appmap.terminate(reason);
Expand Down
8 changes: 4 additions & 4 deletions lib/server/logger.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {debuglog} from "util";

const logger = {error:null, warning:null, info:null};

export const reload = {
logger.error = debuglog("appmap-error", (log) => { logger.error = log }),
logger.warning = debuglog("appmap-warning", (log) => { logger.warning = log }),
logger.info = debuglog("appmap-info", (log) => { logger.info = error });
export const reload = () => {
logger.error = debuglog("appmap-error", (log) => { logger.error = log });
logger.warning = debuglog("appmap-warning", (log) => { logger.warning = log });
logger.info = debuglog("appmap-info", (log) => { logger.info = log });
};

export default logger;
Loading

0 comments on commit 3b6a05b

Please sign in to comment.