mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-08 04:22:09 +00:00
This PR:
- updates `packages/frameworks` to have most supported frameworks specify which dependency version should reflect the overall framework version
- updates `packages/fs-detectors` to allow framework detection that returns the full `Framework` record instead of just the slug
- updates `packages/next` to return the detected Next.js version in the build result
- updates `packages/cli` to leverage these changes so that `vc build` can add `framework: { version: string; }` to `config.json` output
The result is that Build Output API and supported frameworks will return their framework version in the build result of `vc build` when possible, which is used by the build container when creating the deployment. The dashboard later retrieves this value to display in richer deployment outputs.
Supports:
- https://github.com/vercel/api/pull/15601
- https://github.com/vercel/front/pull/18319
---
With the related build container updates, we get to see Next.js version in the build output. You'll see this with BOA+Prebuilt or a normal deploy:
<img width="1228" alt="Screen Shot 2022-12-09 at 2 48 12 PM" src="https://user-images.githubusercontent.com/41545/206793639-f9cd3bdf-b822-45dd-9564-95b94994271d.png">
---
### The Path to this PR
I went through all the supported frameworks and figured out how to best determine their versions. For most of them, we can check a known dependency's installed version number.
We can get most of the way only checking npm. For a handful, we'd have to support Go/Ruby/Rust/Whatever dependencies.
I started with a more complex method signature to allow for later expansion without changing the signature. It looked like this, in practice:
```
async getVersion(dependencies: DependencyMap) => depedencies['next']
```
However, after checking all currently supported frameworks, I don't think this will end up being necessary. It also has the constraint that all dependencies have to be gathered and presented to the function even though it only needs to check for one or two. That's not a huge deal if we have them already where we need them, but we don't. We could use a variant here where this function does its own lookups, but this seemed unnecessary and would beg for duplication and small variances that could cause bugs.
Further, if we only look at `package.json`, we're going to either see a specific version of a version range. To be precise, we have to look at the installed version of the package. That means checking one of the various types of lockfiles that can exist or poking into node_modules.
If we poke into node_modules to detect the installed version, we introduce another point where Yarn 3 (default mode) will not be supported. If we read lockfiles, we have to potentially parse `npm`, `pnpm`, and `yarn` lockfiles.
If we use `npm ls <package-name>`, that also fails in Yarn 3 (default mode). We could accept that and go forward anyway, which would look like:
```
const args = `ls ${packageName} --depth=0 --json`.split(' ');
const { stdout } = await execa('npm', args, { cwd });
const regex = new RegExp(String.raw`${packageName}@([\.\d]+)`);
const matches = stdout.match(regex);
if (matches) {
return matches[1];
}
```
But it turns out there's a `--json` option! That's what I ended up using, for now.
We could explore the lockfile route more, but after some initial digging, it' non-trivial. There are 3 main lockfiles we'd want to check for (npm, pnpm, and yarn) and there are different lockfile versions that put necessary data in different places. I looked for existing tools that parse this, but I didn't find any. We could certainly go down this path, but the effort doesn't seem worth it when `npm ls` gets us really close.
---
### Follow-up Versioning
Now that we know how to determine version per framework, we can vary configuration by version. In a future PR, we could allow a given value to vary by version number:
```
name: (version) => {
if (semver.gt(version, '9.8.7')) {
return 'some-framework-2''
}
return 'some-framework';
}
```
However, it may still be easier to differentiate significant versions by adding multiple entries in the list.
127 lines
3.5 KiB
TypeScript
Vendored
127 lines
3.5 KiB
TypeScript
Vendored
import path from 'path';
|
|
import { DetectorFilesystem } from '../src';
|
|
import { DetectorFilesystemStat } from '../src/detectors/filesystem';
|
|
|
|
const posixPath = path.posix;
|
|
|
|
export default class VirtualFilesystem extends DetectorFilesystem {
|
|
private files: Map<string, Buffer>;
|
|
private cwd: string;
|
|
|
|
constructor(files: { [key: string]: string | Buffer }, cwd = '') {
|
|
super();
|
|
this.files = new Map();
|
|
this.cwd = cwd;
|
|
Object.entries(files).map(([key, value]) => {
|
|
const buffer = typeof value === 'string' ? Buffer.from(value) : value;
|
|
this.files.set(key, buffer);
|
|
});
|
|
}
|
|
|
|
private _normalizePath(rawPath: string): string {
|
|
return posixPath.normalize(rawPath);
|
|
}
|
|
|
|
async _hasPath(name: string): Promise<boolean> {
|
|
const basePath = this._normalizePath(posixPath.join(this.cwd, name));
|
|
for (const file of this.files.keys()) {
|
|
if (file.startsWith(basePath)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
async _isFile(name: string): Promise<boolean> {
|
|
const basePath = this._normalizePath(posixPath.join(this.cwd, name));
|
|
return this.files.has(basePath);
|
|
}
|
|
|
|
async _readFile(name: string): Promise<Buffer> {
|
|
const basePath = this._normalizePath(posixPath.join(this.cwd, name));
|
|
const file = this.files.get(basePath);
|
|
|
|
if (file === undefined) {
|
|
throw new Error('File does not exist');
|
|
}
|
|
|
|
if (typeof file === 'string') {
|
|
return Buffer.from(file);
|
|
}
|
|
|
|
return file;
|
|
}
|
|
|
|
/**
|
|
* An example of how to implement readdir for a virtual filesystem.
|
|
*/
|
|
async _readdir(name = '/'): Promise<DetectorFilesystemStat[]> {
|
|
return (
|
|
[...this.files.keys()]
|
|
.map(filepath => {
|
|
const basePath = this._normalizePath(
|
|
posixPath.join(this.cwd, name === '/' ? '' : name)
|
|
);
|
|
const fileDirectoryName = posixPath.dirname(filepath);
|
|
|
|
if (fileDirectoryName === basePath) {
|
|
return {
|
|
name: posixPath.basename(filepath),
|
|
path: filepath.replace(
|
|
this.cwd === '' ? this.cwd : `${this.cwd}/`,
|
|
''
|
|
),
|
|
type: 'file',
|
|
};
|
|
}
|
|
|
|
if (
|
|
(basePath === '.' && fileDirectoryName !== '.') ||
|
|
fileDirectoryName.startsWith(basePath)
|
|
) {
|
|
let subDirectoryName = fileDirectoryName.replace(
|
|
basePath === '.' ? '' : `${basePath}/`,
|
|
''
|
|
);
|
|
|
|
if (subDirectoryName.includes('/')) {
|
|
subDirectoryName = subDirectoryName.split('/')[0];
|
|
}
|
|
|
|
return {
|
|
name: subDirectoryName,
|
|
path:
|
|
name === '/'
|
|
? subDirectoryName
|
|
: this._normalizePath(posixPath.join(name, subDirectoryName)),
|
|
type: 'dir',
|
|
};
|
|
}
|
|
|
|
return null;
|
|
})
|
|
// remove nulls
|
|
.filter((stat): stat is DetectorFilesystemStat => stat !== null)
|
|
// remove duplicates
|
|
.filter(
|
|
(stat, index, self) =>
|
|
index ===
|
|
self.findIndex(s => s.name === stat.name && s.path === stat.path)
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* An example of how to implement chdir for a virtual filesystem.
|
|
*/
|
|
_chdir(name: string): DetectorFilesystem {
|
|
const basePath = this._normalizePath(posixPath.join(this.cwd, name));
|
|
const files = Object.fromEntries(
|
|
[...this.files.keys()].map(key => [key, this.files.get(key) ?? ''])
|
|
);
|
|
|
|
return new VirtualFilesystem(files, basePath);
|
|
}
|
|
}
|