Skip to content

Commit

Permalink
Isolate FS stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondfrancis committed Aug 27, 2021
1 parent 88c956b commit 14443b4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
6 changes: 5 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -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()
}
63 changes: 11 additions & 52 deletions src/torchlight.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
Expand All @@ -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<*>}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/support/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function fixture (file, callback, options = {}) {
})

torchlight.init()

if (options.clearCache) {
torchlight.cache.clear()
}
Expand Down

0 comments on commit 14443b4

Please sign in to comment.