From f4cb81c734127c23575a971c724f3a8a970de345 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 28 Oct 2020 16:26:11 +0100 Subject: [PATCH] Add `hasFramework()` --- README.md | 12 +++++++++++- src/main.js | 21 ++++++++++++++++++++- test/helpers/main.js | 8 ++++++-- test/main.js | 16 ++++++++++++++-- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f85f3994..baac0208 100644 --- a/README.md +++ b/README.md @@ -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' })) // [ @@ -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', @@ -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` + +Same as [`listFramework()`](#listframeworksoptions) except only for a specific framework and returns a boolean. + ## getFramework(frameworkName, options?) `options`: `object?`\ diff --git a/src/main.js b/src/main.js index 25714b7c..4f62767c 100644 --- a/src/main.js +++ b/src/main.js @@ -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 @@ -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 } diff --git a/test/helpers/main.js b/test/helpers/main.js index bd4a3b0f..40dbf318 100644 --- a/test/helpers/main.js +++ b/test/helpers/main.js @@ -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` @@ -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 } diff --git a/test/main.js b/test/main.js index de6ea309..b7dc79ce 100644 --- a/test/main.js +++ b/test/main.js @@ -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') @@ -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')) +})