@vercel/fs-detectors 5.7.22 → 5.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import type { Route } from '@vercel/routing-utils';
2
- import type { PackageJson, Builder, BuilderFunctions, ProjectSettings } from '@vercel/build-utils';
2
+ import type { PackageJson, Builder, BuilderFunctions, ProjectSettings, Service } from '@vercel/build-utils';
3
3
  /**
4
4
  * Pattern for finding all supported middleware files.
5
5
  */
@@ -41,4 +41,5 @@ export declare function detectBuilders(files: string[], pkg?: PackageJson | unde
41
41
  redirectRoutes: Route[] | null;
42
42
  rewriteRoutes: Route[] | null;
43
43
  errorRoutes: Route[] | null;
44
+ services?: Service[];
44
45
  }>;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export { detectBuilders, detectOutputDirectory, detectApiDirectory, detectApiExtensions, type Options as DetectBuildersOptions, } from './detect-builders';
2
2
  export { detectServices, generateServicesRoutes, } from './services/detect-services';
3
+ export { autoDetectServices } from './services/auto-detect';
4
+ export type { AutoDetectOptions, AutoDetectResult, } from './services/auto-detect';
3
5
  export { isStaticBuild } from './services/utils';
4
6
  export { getServicesBuilders } from './services/get-services-builders';
5
7
  export type { DetectServicesOptions, DetectServicesResult, ResolvedService, ServicesRoutes, ServiceDetectionError, } from './services/types';
