Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to ESM #15

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions bin/gluegun

This file was deleted.

17 changes: 17 additions & 0 deletions bin/gluegun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node
import { dirname } from 'path'
import { fileURLToPath } from 'url'
import { run } from '../build/cli/cli.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

// speed up `gluegun --version` et al
if (['v', 'version', '-v', '--v', '-version', '--version'].includes(process.argv[2])) {
const fs = await import('fs')
const contents = fs.readFileSync(__dirname + '/../package.json')
const pkg = JSON.parse(contents)
console.log(pkg.version)
process.exit(0)
}

run(process.argv)
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"version": "0.0.4",
"description": "A delightful toolkit for building Node-powered CLIs.",
"repository": "webstoneHQ/gluegun",
"type": "module",
"bin": {
"gluegun": "bin/gluegun"
"gluegun": "bin/gluegun.js"
},
"main": "build/index.js",
"types": "build/types/index.d.ts",
Expand Down Expand Up @@ -99,6 +100,8 @@
"@typescript-eslint/eslint-plugin": "5.38.0",
"@typescript-eslint/parser": "5.38.0",
"cpy-cli": "4.2.0",
"esbuild": "^0.15.10",
"esbuild-node-externals": "^1.5.0",
"eslint": "8.24.0",
"eslint-config-prettier": "8.5.0",
"eslint-config-standard": "17.0.0",
Expand All @@ -119,6 +122,7 @@
"sinon": "14.0.0",
"strip-ansi": "7.0.1",
"temp-write": "5.0.0",
"tiny-glob": "^0.2.9",
"ts-jest": "^29.0.2",
"ts-node": "10.9.1",
"typescript": "4.8.3",
Expand Down
38 changes: 38 additions & 0 deletions scripts/esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import esbuild from 'esbuild'
import { nodeExternalsPlugin } from 'esbuild-node-externals'
import glob from 'tiny-glob'

/**
* @type {Object.<string, esbuild.BuildOptions>}
*/

const config = {
shared: {
format: 'esm',
target: 'esnext',
plugins: [nodeExternalsPlugin()],
entryPoints: await glob('src/**/*.ts'),
logLevel: 'info',
bundle: true,
minify: true,
outdir: 'build',
platform: 'node',
},
build: {},
dev: {
watch: true,
},
}

const mode = process.argv[2]
if (!mode) {
console.error('Usage: node ./scripts/esbuild.js build|dev')
process.exit(1)
}

esbuild
.build({
...config.shared,
...(config[mode] || {}),
})
.catch(() => process.exit(1))
2 changes: 1 addition & 1 deletion sniff.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (ver[0] >= 8) {

ok = hasAsyncAwait && isNewEnough

module.exports = {
export default {
nodeMinimum,
nodeVersion,
isNewEnough,
Expand Down
5 changes: 5 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { build, GluegunToolbox } from '../index'

import { dirname } from 'path'
import { fileURLToPath } from 'url'

const __dirname = dirname(fileURLToPath(import.meta.url))

/**
* Create the cli and kick it off
*/
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as path from 'path'

// first, do a sniff test to ensure our dependencies are met
const sniff = require('../sniff')
import sniff from '../sniff.js'
import AppModulePath from 'app-module-path'

import { dirname } from 'path'
import { fileURLToPath } from 'url'

const __dirname = dirname(fileURLToPath(import.meta.url))

// check the node version
if (!sniff.isNewEnough) {
Expand Down Expand Up @@ -40,5 +46,5 @@ export { GluegunMeta } from './core-extensions/meta-extension'

// this adds the node_modules path to the "search path"
// it's hacky, but it works well!
require('app-module-path').addPath(path.join(__dirname, '..', 'node_modules'))
require('app-module-path').addPath(process.cwd())
AppModulePath.addPath(path.join(__dirname, '..', 'node_modules'))
AppModulePath.addPath(process.cwd())
4 changes: 2 additions & 2 deletions src/toolbox/print-tools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as CLITable from 'cli-table3'
import * as importedColors from 'colors/safe'
import CLITable from 'cli-table3'
import importedColors from 'colors'
import { commandInfo } from './meta-tools'
import { Toolbox } from '../domain/toolbox'
import { times } from './utils'
Expand Down
2 changes: 1 addition & 1 deletion src/toolbox/print-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GluegunToolbox } from '../index'
import * as CLITable from 'cli-table3'
import * as importedColors from 'colors'
import { Toolbox } from '../domain/toolbox'
import ora = require('ora')
import ora from 'ora'

export type GluegunPrintColors = typeof importedColors & {
highlight: (t: string) => string
Expand Down
2 changes: 1 addition & 1 deletion src/toolbox/template-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function buildGenerate(toolbox: GluegunToolbox): (opts: Options) => Promise<stri
* @return The generated string.
*/
async function generate(opts: Options = {}): Promise<string> {
const ejs = require('ejs')
const ejs = await import('ejs')
// required
const template = opts.template

Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"lib": ["es2016", "es2016.array.include", "scripthost"],
"module": "commonjs",
"module": "ESNext",
"moduleResolution": "node",
"noImplicitAny": false,
"noImplicitThis": true,
Expand All @@ -14,7 +14,7 @@
"declaration": true,
"declarationDir": "build/types",
"strict": false,
"target": "ES6",
"target": "ESNext",
"pretty": true,
"skipLibCheck": true
},
Expand Down
Loading