diff --git a/package.json b/package.json index bf0c409c2..7bfd1d489 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "@oclif/core", + "name": "oclif-core-rc", "description": "base library for oclif CLIs", - "version": "1.16.4", + "version": "16.6.7", "author": "Salesforce", "bugs": "https://github.com/oclif/core/issues", "dependencies": { @@ -22,6 +22,7 @@ "indent-string": "^4.0.0", "is-wsl": "^2.2.0", "js-yaml": "^3.14.1", + "json5": "^2.2.1", "natural-orderby": "^2.0.3", "object-treeify": "^1.1.33", "password-prompt": "^1.1.2", diff --git a/src/config/plugin.ts b/src/config/plugin.ts index 9e69b002b..b95cb013b 100644 --- a/src/config/plugin.ts +++ b/src/config/plugin.ts @@ -17,6 +17,9 @@ import ModuleLoader from '../module-loader' const _pjson = require('../../package.json') +const PACKAGE_JSON = 'package.json' +const DENO_JSON = 'deno.jsonc' + function topicsToArray(input: any, base?: string): Topic[] { if (!input) return [] base = base ? `${base}:` : '' @@ -40,11 +43,14 @@ function * up(from: string) { yield from } +// Finds the root dir based on a node or a deno root config file, +// which ever is closest up in the directory tree. async function findSourcesRoot(root: string) { for (const next of up(root)) { - const cur = path.join(next, 'package.json') + const nodeConfigFile = path.join(next, PACKAGE_JSON) + const denoConfigFile = path.join(next, DENO_JSON) // eslint-disable-next-line no-await-in-loop - if (await exists(cur)) return path.dirname(cur) + if (await exists(nodeConfigFile) || await exists(denoConfigFile)) return path.dirname(nodeConfigFile) } } @@ -92,6 +98,11 @@ async function findRoot(name: string | undefined, root: string) { return findSourcesRoot(root) } +async function findRootConfigFile(rootPath: string) { + if (await exists(path.join(rootPath, PACKAGE_JSON))) return PACKAGE_JSON + if (await exists(path.join(rootPath, DENO_JSON))) return DENO_JSON +} + export class Plugin implements IPlugin { // static loadedPlugins: {[name: string]: Plugin} = {} _base = `${_pjson.name}@${_pjson.version}` @@ -137,12 +148,14 @@ export class Plugin implements IPlugin { this.tag = this.options.tag const root = await findRoot(this.options.name, this.options.root) if (!root) throw new Error(`could not find package.json with ${inspect(this.options)}`) + const rootConfigFile = await findRootConfigFile(root) + if (!rootConfigFile) throw new Error(`could not find ${rootConfigFile} in ${root}`) this.root = root this._debug('reading %s plugin %s', this.type, root) - this.pjson = await loadJSON(path.join(root, 'package.json')) as any + this.pjson = await loadJSON(path.join(root, rootConfigFile)) as any this.name = this.pjson.name this.alias = this.options.name ?? this.pjson.name - const pjsonPath = path.join(root, 'package.json') + const pjsonPath = path.join(root, rootConfigFile) if (!this.name) throw new Error(`no name in ${pjsonPath}`) if (!isProd() && !this.pjson.files) this.warn(`files attribute must be specified in ${pjsonPath}`) // eslint-disable-next-line new-cap diff --git a/src/config/util.ts b/src/config/util.ts index 5051b06c9..66f541e34 100644 --- a/src/config/util.ts +++ b/src/config/util.ts @@ -1,4 +1,6 @@ import * as fs from 'fs' +import * as path from 'path' +import * as JSON5 from 'json5' const debug = require('debug') @@ -23,13 +25,16 @@ export function resolvePackage(id: string, paths: { paths: string[] }): string { return require.resolve(id, paths) } -export function loadJSON(path: string): Promise { +export function loadJSON(_path: string): Promise { debug('config')('loadJSON %s', path) + // Allows reading JSON with comments + const _JSON = path.extname(_path) === '.jsonc' ? JSON5 : JSON return new Promise((resolve, reject) => { - fs.readFile(path, 'utf8', (err: any, d: any) => { + fs.readFile(_path, 'utf8', (err: any, d: any) => { try { if (err) reject(err) - else resolve(JSON.parse(d)) + const obj = _JSON.parse(d) + resolve(obj) } catch (error: any) { reject(error) } diff --git a/src/interfaces/pjson.ts b/src/interfaces/pjson.ts index bb7285b15..4eb1f83c7 100644 --- a/src/interfaces/pjson.ts +++ b/src/interfaces/pjson.ts @@ -36,6 +36,10 @@ export namespace PJSON { version?: string; targets?: string[]; }; + deno?: { + version?: string; + targets?: string[]; + }; }; topics?: { [k: string]: { diff --git a/yarn.lock b/yarn.lock index 43a20535f..1e83631c0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2249,6 +2249,11 @@ json5@^2.1.2: dependencies: minimist "^1.2.5" +json5@^2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c" + integrity sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA== + jsonfile@^6.0.1: version "6.1.0" resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae"