mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-07 21:07:46 +00:00
[now-build-utils] Fix now dev to use system node (#3509)
This PR will use the system installed version of Node.js and avoid printing a warning or error if a discontinued version is selected. This optimization was already in `@now/node` but for some reason it was never add to `@now/next`. The reason why its relevant today is because the warnings turned into errors due to Node 8 deprecation and we don't have the "Project" in `now dev` so we don't know which version of node to select. So instead of determining the version, `now dev` will always use `node` in the PATH and avoid printing warnings or errors. This also results in less FS reads since we no longer need to read package.json.
This commit is contained in:
@@ -7,7 +7,7 @@ import { SpawnOptions } from 'child_process';
|
|||||||
import { deprecate } from 'util';
|
import { deprecate } from 'util';
|
||||||
import { cpus } from 'os';
|
import { cpus } from 'os';
|
||||||
import { Meta, PackageJson, NodeVersion, Config } from '../types';
|
import { Meta, PackageJson, NodeVersion, Config } from '../types';
|
||||||
import { getSupportedNodeVersion } from './node-version';
|
import { getSupportedNodeVersion, getLatestNodeVersion } from './node-version';
|
||||||
|
|
||||||
export function spawnAsync(
|
export function spawnAsync(
|
||||||
command: string,
|
command: string,
|
||||||
@@ -140,8 +140,14 @@ export function getSpawnOptions(
|
|||||||
export async function getNodeVersion(
|
export async function getNodeVersion(
|
||||||
destPath: string,
|
destPath: string,
|
||||||
minNodeVersion?: string,
|
minNodeVersion?: string,
|
||||||
config?: Config
|
config?: Config,
|
||||||
|
meta?: Meta
|
||||||
): Promise<NodeVersion> {
|
): Promise<NodeVersion> {
|
||||||
|
if (meta && meta.isDev) {
|
||||||
|
// Use the system-installed version of `node` in PATH for `now dev`
|
||||||
|
const latest = getLatestNodeVersion();
|
||||||
|
return { ...latest, runtime: 'nodejs' };
|
||||||
|
}
|
||||||
const { packageJson } = await scanParentDirs(destPath, true);
|
const { packageJson } = await scanParentDirs(destPath, true);
|
||||||
let range: string | undefined;
|
let range: string | undefined;
|
||||||
let isAuto = false;
|
let isAuto = false;
|
||||||
|
|||||||
11
packages/now-build-utils/test/unit.test.js
vendored
11
packages/now-build-utils/test/unit.test.js
vendored
@@ -104,6 +104,17 @@ it('should select correct node version in getNodeVersion()', async () => {
|
|||||||
).toHaveProperty('major', 10);
|
).toHaveProperty('major', 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should ignore node version in now dev getNodeVersion()', async () => {
|
||||||
|
expect(
|
||||||
|
await getNodeVersion(
|
||||||
|
'/tmp',
|
||||||
|
undefined,
|
||||||
|
{ nodeVersion: '1' },
|
||||||
|
{ isDev: true }
|
||||||
|
)
|
||||||
|
).toHaveProperty('runtime', 'nodejs');
|
||||||
|
});
|
||||||
|
|
||||||
it('should get latest node version', async () => {
|
it('should get latest node version', async () => {
|
||||||
expect(await getLatestNodeVersion()).toHaveProperty('major', 12);
|
expect(await getLatestNodeVersion()).toHaveProperty('major', 12);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ export const build = async ({
|
|||||||
const pkg = await readPackageJson(entryPath);
|
const pkg = await readPackageJson(entryPath);
|
||||||
const nextVersion = getNextVersion(pkg);
|
const nextVersion = getNextVersion(pkg);
|
||||||
|
|
||||||
const nodeVersion = await getNodeVersion(entryPath, undefined, config);
|
const nodeVersion = await getNodeVersion(entryPath, undefined, config, meta);
|
||||||
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
||||||
|
|
||||||
if (!nextVersion) {
|
if (!nextVersion) {
|
||||||
|
|||||||
@@ -71,7 +71,8 @@ async function downloadInstallAndBundle({
|
|||||||
const nodeVersion = await getNodeVersion(
|
const nodeVersion = await getNodeVersion(
|
||||||
entrypointFsDirname,
|
entrypointFsDirname,
|
||||||
undefined,
|
undefined,
|
||||||
config
|
config,
|
||||||
|
meta
|
||||||
);
|
);
|
||||||
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
||||||
await runNpmInstall(
|
await runNpmInstall(
|
||||||
@@ -369,16 +370,13 @@ export async function build({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the system-installed version of `node` when running via `now dev`
|
|
||||||
const runtime = meta.isDev ? 'nodejs' : nodeVersion.runtime;
|
|
||||||
|
|
||||||
const lambda = await createLambda({
|
const lambda = await createLambda({
|
||||||
files: {
|
files: {
|
||||||
...preparedFiles,
|
...preparedFiles,
|
||||||
...launcherFiles,
|
...launcherFiles,
|
||||||
},
|
},
|
||||||
handler: `${LAUNCHER_FILENAME}.launcher`,
|
handler: `${LAUNCHER_FILENAME}.launcher`,
|
||||||
runtime,
|
runtime: nodeVersion.runtime,
|
||||||
});
|
});
|
||||||
|
|
||||||
return { output: lambda, watch };
|
return { output: lambda, watch };
|
||||||
|
|||||||
@@ -300,7 +300,8 @@ export async function build({
|
|||||||
const nodeVersion = await getNodeVersion(
|
const nodeVersion = await getNodeVersion(
|
||||||
entrypointDir,
|
entrypointDir,
|
||||||
minNodeRange,
|
minNodeRange,
|
||||||
config
|
config,
|
||||||
|
meta
|
||||||
);
|
);
|
||||||
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
||||||
|
|
||||||
@@ -453,7 +454,12 @@ export async function build({
|
|||||||
|
|
||||||
if (!config.zeroConfig && entrypoint.endsWith('.sh')) {
|
if (!config.zeroConfig && entrypoint.endsWith('.sh')) {
|
||||||
debug(`Running build script "${entrypoint}"`);
|
debug(`Running build script "${entrypoint}"`);
|
||||||
const nodeVersion = await getNodeVersion(entrypointDir, undefined, config);
|
const nodeVersion = await getNodeVersion(
|
||||||
|
entrypointDir,
|
||||||
|
undefined,
|
||||||
|
config,
|
||||||
|
meta
|
||||||
|
);
|
||||||
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
||||||
await runShellScript(path.join(workPath, entrypoint), [], spawnOpts);
|
await runShellScript(path.join(workPath, entrypoint), [], spawnOpts);
|
||||||
validateDistDir(distPath, meta.isDev, config);
|
validateDistDir(distPath, meta.isDev, config);
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
const getWritableDirectory = require('../../packages/now-build-utils/fs/get-writable-directory.js');
|
const {
|
||||||
const glob = require('../../packages/now-build-utils/fs/glob.js');
|
getLatestNodeVersion,
|
||||||
|
glob,
|
||||||
|
getWriteableDirectory,
|
||||||
|
} = require('@now/build-utils');
|
||||||
|
|
||||||
function runAnalyze(wrapper, context) {
|
function runAnalyze(wrapper, context) {
|
||||||
if (wrapper.analyze) {
|
if (wrapper.analyze) {
|
||||||
@@ -16,6 +19,11 @@ async function runBuildLambda(inputPath) {
|
|||||||
const nowJson = require(nowJsonRef.fsPath);
|
const nowJson = require(nowJsonRef.fsPath);
|
||||||
expect(nowJson.builds.length).toBe(1);
|
expect(nowJson.builds.length).toBe(1);
|
||||||
const build = nowJson.builds[0];
|
const build = nowJson.builds[0];
|
||||||
|
if (!build.config || !build.config.nodeVersion) {
|
||||||
|
// Mimic api-deployments when a new project is created
|
||||||
|
const nodeVersion = getLatestNodeVersion().range;
|
||||||
|
build.config = { ...build.config, nodeVersion };
|
||||||
|
}
|
||||||
expect(build.src.includes('*')).toBeFalsy();
|
expect(build.src.includes('*')).toBeFalsy();
|
||||||
const entrypoint = build.src.replace(/^\//, ''); // strip leftmost slash
|
const entrypoint = build.src.replace(/^\//, ''); // strip leftmost slash
|
||||||
expect(inputFiles[entrypoint]).toBeDefined();
|
expect(inputFiles[entrypoint]).toBeDefined();
|
||||||
@@ -26,15 +34,15 @@ async function runBuildLambda(inputPath) {
|
|||||||
const analyzeResult = runAnalyze(wrapper, {
|
const analyzeResult = runAnalyze(wrapper, {
|
||||||
files: inputFiles,
|
files: inputFiles,
|
||||||
entrypoint,
|
entrypoint,
|
||||||
config: build.config
|
config: build.config,
|
||||||
});
|
});
|
||||||
|
|
||||||
const workPath = await getWritableDirectory();
|
const workPath = await getWriteableDirectory();
|
||||||
const buildResult = await wrapper.build({
|
const buildResult = await wrapper.build({
|
||||||
files: inputFiles,
|
files: inputFiles,
|
||||||
entrypoint,
|
entrypoint,
|
||||||
config: build.config,
|
config: build.config,
|
||||||
workPath
|
workPath,
|
||||||
});
|
});
|
||||||
const { output } = buildResult;
|
const { output } = buildResult;
|
||||||
|
|
||||||
@@ -43,7 +51,7 @@ async function runBuildLambda(inputPath) {
|
|||||||
buildResult.output = Object.keys(output).reduce(
|
buildResult.output = Object.keys(output).reduce(
|
||||||
(result, path) => ({
|
(result, path) => ({
|
||||||
...result,
|
...result,
|
||||||
[path.replace(/\\/g, '/')]: output[path]
|
[path.replace(/\\/g, '/')]: output[path],
|
||||||
}),
|
}),
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
@@ -52,7 +60,7 @@ async function runBuildLambda(inputPath) {
|
|||||||
return {
|
return {
|
||||||
analyzeResult,
|
analyzeResult,
|
||||||
buildResult,
|
buildResult,
|
||||||
workPath
|
workPath,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user