From 85296de51db9e61bd4c9a41fdb39a1bfea33db4c Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 10 Jan 2020 14:56:48 -0500 Subject: [PATCH] [now-build-utils] Add function `detectApiDirectory()` (#3567) - Add new exported function `detectApiDirectory()` - Add tests for both `detectApiDirectory()` and `detectOutputDirectory()` --- packages/now-build-utils/src/detect-routes.ts | 7 ++ packages/now-build-utils/src/index.ts | 6 +- .../unit.builds-and-routes-detector.test.ts | 107 ++++++++++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/packages/now-build-utils/src/detect-routes.ts b/packages/now-build-utils/src/detect-routes.ts index 5cf0e1c1cea..be77c34c1ed 100644 --- a/packages/now-build-utils/src/detect-routes.ts +++ b/packages/now-build-utils/src/detect-routes.ts @@ -328,6 +328,13 @@ export function detectOutputDirectory(builders: Builder[]): string | null { return publicBuilder ? publicBuilder.src.replace('/**/*', '') : null; } +export function detectApiDirectory(builders: Builder[]): string | null { + // TODO: We eventually want to save the api directory to + // builder.config.apiDirectory so it is only detected once + const isZeroConfig = builders.some(b => b.config && b.config.zeroConfig); + return isZeroConfig ? 'api' : null; +} + export async function detectRoutes( files: string[], builders: Builder[], diff --git a/packages/now-build-utils/src/index.ts b/packages/now-build-utils/src/index.ts index 28dcc07b39f..2072c9c3160 100644 --- a/packages/now-build-utils/src/index.ts +++ b/packages/now-build-utils/src/index.ts @@ -62,7 +62,11 @@ export { getLambdaOptionsFromFunction, }; -export { detectRoutes, detectOutputDirectory } from './detect-routes'; +export { + detectRoutes, + detectOutputDirectory, + detectApiDirectory, +} from './detect-routes'; export { detectBuilders } from './detect-builders'; export { detectFramework } from './detect-framework'; export { DetectorFilesystem } from './detectors/filesystem'; diff --git a/packages/now-build-utils/test/unit.builds-and-routes-detector.test.ts b/packages/now-build-utils/test/unit.builds-and-routes-detector.test.ts index 706bdc718e8..dd6eb37b1ba 100644 --- a/packages/now-build-utils/test/unit.builds-and-routes-detector.test.ts +++ b/packages/now-build-utils/test/unit.builds-and-routes-detector.test.ts @@ -1,5 +1,6 @@ import { Source, Route } from '@now/routing-utils'; import { detectBuilders, detectRoutes } from '../src'; +import { detectOutputDirectory, detectApiDirectory } from '../'; describe('Test `detectBuilders`', () => { it('package.json + no build', async () => { @@ -1753,6 +1754,112 @@ it('Test `detectRoutes` with `featHandleMiss=true`, `cleanUrls=true`, `trailingS } }); +describe('Test `detectOutputDirectory`', () => { + it('should be `null` with no config', async () => { + const builders = [ + { + use: '@now/static', + src: 'public/**/*', + }, + ]; + const result = detectOutputDirectory(builders); + expect(result).toBe(null); + }); + + it('should be `null` with no zero config builds', async () => { + const builders = [ + { + use: '@now/static', + src: 'public/**/*', + config: {}, + }, + ]; + const result = detectOutputDirectory(builders); + expect(result).toBe(null); + }); + + it('should be `public` with one zero config', async () => { + const builders = [ + { + use: '@now/static', + src: 'public/**/*', + config: { zeroConfig: true }, + }, + ]; + const result = detectOutputDirectory(builders); + expect(result).toBe('public'); + }); + + it('should be `public` with one zero config and one without config', async () => { + const builders = [ + { + use: '@now/static', + src: 'public/**/*', + config: { zeroConfig: true }, + }, + { + use: '@now/node', + src: 'api/index.js', + }, + ]; + const result = detectOutputDirectory(builders); + expect(result).toBe('public'); + }); +}); + +describe('Test `detectApiDirectory`', () => { + it('should be `null` with no config', async () => { + const builders = [ + { + use: '@now/node', + src: 'api/**/*.js', + }, + ]; + const result = detectApiDirectory(builders); + expect(result).toBe(null); + }); + + it('should be `null` with no zero config builds', async () => { + const builders = [ + { + use: '@now/node', + src: 'api/**/*.js', + config: {}, + }, + ]; + const result = detectApiDirectory(builders); + expect(result).toBe(null); + }); + + it('should be `api` with one zero config', async () => { + const builders = [ + { + use: '@now/node', + src: 'api/**/*.js', + config: { zeroConfig: true }, + }, + ]; + const result = detectApiDirectory(builders); + expect(result).toBe('api'); + }); + + it('should be `api` with one zero config and one without config', async () => { + const builders = [ + { + use: '@now/node', + src: 'api/**/*.js', + config: { zeroConfig: true }, + }, + { + use: '@now/php', + src: 'api/**/*.php', + }, + ]; + const result = detectApiDirectory(builders); + expect(result).toBe('api'); + }); +}); + /** * Create a function that will replace matched redirects * similar to how it works with `now-proxy` in production.