From 14443b4b054ccf8ffe1c9d811ef600078f682c95 Mon Sep 17 00:00:00 2001 From: Aaron Francis Date: Thu, 26 Aug 2021 21:40:30 -0500 Subject: [PATCH] Isolate FS stuff --- package.json | 2 +- src/cli.js | 6 +++- src/config.js | 41 ++++++++++++++++++++++++++ src/torchlight.js | 63 +++++++--------------------------------- tests/support/helpers.js | 2 +- 5 files changed, 59 insertions(+), 55 deletions(-) create mode 100644 src/config.js diff --git a/package.json b/package.json index d82a66f..a15a001 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@torchlight-api/torchlight-cli", - "version": "0.1.1", + "version": "0.1.2", "description": "A CLI for Torchlight - the syntax highlighting API", "main": "index.js", "scripts": { diff --git a/src/cli.js b/src/cli.js index 5444593..085e826 100644 --- a/src/cli.js +++ b/src/cli.js @@ -3,6 +3,7 @@ import torchlight from './torchlight' import highlight from './commands/highlight' import init from './commands/init' import cacheClear from './commands/cache/clear' +import { makeConfig, makeCache } from './config' /** * Configure the commander CLI application. @@ -30,7 +31,10 @@ export function makeProgram (options = {}) { // Bootstrap the Torchlight singleton before every command. program.hook('preAction', thisCommand => { - torchlight.init(thisCommand.opts().config) + const config = makeConfig(thisCommand.opts().config) + const cache = makeCache(config) + + torchlight.init(config, cache) }) makeCommand('_default_', highlight) diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..044581d --- /dev/null +++ b/src/config.js @@ -0,0 +1,41 @@ +import fs from 'fs-extra' +import path from 'path' +import FileCache from './cache/file' +import MemoryCache from './cache/memory' + +/** + * @param {string|object} config + * @return {*} + */ +export function makeConfig (config) { + // By convention, look in the root directory for + // a torchlight.config.js file. + if (config === undefined || config === '') { + config = 'torchlight.config.js' + } + + if (typeof config === 'string') { + config = fs.pathExistsSync(path.resolve(config)) ? require(path.resolve(config)) : {} + } + + return config || {} +} + +/** + * Make a cache to hold highlighted blocks. + * + * @return {Cache} + */ +export function makeCache (config) { + const cache = config?.cache + + // Make a file cache if we're given a directory. + if (cache && typeof cache === 'string') { + return new FileCache({ + directory: cache + }) + } + + // Use the cache they have provided, or default to an in-memory cache. + return cache || new MemoryCache() +} diff --git a/src/torchlight.js b/src/torchlight.js index 28d1eb0..913d046 100644 --- a/src/torchlight.js +++ b/src/torchlight.js @@ -1,14 +1,9 @@ import axios from 'axios' -import FileCache from './cache/file' -import fs from 'fs-extra' -import MemoryCache from './cache/memory' -import path from 'path' import md5 from 'md5' import get from 'lodash.get' import chunk from 'lodash.chunk' import log from './support/log' - -const VERSION = fs.readJsonSync(path.resolve('package.json')).version +import MemoryCache from './cache/memory' /** * @constructor @@ -23,14 +18,20 @@ const Torchlight = function () { * @param config * @return {Torchlight} */ -Torchlight.prototype.init = function (config) { +Torchlight.prototype.init = function (config, cache) { if (this.initialized) { return this } + config = config || {} + + if (process?.env?.TORCHLIGHT_TOKEN && !config?.token) { + config.token = process.env.TORCHLIGHT_TOKEN + } + this.initialized = true - this.configuration = this.normalizeConfiguration(config) - this.cache = this.makeCache() + this.configuration = config + this.cache = cache || new MemoryCache() return this } @@ -55,48 +56,6 @@ Torchlight.prototype.configHash = function () { return md5(this.configuration) } -/** - * @param {string|object} config - * @return {*} - */ -Torchlight.prototype.normalizeConfiguration = function (config) { - // By convention, look in the root directory for - // a torchlight.config.js file. - if (config === undefined || config === '') { - config = 'torchlight.config.js' - } - - // Allow the developer to pass another path to us. - if (typeof config === 'string') { - config = fs.pathExistsSync(path.resolve(config)) ? require(path.resolve(config)) : {} - } - - if (process.env.TORCHLIGHT_TOKEN && !config.token) { - config.token = process.env.TORCHLIGHT_TOKEN - } - - return config -} - -/** - * Make a cache to hold highlighted blocks. - * - * @return {Cache} - */ -Torchlight.prototype.makeCache = function () { - const cache = this.config('cache', false) - - // Make a file cache if we're given a directory. - if (cache && typeof cache === 'string') { - return new FileCache({ - directory: cache - }) - } - - // Use the cache they have provided, or default to an in-memory cache. - return cache || new MemoryCache() -} - /** * @param blocks * @return {Promise<*>} @@ -172,7 +131,7 @@ Torchlight.prototype.request = function (blocks) { }, { headers: { Authorization: `Bearer ${token}`, - 'X-Torchlight-Client': `Torchlight CLI ${VERSION}` + 'X-Torchlight-Client': 'Torchlight CLI' } }).then(response => response.data.blocks) } diff --git a/tests/support/helpers.js b/tests/support/helpers.js index 4d9b6f6..2bf6bd2 100644 --- a/tests/support/helpers.js +++ b/tests/support/helpers.js @@ -38,7 +38,7 @@ function fixture (file, callback, options = {}) { }) torchlight.init() - + if (options.clearCache) { torchlight.cache.clear() }