Skip to content

Commit

Permalink
Switch everything to ECMAScript modules
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondfrancis committed Aug 26, 2021
1 parent a88f35b commit dacfa55
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 320 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
torchlight.config.js
source
built
lib
.idea
node_modules
tests/tmp
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#! /usr/bin/env node
const { makeProgram } = require('./src/cli')
import torchlight from './src/torchlight'
import Block from './src/block'

makeProgram().parse()
export {
torchlight,
Block
}
194 changes: 0 additions & 194 deletions jest.config.js

This file was deleted.

41 changes: 33 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
{
"name": "@torchlight-api/torchlight-cli",
"version": "0.0.8",
"version": "0.1.0",
"description": "A CLI for Torchlight - the syntax highlighting API",
"main": "index.js",
"scripts": {
"test": "standard --env jest && jest"
"test": "standard --env jest && jest",
"build": "babel src --out-dir lib --copy-files \"--ignore\" \"src/stubs/**/*\""
},
"bin": {
"torchlight": "index.js"
"torchlight": "lib/bin/torchlight.js"
},
"keywords": [
"syntax",
"highlighting",
"torchlight"
],
"standard": {
"ignore": [
"tests"
]
},
"author": "Aaron Francis <[email protected]> (https://torchlight.dev)",
"license": "MIT",
"dependencies": {
Expand All @@ -34,7 +30,36 @@
"md5": "^2.3.0"
},
"devDependencies": {
"@babel/preset-env": "^7.15.0",
"@babel/cli": "^7.14.8",
"@babel/core": "^7.15.0",
"@babel/node": "^7.12.13",
"babel-jest": "^27.0.6",
"jest": "^27.0.6",
"standard": "^16.0.3"
},
"standard": {
"ignore": [
"tests",
"lib"
]
},
"jest": {
"clearMocks": true,
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
}
},
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "12.13.0"
}
}
]
]
}
}
4 changes: 4 additions & 0 deletions src/bin/torchlight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /usr/bin/env node
import { makeProgram } from '../cli'

makeProgram().parse()
20 changes: 9 additions & 11 deletions src/block.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const md5 = require('md5')
const guid = require('./support/guid')
const torchlight = require('./torchlight')
import md5 from 'md5'
import guid from './support/guid'
import torchlight from './torchlight'

const Block = function (opts = {}) {
export default function Block (opts = {}) {
opts = {
id: guid(),
theme: torchlight.config('theme', 'nord'),
Expand All @@ -21,11 +21,11 @@ const Block = function (opts = {}) {

Block.prototype.hash = function () {
return md5('' +
this.language +
this.theme +
this.code +
torchlight.config('bust', 0) +
JSON.stringify(torchlight.config('options'))
this.language +
this.theme +
this.code +
torchlight.config('bust', 0) +
JSON.stringify(torchlight.config('options'))
)
}

Expand Down Expand Up @@ -74,5 +74,3 @@ Block.prototype.toRequestParams = function () {
code: this.code
}
}

module.exports = Block
28 changes: 10 additions & 18 deletions src/cache/file.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
const md5 = require('md5')
const path = require('path')
const {
readJsonSync,
writeJsonSync,
ensureDirSync,
pathExistsSync,
removeSync
} = require('fs-extra')
import md5 from 'md5'
import path from 'path'
import fs from 'fs-extra'

/**
* @param options
* @constructor
*/
const File = function (options = {}) {
export default function File (options = {}) {
if (!options.directory) {
throw new Error('No cache directory specified.')
}

this.directory = path.resolve(options.directory)

ensureDirSync(this.directory)
fs.ensureDirSync(this.directory)
}

/**
Expand All @@ -30,11 +24,11 @@ const File = function (options = {}) {
* @return {*}
*/
File.prototype.get = function (key, def) {
if (!pathExistsSync(this.filename(key))) {
if (!fs.pathExistsSync(this.filename(key))) {
return def
}

const entry = readJsonSync(this.filename(key))
const entry = fs.readJsonSync(this.filename(key))

if (Date.now() / 1000 > entry.expires) {
this.delete(key)
Expand All @@ -53,7 +47,7 @@ File.prototype.get = function (key, def) {
* @param {number} ttlSeconds
*/
File.prototype.set = function (key, value, ttlSeconds = 60 * 24 * 7) {
writeJsonSync(this.filename(key), {
fs.writeJsonSync(this.filename(key), {
expires: (Date.now() / 1000) + ttlSeconds,
value: value
})
Expand All @@ -65,14 +59,14 @@ File.prototype.set = function (key, value, ttlSeconds = 60 * 24 * 7) {
* @param key
*/
File.prototype.delete = function (key) {
removeSync(this.filename(key))
fs.removeSync(this.filename(key))
}

/**
* Clear the cache.
*/
File.prototype.clear = function () {
removeSync(this.directory)
fs.removeSync(this.directory)
}

/**
Expand All @@ -82,5 +76,3 @@ File.prototype.clear = function () {
File.prototype.filename = function (key) {
return path.join(this.directory, md5(key) + '.json')
}

module.exports = File
Loading

0 comments on commit dacfa55

Please sign in to comment.