Skip to content

Commit

Permalink
Revert "feat: remove OCLIF_ALLOW_ESM env var (#1171)" (#1179)
Browse files Browse the repository at this point in the history
This reverts commit 3100bc8.
  • Loading branch information
mdonnalley committed Aug 31, 2023
1 parent f9ab4c4 commit 5472380
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 214 deletions.
18 changes: 18 additions & 0 deletions bin/dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node

const oclif = require('@oclif/core')

const path = require('path')
const project = path.join(__dirname, '..', 'tsconfig.json')

// In dev mode -> use ts-node and dev plugins
process.env.NODE_ENV = 'development'

require('ts-node').register({project})

// In dev mode, always show stack traces
oclif.settings.debug = true;


// Start the CLI
oclif.run().then(oclif.flush).catch(oclif.Errors.handle)
2 changes: 1 addition & 1 deletion bin/dev.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@echo off

node "%~dp0\dev" %*
node "%~dp0\dev" %*
6 changes: 0 additions & 6 deletions bin/dev.js

This file was deleted.

5 changes: 5 additions & 0 deletions bin/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

const oclif = require('@oclif/core')

oclif.run().then(require('@oclif/core/flush')).catch(require('@oclif/core/handle'))
6 changes: 0 additions & 6 deletions bin/run.js

This file was deleted.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"version": "3.13.0",
"author": "Salesforce",
"bin": {
"oclif": "bin/run.js"
"oclif": "bin/run"
},
"bugs": "https://github.com/oclif/oclif/issues",
"dependencies": {
"@oclif/core": "^3.0.0-beta.4",
"@oclif/core": "^2.11.4",
"@oclif/plugin-help": "^5.2.14",
"@oclif/plugin-not-found": "^2.3.32",
"@oclif/plugin-warn-if-update-available": "^2.0.44",
Expand Down Expand Up @@ -42,7 +42,7 @@
"@types/yeoman-generator": "^5.2.11",
"chai": "^4.3.7",
"conventional-changelog-cli": "^2.2.2",
"eslint": "^8.47.0",
"eslint": "^7.32.0",
"eslint-config-oclif": "^4.0.0",
"eslint-config-oclif-typescript": "^1.0.2",
"fancy-test": "^1.4.10",
Expand Down Expand Up @@ -124,11 +124,11 @@
"lint": "nps lint",
"postpack": "shx rm .oclif.manifest.json",
"posttest": "yarn run lint",
"prepack": "shx rm -rf lib && tsc && bin/dev.js manifest .",
"prepack": "shx rm -rf lib && tsc && bin/dev manifest .",
"test": "nps test && yarn test:unit && yarn test:integration",
"test:integration": "mocha --forbid-only \"test/integration/*.test.ts\"",
"test:unit": "mocha --forbid-only \"test/unit/*.test.ts\"",
"version": "bin/dev.js readme && git add README.md"
"version": "bin/dev readme && git add README.md"
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
Expand Down
17 changes: 17 additions & 0 deletions src/command-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {Command} from '@oclif/core'
import {createEnv} from 'yeoman-environment'

export default abstract class CommandBase extends Command {
protected async generate(type: string, generatorOptions: Record<string, unknown> = {}): Promise<void> {
const env = createEnv()

env.register(
require.resolve(`./generators/${type}`),
`oclif:${type}`,
)

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
await env.run(`oclif:${type}`, generatorOptions)
}
}
8 changes: 4 additions & 4 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Args, Command} from '@oclif/core'
import {generate} from '../util'
import {Args} from '@oclif/core'
import CommandBase from './../command-base'

export default class Generate extends Command {
export default class Generate extends CommandBase {
static description = `generate a new CLI
This will clone the template repo 'oclif/hello-world' and update package properties`

Expand All @@ -14,7 +14,7 @@ This will clone the template repo 'oclif/hello-world' and update package propert
async run(): Promise<void> {
const {args} = await this.parse(Generate)

await generate('cli', {
await super.generate('cli', {
name: args.name,
force: true,
})
Expand Down
8 changes: 4 additions & 4 deletions src/commands/generate/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Args, Command, Flags} from '@oclif/core'
import {generate} from '../../util'
import CommandBase from './../../command-base'
import {Args, Flags} from '@oclif/core'

export default class GenerateCommand extends Command {
export default class GenerateCommand extends CommandBase {
static description = 'add a command to an existing CLI or plugin'

static flags = {
Expand All @@ -15,7 +15,7 @@ export default class GenerateCommand extends Command {
async run(): Promise<void> {
const {args, flags} = await this.parse(GenerateCommand)

await generate('command', {
await super.generate('command', {
name: args.name,
force: flags.force,
})
Expand Down
8 changes: 4 additions & 4 deletions src/commands/generate/hook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Args, Command, Flags} from '@oclif/core'
import {generate} from '../../util'
import CommandBase from './../../command-base'
import {Args, Flags} from '@oclif/core'

export default class GenerateHook extends Command {
export default class GenerateHook extends CommandBase {
static description = 'add a hook to an existing CLI or plugin'

static flags = {
Expand All @@ -16,7 +16,7 @@ export default class GenerateHook extends Command {
async run(): Promise<void> {
const {args, flags} = await this.parse(GenerateHook)

await generate('hook', {
await super.generate('hook', {
name: args.name,
event: flags.event,
force: flags.force,
Expand Down
10 changes: 2 additions & 8 deletions src/commands/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,9 @@ export default class Manifest extends Command {
ux.action.stop()
}

let plugin = new Plugin({
root,
type: 'core',
ignoreManifest: true,
errorOnManifestCreate: true,
respectNoCacheDefault: true,
})
let plugin = new Plugin({root, type: 'core', ignoreManifest: true, errorOnManifestCreate: true})
if (!plugin) throw new Error('plugin not found')
await plugin.load()
await plugin.load(true)
if (!plugin.valid) {
const p = require.resolve('@oclif/plugin-legacy', {paths: [process.cwd()]})
const {PluginLegacy} = require(p)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Customize the code URL prefix by setting oclif.repositoryPrefix in package.json.
const p = require.resolve('@oclif/plugin-legacy', {paths: [cwd]})
const plugin = new Plugin({root: p, type: 'core'})
await plugin.load()
config.plugins.set(plugin.name, plugin)
config.plugins.push(plugin)
} catch {}

await (config as Config).runHook('init', {id: 'readme', argv: this.argv})
Expand Down Expand Up @@ -190,7 +190,7 @@ USAGE
commandCode(config: Interfaces.Config, c: Command.Cached): string | undefined {
const pluginName = c.pluginName
if (!pluginName) return
const plugin = config.plugins.get(pluginName)
const plugin = config.plugins.find(p => p.name === c.pluginName)
if (!plugin) return
const repo = this.repo(plugin)
if (!repo) return
Expand Down
5 changes: 3 additions & 2 deletions src/generators/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ export default class CLI extends Generator {
}
}

// eslint-disable-next-line complexity
async prompting(): Promise<void> {
const msg = 'Time to build an oclif CLI!'

this.log(`${msg} Version: ${version}`)

const {moduleType} = await this.prompt([
const {moduleType} = process.env.OCLIF_ALLOW_ESM ? await this.prompt([
{
type: 'list',
name: 'moduleType',
Expand All @@ -77,7 +78,7 @@ export default class CLI extends Generator {
],
default: 'cjs',
},
])
]) : 'cjs'

const repo = moduleType === 'esm' ? 'hello-world-esm' : 'hello-world'
execSync(`git clone https://github.com/oclif/${repo}.git "${path.resolve(this.name)}" --depth=1`)
Expand Down
12 changes: 0 additions & 12 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ import * as os from 'os'
import * as crypto from 'node:crypto'
import {log} from './log'
import * as fs from 'fs-extra'
import {createEnv} from 'yeoman-environment'

export async function generate(type: string, generatorOptions: Record<string, unknown> = {}): Promise<void> {
const env = createEnv()

env.register(
require.resolve(`./generators/${type}`),
`oclif:${type}`,
)

return env.run(`oclif:${type}`, generatorOptions)
}

export function castArray<T>(input?: T | T[]): T[] {
if (input === undefined) return []
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"strict": true,
"lib": ["ES2019"],
"target": "es2019"
"target": "es2017"
},
"include": [
"./src/**/*"
Expand Down
Loading

0 comments on commit 5472380

Please sign in to comment.