Skip to content

Commit

Permalink
feat(quasar): add docsUrl to API definition (quasarframework#4045)
Browse files Browse the repository at this point in the history
* quasar docs command

* add docs

* add sections

* add sections to command

* fix open browser

* pass supplier to docs function

* read quasar conf for open browser

* fix command with app extensions API

* open-browser using its own logger

* fix Ripple docs

* move "docs" defaults to build.api.js

* fix DocApi trying to render docs field

* fix QUploaderAddTrigger page

* feat(api) change "docs" key to "meta"

* fix(api) definitions for meta

* fix(api) definitions for meta & `quasar docs` usage

* feat(api) move docs opening to describe command and prefix API with "docs"

* feat(docs) add new quasar describe option

* fix(quasar) code review updates

* fix(app) describe command
  • Loading branch information
lucasfernog authored and rstoenescu committed Nov 26, 2019
1 parent d910893 commit eb3cb1e
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 111 deletions.
114 changes: 31 additions & 83 deletions bin/quasar-describe
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ const
chalk = require('chalk')

const
appPaths = require('../lib/app-paths'),
getApi = require('../lib/helpers/get-api'),
logger = require('../lib/helpers/logger'),
warn = logger('app:describe', 'red')
warn = logger('app:describe', 'red'),
openBrowser = require('../lib/helpers/open-browser')

const partArgs = {
p: 'props',
Expand All @@ -18,7 +19,8 @@ const partArgs = {
a: 'arg',
M: 'modifiers',
i: 'injection',
q: 'quasar'
q: 'quasar',
d: 'docs'
}

const partArgsKeys = Object.keys(partArgs)
Expand Down Expand Up @@ -55,6 +57,9 @@ if (!item || argv.help) {
# filtering only props by "co":
$ quasar describe QIcon -p -f co
# Open docs URL:
$ quasar describe QIcon -d
Options
--filter, -f <filter> Filters the API
--props, -p Displays the API props
Expand All @@ -66,6 +71,7 @@ if (!item || argv.help) {
--modifiers, -M Displays the API modifiers
--injection, -i Displays the API injection
--quasar, -q Displays the API quasar conf options
--docs, -d Opens the docs API URL
--help, -h Displays this message
`)
process.exit(0)
Expand All @@ -80,77 +86,10 @@ if (partArgsKeys.some(part => argv[part])) {
}
else {
Object.values(partArgs).forEach(part => {
apiParts[part] = true
})
}

async function getApi (item) {
try {
const api = require(
require.resolve(`quasar/dist/api/${item}.json`, {
paths: [ appPaths.appDir ]
})
)
return { api }
}
catch (e) {}

const extensionJson = require('../lib/app-extension/extension-json')
const extensions = Object.keys(extensionJson.getList())

if (extensions.length > 0) {
const Extension = require('../lib/app-extension/Extension.js')

for (let ext of extensions) {
const instance = new Extension(ext)
const hooks = await instance.run({})

if (hooks.describeApi !== void 0 && hooks.describeApi[item]) {
const fs = require('fs')
const path = require('path')

let file
const { callerPath, relativePath } = hooks.describeApi[item]

if (relativePath.charAt(0) === '~') {
try {
file = relativePath.slice(1)
file = require.resolve(
file,
{ paths: [ callerPath, appPaths.appDir ] }
)
}
catch (e) {
warn(`⚠️ Extension(${instance.extId}): registerDescribeApi - there is no package "${file}" installed`)
process.exit(1)
}
}
else {
file = path.resolve(callerPath, relativePath)

if (!fs.existsSync(file)) {
warn(`⚠️ Extension(${instance.extId}): registerDescribeApi - there is no file at ${file}`)
process.exit(1)
}
}

try {
return {
api: require(file),
supplier: instance.extId
}
}
catch (e) {
warn(`⚠️ Extension(${instance.extId}): Malformed API file at ${file}`)
process.exit(1)
}
}
if (part !== 'docs') {
apiParts[part] = true
}
}

warn(`⚠️ No API found for requested "${item}"`)
warn()
process.exit(1)
})
}

function getEventParams (event) {
Expand Down Expand Up @@ -544,23 +483,32 @@ function describe (api) {
apiParts.methods === true && printMethods(api)
break
}
if (apiParts.docs && api.meta && api.meta.docsUrl) {
openBrowser(api.meta.docsUrl)
}
}

async function run () {
const { api, supplier } = await getApi(item)
try {
const { api, supplier } = await getApi(item)

console.log()
console.log(` Describing ${chalk.green(item)} ${api.type} API`)
console.log()
console.log(` Describing ${chalk.green(item)} ${api.type} API`)

if (supplier === void 0) {
console.log(` ${chalk.italic(`Description is based on your project's Quasar version`)}`)
}
else {
console.log(` ${chalk.italic(`Supplied by "${supplier}" App Extension`)}`)
}

if (supplier === void 0) {
console.log(` ${chalk.italic(`Description is based on your project's Quasar version`)}`)
describe(api)
console.log()
}
else {
console.log(` ${chalk.italic(`Supplied by "${supplier}" App Extension`)}`)
catch (e) {
warn(e)
process.exit(1)
}

describe(api)
console.log()
}

run()
30 changes: 2 additions & 28 deletions lib/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,12 @@ const

const
appPaths = require('./app-paths'),
logger = require('./helpers/logger')
logger = require('./helpers/logger'),
openBrowser = require('./helpers/open-browser')
log = logger('app:dev-server'),
warn = logger('app:dev-server', 'red')

let alreadyNotified = false

function openBrowser (url, opts) {
const open = require('open')

const openDefault = () => {
log('Opening default browser at ' + url)
log()
open(url, { wait: true, url: true }).catch(() => {
warn(`⚠️ Failed to open default browser`)
warn()
})
}

if (opts) {
log('Opening browser at ' + url + ' with options: ' + opts)
log()
open(url, { app: opts, wait: true, url: true }).catch(() => {
warn(`⚠️ Failed to open specific browser`)
warn()
openDefault()
})
}
else {
openDefault()
}
}

module.exports = class DevServer {
constructor (quasarConfig) {
this.quasarConfig = quasarConfig
Expand Down
74 changes: 74 additions & 0 deletions lib/helpers/get-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const appPaths = require('../app-paths'),
logger = require('./logger'),
warn = logger('app:docs', 'red')

module.exports = async function getApi(item) {
try {
const api = require(
require.resolve(`quasar/dist/api/${item}.json`, {
paths: [appPaths.appDir]
})
)
return {
api
}
} catch (e) {}

const extensionJson = require('../app-extension/extension-json')
const extensions = Object.keys(extensionJson.getList())

if (extensions.length > 0) {
const Extension = require('../app-extension/Extension.js')

for (let ext of extensions) {
const instance = new Extension(ext)
const hooks = await instance.run({})

if (hooks.describeApi !== void 0 && hooks.describeApi[item]) {
const fs = require('fs')
const path = require('path')

let file
const {
callerPath,
relativePath
} = hooks.describeApi[item]

if (relativePath.charAt(0) === '~') {
try {
file = relativePath.slice(1)
file = require.resolve(
file, {
paths: [callerPath, appPaths.appDir]
}
)
} catch (e) {
warn(`⚠️ Extension(${instance.extId}): registerDescribeApi - there is no package "${file}" installed`)
process.exit(1)
}
} else {
file = path.resolve(callerPath, relativePath)

if (!fs.existsSync(file)) {
warn(`⚠️ Extension(${instance.extId}): registerDescribeApi - there is no file at ${file}`)
process.exit(1)
}
}

try {
return {
api: require(file),
supplier: instance.extId
}
} catch (e) {
warn(`⚠️ Extension(${instance.extId}): Malformed API file at ${file}`)
process.exit(1)
}
}
}
}

warn(`⚠️ No API found for requested "${item}"`)
warn()
process.exit(1)
}
35 changes: 35 additions & 0 deletions lib/helpers/open-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const logger = require('./logger'),
log = logger('app:browser'),
warn = logger('app:browser', 'red')

module.exports = function openBrowser(url, opts) {
const open = require('open')

const openDefault = () => {
log('Opening default browser at ' + url)
log()
open(url, {
wait: true,
url: true
}).catch(() => {
warn(`⚠️ Failed to open default browser`)
warn()
})
}

if (opts) {
log('Opening browser at ' + url + ' with options: ' + opts)
log()
open(url, {
app: opts,
wait: true,
url: true
}).catch(() => {
warn(`⚠️ Failed to open specific browser`)
warn()
openDefault()
})
} else {
openDefault()
}
}

0 comments on commit eb3cb1e

Please sign in to comment.