Skip to content

Commit

Permalink
[now-build-utils] Add function detectApiDirectory() (#3567)
Browse files Browse the repository at this point in the history
- Add new exported function `detectApiDirectory()`
- Add tests for both `detectApiDirectory()` and `detectOutputDirectory()`
  • Loading branch information
styfle authored and kodiakhq[bot] committed Jan 10, 2020
1 parent 0b65932 commit 85296de
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/now-build-utils/src/detect-routes.ts
Expand Up @@ -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[],
Expand Down
6 changes: 5 additions & 1 deletion packages/now-build-utils/src/index.ts
Expand Up @@ -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';
Expand Down
107 changes: 107 additions & 0 deletions 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 () => {
Expand Down Expand Up @@ -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.
Expand Down

1 comment on commit 85296de

@vercel
Copy link

@vercel vercel bot commented on 85296de Jan 10, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.