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

Extends Config/Plugin for Deno packaging #504

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
21 changes: 17 additions & 4 deletions src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}:` : ''
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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}`
Expand Down Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/config/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as fs from 'fs'
import * as path from 'path'
import * as JSON5 from 'json5'

const debug = require('debug')

Expand All @@ -23,13 +25,16 @@ export function resolvePackage(id: string, paths: { paths: string[] }): string {
return require.resolve(id, paths)
}

export function loadJSON(path: string): Promise<any> {
export function loadJSON(_path: string): Promise<any> {
debug('config')('loadJSON %s', path)
// Allows reading JSON with comments
const _JSON = path.extname(_path) === '.jsonc' ? JSON5 : JSON
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking out loud here, maybe using dynamic imports here (so json5 isn't loaded by json users) might be kind.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, although it seems salesforce ppl have lost interest in supporting Deno

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)
}
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/pjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export namespace PJSON {
version?: string;
targets?: string[];
};
deno?: {
version?: string;
targets?: string[];
};
};
topics?: {
[k: string]: {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down