Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #31 from netlify/feat/has-framework
Browse files Browse the repository at this point in the history
Add `hasFramework()`
  • Loading branch information
ehmicky authored Oct 29, 2020
2 parents 7dbe1c0 + f4cb81c commit 8e90ea9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The following frameworks are detected:
# Example (Node.js)

```js
const { listFrameworks, getFramework } = require('@netlify/framework-info')
const { listFrameworks, hasFramework, getFramework } = require('@netlify/framework-info')

console.log(await listFrameworks({ projectDir: './path/to/gatsby/website' }))
// [
Expand Down Expand Up @@ -50,6 +50,9 @@ console.log(await listFrameworks({ projectDir: './path/to/vue/website' }))
// }
// ]

console.log(await hasFramework('vue', { projectDir: './path/to/vue/website' }))
// true

console.log(await getFramework('vue', { projectDir: './path/to/vue/website' }))
// {
// name: 'vue',
Expand Down Expand Up @@ -161,6 +164,13 @@ _Type_: `object`

Environment variables that should be set when calling the watch command.

## hasFramework(frameworkName, options?)

`options`: `object?`\
_Return value_: `Promise<boolean>`

Same as [`listFramework()`](#listframeworksoptions) except only for a specific framework and returns a boolean.

## getFramework(frameworkName, options?)

`options`: `object?`\
Expand Down
21 changes: 20 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,28 @@ const listFrameworks = async function(opts) {
return frameworkInfos
}

/**
* Return whether a project uses a specific framework
*
* @param {string} frameworkName - Name such as `"gatsby"`
* @param {object} [options] - Options
* @param {string} [flags.projectDir=process.cwd()] - Project's directory
* @param {string} [flags.ignoredWatchCommand] - When guessing the watch command, ignore `package.json` `scripts` whose value includes this string
*
* @returns {boolean} result - Whether the project uses this framework
*/
const hasFramework = async function(frameworkName, opts) {
const framework = getFrameworkByName(frameworkName)
const { projectDir, ignoredWatchCommand } = getOptions(opts)
const { npmDependencies } = await getProjectInfo({ projectDir, ignoredWatchCommand })
const result = await usesFramework(framework, { projectDir, npmDependencies })
return result
}

/**
* Return some information about a framework used by a project.
*
* @param {string} frameworkName - Name such as `"gatsby"`
* @param {object} [options] - Options
* @param {string} [flags.projectDir=process.cwd()] - Project's directory
* @param {string} [flags.ignoredWatchCommand] - When guessing the watch command, ignore `package.json` `scripts` whose value includes this string
Expand Down Expand Up @@ -82,4 +101,4 @@ const getFrameworkInfo = function(
return { name, category, watch: { commands: watchCommands, directory, port }, env }
}

module.exports = { listFrameworks, getFramework }
module.exports = { listFrameworks, hasFramework, getFramework }
8 changes: 6 additions & 2 deletions test/helpers/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { listFrameworks, getFramework: getFrameworkLib } = require('../../src/main.js')
const { listFrameworks, getFramework: getFrameworkLib, hasFramework: hasFrameworkLib } = require('../../src/main.js')

const FIXTURES_DIR = `${__dirname}/../fixtures`

Expand All @@ -11,4 +11,8 @@ const getFramework = function(fixtureName, frameworkName, opts = {}) {
return getFrameworkLib(frameworkName, { projectDir: `${FIXTURES_DIR}/${fixtureName}`, ...opts })
}

module.exports = { getFrameworks, getFramework, FIXTURES_DIR }
const hasFramework = function(fixtureName, frameworkName, opts = {}) {
return hasFrameworkLib(frameworkName, { projectDir: `${FIXTURES_DIR}/${fixtureName}`, ...opts })
}

module.exports = { getFrameworks, getFramework, hasFramework, FIXTURES_DIR }
16 changes: 14 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('ava')

const { getFrameworks, getFramework } = require('./helpers/main.js')
const { getFrameworks, getFramework, hasFramework } = require('./helpers/main.js')

test('Should detect frameworks', async t => {
const frameworks = await getFrameworks('simple')
Expand All @@ -22,6 +22,18 @@ test('Should allow getting a specific framework', async t => {
t.snapshot(framework)
})

test('Should throw when passing an inva;lid framework', async t => {
test('Should throw when passing an invalid framework', async t => {
await t.throwsAsync(getFramework('simple', 'doesNotExist'))
})

test('Should allow testing a specific framework', async t => {
const trueResult = await hasFramework('simple', 'sapper')
t.true(trueResult)

const falseResult = await hasFramework('simple', 'nuxt')
t.false(falseResult)
})

test('Should throw when testing an invalid framework', async t => {
await t.throwsAsync(hasFramework('simple', 'doesNotExist'))
})

0 comments on commit 8e90ea9

Please sign in to comment.