package/dist/index.js CHANGED
@@ -26,6 +26,7 @@ __export(src_exports, {
26
26
  REGEX_NON_VERCEL_PLATFORM_FILES: () => import_detect_builders2.REGEX_NON_VERCEL_PLATFORM_FILES,
27
27
  Workspace: () => import_get_workspaces.Workspace,
28
28
  WorkspaceType: () => import_get_workspaces.WorkspaceType,
29
+ autoDetectServices: () => import_auto_detect.autoDetectServices,
29
30
  detectApiDirectory: () => import_detect_builders.detectApiDirectory,
30
31
  detectApiExtensions: () => import_detect_builders.detectApiExtensions,
31
32
  detectBuilders: () => import_detect_builders.detectBuilders,
@@ -52,6 +53,7 @@ __export(src_exports, {
52
53
  module.exports = __toCommonJS(src_exports);
53
54
  var import_detect_builders = require("./detect-builders");
54
55
  var import_detect_services = require("./services/detect-services");
56
+ var import_auto_detect = require("./services/auto-detect");
55
57
  var import_utils = require("./services/utils");
56
58
  var import_get_services_builders = require("./services/get-services-builders");
57
59
  var import_detect_file_system_api = require("./detect-file-system-api");
@@ -77,6 +79,7 @@ var import_detect_instrumentation = require("./detect-instrumentation");
77
79
  REGEX_NON_VERCEL_PLATFORM_FILES,
78
80
  Workspace,
79
81
  WorkspaceType,
82
+ autoDetectServices,
80
83
  detectApiDirectory,
81
84
  detectApiExtensions,
82
85
  detectBuilders,
@@ -0,0 +1,39 @@
1
+ import type { DetectorFilesystem } from '../detectors/filesystem';
2
+ import type { ExperimentalServices, ServiceDetectionError } from './types';
3
+ export interface AutoDetectOptions {
4
+ fs: DetectorFilesystem;
5
+ }
6
+ export interface AutoDetectResult {
7
+ services: ExperimentalServices | null;
8
+ errors: ServiceDetectionError[];
9
+ }
10
+ /**
11
+ * Auto-detect services when experimentalServices is not configured.
12
+ *
13
+ * Scans the project for frameworks, supporting multiple layouts:
14
+ *
15
+ * Frontend at root, backend in backend/:
16
+ * project/
17
+ * ├── package.json
18
+ * └── backend/
19
+ *
20
+ * Frontend in frontend/, backend in backend/:
21
+ * project/
22
+ * ├── frontend/
23
+ * └── backend/
24
+ *
25
+ * Frontend in frontend/, backend in services/{service-name}/:
26
+ * project/
27
+ * ├── frontend/
28
+ * └── services/
29
+ * ├── service-a/
30
+ * └── service-b/
31
+ *
32
+ * Frontend in apps/web/ monorepo, backend in services/{service-name}/:
33
+ * project/
34
+ * ├── apps/web/
35
+ * └── services/
36
+ * ├── service-a/
37
+ * └── service-b/
38
+ */
39
+ export declare function autoDetectServices(options: AutoDetectOptions): Promise<AutoDetectResult>;
@@ -0,0 +1,228 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var auto_detect_exports = {};
30
+ __export(auto_detect_exports, {
31
+ autoDetectServices: () => autoDetectServices
32
+ });
33
+ module.exports = __toCommonJS(auto_detect_exports);
34
+ var import_detect_framework = require("../detect-framework");
35
+ var import_frameworks = __toESM(require("@vercel/frameworks"));
36
+ const FRONTEND_DIR = "frontend";
37
+ const APPS_WEB_DIR = "apps/web";
38
+ const BACKEND_DIR = "backend";
39
+ const SERVICES_DIR = "services";
40
+ const FRONTEND_LOCATIONS = [FRONTEND_DIR, APPS_WEB_DIR];
41
+ async function autoDetectServices(options) {
42
+ const { fs } = options;
43
+ const rootFrameworks = await (0, import_detect_framework.detectFrameworks)({
44
+ fs,
45
+ frameworkList: import_frameworks.default
46
+ });
47
+ if (rootFrameworks.length > 1) {
48
+ const frameworkNames = rootFrameworks.map((f) => f.name).join(", ");
49
+ return {
50
+ services: null,
51
+ errors: [
52
+ {
53
+ code: "MULTIPLE_FRAMEWORKS_ROOT",
54
+ message: `Multiple frameworks detected at root: ${frameworkNames}. Use explicit experimentalServices config.`
55
+ }
56
+ ]
57
+ };
58
+ }
59
+ if (rootFrameworks.length === 1) {
60
+ return detectServicesAtRoot(fs, rootFrameworks[0]);
61
+ }
62
+ for (const frontendLocation of FRONTEND_LOCATIONS) {
63
+ const hasFrontendDir = await fs.hasPath(frontendLocation);
64
+ if (!hasFrontendDir) {
65
+ continue;
66
+ }
67
+ const frontendFs = fs.chdir(frontendLocation);
68
+ const frontendFrameworks = await (0, import_detect_framework.detectFrameworks)({
69
+ fs: frontendFs,
70
+ frameworkList: import_frameworks.default
71
+ });
72
+ if (frontendFrameworks.length > 1) {
73
+ const frameworkNames = frontendFrameworks.map((f) => f.name).join(", ");
74
+ return {
75
+ services: null,
76
+ errors: [
77
+ {
78
+ code: "MULTIPLE_FRAMEWORKS_SERVICE",
79
+ message: `Multiple frameworks detected in ${frontendLocation}/: ${frameworkNames}. Use explicit experimentalServices config.`
80
+ }
81
+ ]
82
+ };
83
+ }
84
+ if (frontendFrameworks.length === 1) {
85
+ return detectServicesFrontendSubdir(
86
+ fs,
87
+ frontendFrameworks[0],
88
+ frontendLocation
89
+ );
90
+ }
91
+ }
92
+ return {
93
+ services: null,
94
+ errors: [
95
+ {
96
+ code: "NO_SERVICES_CONFIGURED",
97
+ message: "No services detected. Configure experimentalServices in vercel.json or ensure a framework exists at project root, frontend/, or apps/web/."
98
+ }
99
+ ]
100
+ };
101
+ }
102
+ async function detectServicesAtRoot(fs, rootFramework) {
103
+ const services = {};
104
+ services.frontend = {
105
+ framework: rootFramework.slug ?? void 0,
106
+ routePrefix: "/"
107
+ };
108
+ const backendResult = await detectBackendServices(fs);
109
+ if (backendResult.error) {
110
+ return { services: null, errors: [backendResult.error] };
111
+ }
112
+ Object.assign(services, backendResult.services);
113
+ return { services, errors: [] };
114
+ }
115
+ async function detectServicesFrontendSubdir(fs, frontendFramework, frontendLocation) {
116
+ const services = {};
117
+ const serviceName = frontendLocation.split("/").pop() || "frontend";
118
+ services[serviceName] = {
119
+ framework: frontendFramework.slug ?? void 0,
120
+ workspace: frontendLocation,
121
+ routePrefix: "/"
122
+ };
123
+ const backendResult = await detectBackendServices(fs);
124
+ if (backendResult.error) {
125
+ return { services: null, errors: [backendResult.error] };
126
+ }
127
+ if (Object.keys(backendResult.services).length === 0) {
128
+ return {
129
+ services: null,
130
+ errors: [
131
+ {
132
+ code: "NO_BACKEND_SERVICES",
133
+ message: `Frontend detected in ${frontendLocation}/ but no backend services found. Add a backend/ or services/ directory with a supported framework.`
134
+ }
135
+ ]
136
+ };
137
+ }
138
+ Object.assign(services, backendResult.services);
139
+ return { services, errors: [] };
140
+ }
141
+ async function detectBackendServices(fs) {
142
+ const services = {};
143
+ const backendResult = await detectServiceInDir(fs, BACKEND_DIR, "backend");
144
+ if (backendResult.error) {
145
+ return { services: {}, error: backendResult.error };
146
+ }
147
+ if (backendResult.service) {
148
+ services.backend = backendResult.service;
149
+ }
150
+ const multiServicesResult = await detectServicesDirectory(fs);
151
+ if (multiServicesResult.error) {
152
+ return { services: {}, error: multiServicesResult.error };
153
+ }
154
+ for (const serviceName of Object.keys(multiServicesResult.services)) {
155
+ if (services[serviceName]) {
156
+ return {
157
+ services: {},
158
+ error: {
159
+ code: "SERVICE_NAME_CONFLICT",
160
+ message: `Service name conflict: "${serviceName}" exists in both ${BACKEND_DIR}/ and ${SERVICES_DIR}/${serviceName}/. Rename one of the directories or use explicit experimentalServices config.`,
161
+ serviceName
162
+ }
163
+ };
164
+ }
165
+ }
166
+ Object.assign(services, multiServicesResult.services);
167
+ return { services };
168
+ }
169
+ async function detectServicesDirectory(fs) {
170
+ const services = {};
171
+ const hasServicesDir = await fs.hasPath(SERVICES_DIR);
172
+ if (!hasServicesDir) {
173
+ return { services };
174
+ }
175
+ const servicesFs = fs.chdir(SERVICES_DIR);
176
+ const entries = await servicesFs.readdir("/");
177
+ for (const entry of entries) {
178
+ if (entry.type !== "dir") {
179
+ continue;
180
+ }
181
+ const serviceName = entry.name;
182
+ const serviceDir = `${SERVICES_DIR}/${serviceName}`;
183
+ const result = await detectServiceInDir(fs, serviceDir, serviceName);
184
+ if (result.error) {
185
+ return { services: {}, error: result.error };
186
+ }
187
+ if (result.service) {
188
+ services[serviceName] = result.service;
189
+ }
190
+ }
191
+ return { services };
192
+ }
193
+ async function detectServiceInDir(fs, dirPath, serviceName) {
194
+ const hasDirPath = await fs.hasPath(dirPath);
195
+ if (!hasDirPath) {
196
+ return {};
197
+ }
198
+ const serviceFs = fs.chdir(dirPath);
199
+ const frameworks = await (0, import_detect_framework.detectFrameworks)({
200
+ fs: serviceFs,
201
+ frameworkList: import_frameworks.default
202
+ });
203
+ if (frameworks.length > 1) {
204
+ const frameworkNames = frameworks.map((f) => f.name).join(", ");
205
+ return {
206
+ error: {
207
+ code: "MULTIPLE_FRAMEWORKS_SERVICE",
208
+ message: `Multiple frameworks detected in ${dirPath}/: ${frameworkNames}. Use explicit experimentalServices config.`,
209
+ serviceName
210
+ }
211
+ };
212
+ }
213
+ if (frameworks.length === 1) {
214
+ const framework = frameworks[0];
215
+ return {
216
+ service: {
217
+ framework: framework.slug ?? void 0,
218
+ workspace: dirPath,
219
+ routePrefix: `/_/${serviceName}`
220
+ }
221
+ };
222
+ }
223
+ return {};
224
+ }
225
+ // Annotate the CommonJS export names for ESM import in node:
226
+ 0 && (module.exports = {
227
+ autoDetectServices
228
+ });
@@ -24,6 +24,7 @@ __export(detect_services_exports, {
24
24
  module.exports = __toCommonJS(detect_services_exports);
25
25
  var import_utils = require("./utils");
26
26
  var import_resolve = require("./resolve");
27
+ var import_auto_detect = require("./auto-detect");
27
28
  async function detectServices(options) {
28
29
  const { fs, workPath } = options;
29
30
  const scopedFs = workPath ? fs.chdir(workPath) : fs;
@@ -39,6 +40,25 @@ async function detectServices(options) {
39
40
  const configuredServices = vercelConfig?.experimentalServices;
40
41
  const hasConfiguredServices = configuredServices && Object.keys(configuredServices).length > 0;
41
42
  if (!hasConfiguredServices) {
43
+ const autoResult = await (0, import_auto_detect.autoDetectServices)({ fs: scopedFs });
44
+ if (autoResult.errors.length > 0) {
45
+ return {
46
+ services: [],
47
+ routes: { rewrites: [], defaults: [], crons: [], workers: [] },
48
+ errors: autoResult.errors,
49
+ warnings: []
50
+ };
51
+ }
52
+ if (autoResult.services) {
53
+ const result2 = (0, import_resolve.resolveAllConfiguredServices)(autoResult.services);
54
+ const routes2 = generateServicesRoutes(result2.services);
55
+ return {
56
+ services: result2.services,
57
+ routes: routes2,
58
+ errors: result2.errors,
59
+ warnings: []
60
+ };
61
+ }
42
62
  return {
43
63
  services: [],
44
64
  routes: { rewrites: [], defaults: [], crons: [], workers: [] },
@@ -1,29 +1,11 @@
1
1
  import type { Route } from '@vercel/routing-utils';
2
- import type { ExperimentalServiceConfig, ExperimentalServiceGroups, ExperimentalServices, ServiceRuntime, ServiceType, Builder } from '@vercel/build-utils';
2
+ import type { ExperimentalServiceConfig, ExperimentalServiceGroups, ExperimentalServices, ServiceRuntime, ServiceType, Service, Builder } from '@vercel/build-utils';
3
3
  import type { DetectorFilesystem } from '../detectors/filesystem';
4
- export type { ExperimentalServiceConfig, ExperimentalServiceGroups, ExperimentalServices, ServiceRuntime, ServiceType, Builder, };
5
- export interface ResolvedService {
6
- name: string;
7
- type: ServiceType;
8
- /** Service group name if this service belongs to a group */
9
- group?: string;
10
- workspace: string;
11
- entrypoint?: string;
12
- framework?: string;
13
- builder: Builder;
14
- buildCommand?: string;
15
- installCommand?: string;
16
- runtime?: string;
17
- /**
18
- * URL path prefix for routing requests to this service.
19
- * Required for web services; requests matching this prefix are routed to this service.
20
- * Root services use "/" as the catch-all.
21
- */
22
- routePrefix?: string;
23
- schedule?: string;
24
- topic?: string;
25
- consumer?: string;
26
- }
4
+ export type { ExperimentalServiceConfig, ExperimentalServiceGroups, ExperimentalServices, ServiceRuntime, ServiceType, Service, Builder, };
5
+ /**
6
+ * @deprecated Use `Service` instead
7
+ */
8
+ export type ResolvedService = Service;
27
9
  export interface DetectServicesOptions {
28
10
  fs: DetectorFilesystem;
29
11
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/fs-detectors",
3
- "version": "5.7.22",
3
+ "version": "5.8.0",
4
4
  "description": "Vercel filesystem detectors",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -20,8 +20,8 @@
20
20
  "minimatch": "3.1.2",
21
21
  "semver": "6.3.1",
22
22
  "@vercel/error-utils": "2.0.3",
23
- "@vercel/routing-utils": "5.3.2",
24
- "@vercel/frameworks": "3.16.1"
23
+ "@vercel/frameworks": "3.16.1",
24
+ "@vercel/routing-utils": "5.3.2"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/glob": "7.2.0",
@@ -32,7 +32,7 @@
32
32
  "@types/semver": "7.3.10",
33
33
  "jest-junit": "16.0.0",
34
34
  "typescript": "4.9.5",
35
- "@vercel/build-utils": "13.2.17"
35
+ "@vercel/build-utils": "13.3.0"
36
36
  },
37
37
  "scripts": {
38
38
  "build": "node ../../utils/build.mjs",