diff --git a/bin/postpublish.js b/bin/postpublish.js new file mode 100644 index 0000000..b027a24 --- /dev/null +++ b/bin/postpublish.js @@ -0,0 +1,9 @@ +const { removeSync, pathExistsSync, moveSync } = require('fs-extra') + +if (pathExistsSync('./package.json')) { + removeSync('./package.json') +} + +if (pathExistsSync('./package.backup.json')) { + moveSync('./package.backup.json', './package.json') +} diff --git a/bin/prepublish.js b/bin/prepublish.js new file mode 100644 index 0000000..4dc6dae --- /dev/null +++ b/bin/prepublish.js @@ -0,0 +1,36 @@ +const { writeFileSync, copySync, pathExistsSync } = require('fs-extra') +const yargs = require('yargs/yargs') +const { hideBin } = require('yargs/helpers') +const argv = yargs(hideBin(process.argv)).argv + +let pkg = require('../package.json') + +const development = argv.dev +const production = argv.prod + +if (!development && !production) { + throw new Error('Unknown environment.') +} + +let version = '0.1.' + ~~(Date.now() / 1000) +let name = '@aaron-dev/torchlight-cli' + +if (production) { + // Populated by GitHub actions + version = process.env.PACKAGE_VERSION + name = '@torchlight-api/torchlight-cli' +} + +if (pathExistsSync('./package.backup.json')) { + throw new Error('package.backup.json already exists, not overwriting.') +} + +copySync('./package.json', './package.backup.json') + +pkg = { + ...pkg, + name, + version: version +} + +writeFileSync('./package.json', JSON.stringify(pkg, null, 2)) diff --git a/bin/release b/bin/release new file mode 100755 index 0000000..7961fe5 --- /dev/null +++ b/bin/release @@ -0,0 +1,15 @@ +#!/bin/sh + +set -e + +if [ "$1" != "dev" ] && [ "$1" != "prod" ]; then + echo "The first argument must be 'dev' or 'prod'." + exit +fi + +set -x + +yarn run build +node ./bin/prepublish.js --"$1" +npm publish --access public +node ./bin/postpublish.js --"$1" \ No newline at end of file diff --git a/index.js b/index.js index 7aeb2f9..ca451a0 100755 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ -import torchlight from './src/torchlight' -import Block from './src/block' +import torchlight from './src/torchlight.js' +import Block from './src/block.js' export { torchlight, diff --git a/package.json b/package.json index 3777846..5ec659e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "@torchlight-api/torchlight-cli", "version": "0.1.4", "description": "A CLI for Torchlight - the syntax highlighting API", - "main": "index.js", + "type": "module", + "exports": "./index.js", "scripts": { "test": "standard --env jest && jest", "build": "babel src --out-dir lib --copy-files \"--ignore\" \"src/stubs/**/*\"" diff --git a/src/bin/torchlight.js b/src/bin/torchlight.js index 10ac0e5..2359fe6 100755 --- a/src/bin/torchlight.js +++ b/src/bin/torchlight.js @@ -1,4 +1,4 @@ #! /usr/bin/env node -import { makeProgram } from '../cli' +import { makeProgram } from '../cli.js' makeProgram().parse() diff --git a/src/block.js b/src/block.js index 9e3e8d4..7ba4497 100644 --- a/src/block.js +++ b/src/block.js @@ -1,6 +1,6 @@ import md5 from 'md5' -import guid from './support/guid' -import torchlight from './torchlight' +import guid from './support/guid.js' +import torchlight from './torchlight.js' export default function Block (opts = {}) { opts = { diff --git a/src/cli.js b/src/cli.js index 085e826..886c171 100644 --- a/src/cli.js +++ b/src/cli.js @@ -1,9 +1,9 @@ import { program } from 'commander' -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' +import torchlight from './torchlight.js' +import highlight from './commands/highlight.js' +import init from './commands/init.js' +import cacheClear from './commands/cache/clear.js' +import { makeConfig, makeCache } from './config.js' /** * Configure the commander CLI application. diff --git a/src/commands/highlight.js b/src/commands/highlight.js index ae62cc6..d9036c3 100644 --- a/src/commands/highlight.js +++ b/src/commands/highlight.js @@ -1,11 +1,11 @@ import path from 'path' -import torchlight from '../torchlight' -import Block from '../block' +import torchlight from '../torchlight.js' +import Block from '../block.js' import cheerio from 'cheerio' import chokidar from 'chokidar' -import log from '../support/log' +import log from '../support/log.js' import fs from 'fs-extra' -import { bus, FILE_WATCHING_COMPLETE } from '../support/bus' +import { bus, FILE_WATCHING_COMPLETE } from '../support/bus.js' export default function (torchlight, options) { options = { diff --git a/src/commands/init.js b/src/commands/init.js index 1e188c8..ce36a3c 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -1,7 +1,7 @@ import fs from 'fs-extra' import path from 'path' import inquirer from 'inquirer' -import log from '../support/log' +import log from '../support/log.js' function write (location) { const source = path.resolve(path.join(__dirname, '../stubs/config.js')) diff --git a/src/config.js b/src/config.js index 044581d..76cfbeb 100644 --- a/src/config.js +++ b/src/config.js @@ -1,7 +1,7 @@ import fs from 'fs-extra' import path from 'path' -import FileCache from './cache/file' -import MemoryCache from './cache/memory' +import FileCache from './cache/file.js' +import MemoryCache from './cache/memory.js' /** * @param {string|object} config diff --git a/src/torchlight.js b/src/torchlight.js index 913d046..0a9a443 100644 --- a/src/torchlight.js +++ b/src/torchlight.js @@ -2,8 +2,8 @@ import axios from 'axios' import md5 from 'md5' import get from 'lodash.get' import chunk from 'lodash.chunk' -import log from './support/log' -import MemoryCache from './cache/memory' +import log from './support/log.js' +import MemoryCache from './cache/memory.js' /** * @constructor diff --git a/tests/highlight.test.js b/tests/highlight.test.js index d4952de..966196d 100644 --- a/tests/highlight.test.js +++ b/tests/highlight.test.js @@ -1,6 +1,6 @@ -import torchlight from '../src/torchlight'; -import {mockApi, fixture} from './support/helpers'; -import log from '../src/support/log'; +import torchlight from '../src/torchlight.js'; +import {mockApi, fixture} from './support/helpers.js'; +import log from '../src/support/log.js'; process.env.TORCHLIGHT_TOKEN = 'test' diff --git a/tests/support/helpers.js b/tests/support/helpers.js index 2bf6bd2..2de5bed 100644 --- a/tests/support/helpers.js +++ b/tests/support/helpers.js @@ -1,7 +1,7 @@ -import { bus, FILE_WATCHING_COMPLETE } from '../../src/support/bus' -import { testCli } from '../../src/cli' import { readFileSync } from 'fs-extra' -import torchlight from '../../src/torchlight' +import { bus, FILE_WATCHING_COMPLETE } from '../../src/support/bus.js' +import { testCli } from '../../src/cli.js' +import torchlight from '../../src/torchlight.js' function fixture (file, callback, options = {}) { options = {