Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 5x 6x 6x 6x 6x 6x 6x 6x 25x 25x 25x 25x 25x 36x 29x 29x 29x 25x 25x 6x 6x 25x 36x 36x 36x 1x 35x 29x 6x 6x 6x 2x 2x 1x | "use strict";
import { cloneDeep, isArray, isString, isEmpty, isFunction, defaults, isObject } from "lodash";
import { ArgdownApplication, IArgdownRequest, IArgdownResponse, ArgdownPluginError } from "@argdown/core";
import { isAsyncPlugin } from "./IAsyncArgdownPlugin";
import { IFileRequest } from "./IFileRequest";
import { IPluginRequest } from "./IPluginRequest";
import * as path from "path";
import * as chokidar from "chokidar";
import * as glob from "glob";
import { promisify } from "util";
import * as requireUncached from "require-uncached";
import { readFile } from "fs";
const readFileAsync = promisify(readFile);
export class AsyncArgdownApplication extends ArgdownApplication {
async runAsync(request: IArgdownRequest, response?: IArgdownResponse): Promise<IArgdownResponse> {
let process: string[] = [];
this.logger.setLevel("error");
let resp: IArgdownResponse = response || {};
Eif (request) {
if (request.logLevel) {
this.logger.setLevel(request.logLevel);
}
Eif (request.process) {
Eif (isArray(request.process)) {
process = request.process;
} else if (isString(request.process) && request.processes) {
process = request.processes[request.process];
}
}
}
Iif (isEmpty(process)) {
this.logger.log("error", "[AsyncArgdownApplication]: No processors to run.");
return resp;
}
const exceptions: Error[] = [];
resp.exceptions = exceptions;
for (let processorId of process) {
let cancelProcessor = false;
let processor = this.processors[processorId];
Iif (!processor) {
this.logger.log("error", "[AsyncArgdownApplication]: Processor not found: " + processorId);
continue;
}
this.logger.log("verbose", "[AsyncArgdownApplication]: Running processor: " + processorId);
for (let plugin of processor.plugins) {
if (isFunction(plugin.prepare)) {
this.logger.log("verbose", "[AsyncArgdownApplication]: Preparing plugin: " + plugin.name);
try {
plugin.prepare(request, resp, this.logger);
} catch (e) {
e.processor = processorId;
exceptions.push(e);
cancelProcessor = true;
this.logger.log("warning", `Processor ${processorId} canceled.`);
break;
}
}
}
Iif (cancelProcessor) {
break;
}
if (resp.ast && processor.walker) {
try {
processor.walker.walk(request, resp, this.logger);
} catch (e) {
e.processor = processorId;
exceptions.push(e);
this.logger.log("warning", `[ArgdownApplication]: Processor ${processorId} canceled.`);
break;
}
}
for (let plugin of processor.plugins) {
this.logger.log("verbose", "[AsyncArgdownApplication]: Running plugin: " + plugin.name);
try {
if (isAsyncPlugin(plugin)) {
await plugin.runAsync(request, resp, this.logger);
} else if (isFunction(plugin.run)) {
plugin.run(request, resp, this.logger);
}
} catch (e) {
e.processor = processorId;
this.logger.log("warning", `Processor ${processorId} canceled.`);
exceptions.push(e);
break;
}
}
}
Eif (request.logExceptions === undefined || request.logExceptions) {
for (let exception of exceptions) {
let msg = exception.stack || exception.message;
if (exception instanceof ArgdownPluginError) {
msg = `[${exception.processor}/${exception.plugin}]: ${msg}`;
}
this.logger.log("error", msg);
}
}
return resp;
}
load = async (request: IArgdownRequest): Promise<IArgdownResponse[] | undefined> => {
const fileRequest = <IFileRequest>defaults({}, request);
const inputGlob = fileRequest.inputPath || "./*.argdown";
const ignoreFiles = fileRequest.ignore || [
"**/_*", // Exclude files starting with '_'.
"**/_*/**" // Exclude entire directories starting with '_'.
];
if (request.logger && isFunction(request.logger.log) && isFunction(request.logger.setLevel)) {
if (!this.defaultLogger) {
this.defaultLogger = this.logger;
}
this.logger = request.logger;
} else if (this.defaultLogger) {
this.logger = this.defaultLogger;
}
if (!fileRequest.rootPath) {
fileRequest.rootPath = process.cwd();
}
if (request.logLevel) {
this.logger.setLevel(request.logLevel);
}
const pluginRequest = <IPluginRequest>request;
if (pluginRequest.plugins) {
for (let pluginData of pluginRequest.plugins) {
if (isObject(pluginData.plugin) && isString(pluginData.processor)) {
this.addPlugin(pluginData.plugin, pluginData.processor);
}
}
}
if (request.input && !fileRequest.inputPath) {
await this.runAsync(cloneDeep(request));
return;
}
const $ = this;
let absoluteInputGlob = path.resolve(fileRequest.rootPath, inputGlob);
const loadOptions: chokidar.WatchOptions = {};
if (ignoreFiles) {
loadOptions.ignored = ignoreFiles;
}
if (fileRequest.watch) {
const watcher = chokidar.watch(absoluteInputGlob, loadOptions);
const watcherRequest = cloneDeep(fileRequest);
watcherRequest.watch = false;
watcher
.on("add", path => {
this.logger.log("verbose", `File ${path} has been added.`);
watcherRequest.inputPath = path;
$.load(watcherRequest);
})
.on("change", path => {
this.logger.log("verbose", `File ${path} has been changed.`);
watcherRequest.inputPath = path;
$.load(watcherRequest);
})
.on("unlink", path => {
this.logger.log("verbose", `File ${path} has been removed.`);
});
} else {
let files: string[] = await new Promise<string[]>((resolve, reject) => {
glob(absoluteInputGlob, loadOptions, (er: Error | null, files: string[]) => {
if (er) {
reject(er);
}
resolve(files);
});
});
const promises = [];
for (let file of files) {
const requestForFile = cloneDeep(fileRequest);
requestForFile.inputPath = file;
promises.push(this.runAsync(requestForFile));
}
// Remove plugins added by request
if (pluginRequest.plugins) {
for (let pluginData of pluginRequest.plugins) {
this.removePlugin(pluginData.plugin, pluginData.processor);
}
}
return await Promise.all(promises);
}
return;
};
loadConfig = async (filePath: string): Promise<IArgdownRequest> => {
let config: IArgdownRequest = {};
filePath = filePath || "./argdown.config.json"; // json is default because it can be loaded asynchronously
const extension = path.extname(filePath);
// We use non-blocking IO for JSON config files
if (extension === "json") {
try {
const buffer = await readFileAsync(filePath, "utf8");
config = JSON.parse(buffer);
} catch (e) {
this.logger.log("verbose", "[AsyncArgdownApplication]: No config found: " + e.toString());
}
} else if (extension === "js") {
// For Js config files we have to used require-uncached which is synchronous
try {
let jsModuleExports = loadJSFile(filePath);
if (jsModuleExports.config) {
config = jsModuleExports.config;
} else {
// let's try the default export
config = jsModuleExports;
}
} catch (e) {
this.logger.log("verbose", "[AsyncArgdownApplication]: No config found: " + e.toString());
}
}
return config;
};
}
/**
* Taken from eslint: https://github.com/eslint/eslint/blob/master/lib/config/config-file.js
* Loads a JavaScript configuration from a file.
* @param {string} filePath The filename to load.
* @returns {Object} The configuration object from the file.
* @throws {Error} If the file cannot be read.
* @private
*/
const loadJSFile = (filePath: string) => {
let absoluteFilePath = path.resolve(process.cwd(), filePath);
try {
return requireUncached(absoluteFilePath);
} catch (e) {
e.message = `Cannot read file: ${absoluteFilePath}\nError: ${e.message}`;
throw e;
}
};
|