Skip to content
Permalink
Browse files

[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.
  • Loading branch information
styfle committed Jan 6, 2020
1 parent 0b88c15 commit ecb0c08fe2b73fd489da2ee4f417c160de755b60
@@ -7,7 +7,7 @@ import { SpawnOptions } from 'child_process';
import { deprecate } from 'util';
import { cpus } from 'os';
import { Meta, PackageJson, NodeVersion, Config } from '../types';
import { getSupportedNodeVersion } from './node-version';
import { getSupportedNodeVersion, getLatestNodeVersion } from './node-version';

export function spawnAsync(
command: string,
@@ -140,8 +140,14 @@ export function getSpawnOptions(
export async function getNodeVersion(
destPath: string,
minNodeVersion?: string,
config?: Config
config?: Config,
meta?: Meta
): 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);
let range: string | undefined;
let isAuto = false;
@@ -104,6 +104,17 @@ it('should select correct node version in getNodeVersion()', async () => {
).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 () => {
expect(await getLatestNodeVersion()).toHaveProperty('major', 12);
});
@@ -202,7 +202,7 @@ export const build = async ({
const pkg = await readPackageJson(entryPath);
const nextVersion = getNextVersion(pkg);

const nodeVersion = await getNodeVersion(entryPath, undefined, config);
const nodeVersion = await getNodeVersion(entryPath, undefined, config, meta);
const spawnOpts = getSpawnOptions(meta, nodeVersion);

if (!nextVersion) {
@@ -71,7 +71,8 @@ async function downloadInstallAndBundle({
const nodeVersion = await getNodeVersion(
entrypointFsDirname,
undefined,
config
config,
meta
);
const spawnOpts = getSpawnOptions(meta, nodeVersion);
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({
files: {
...preparedFiles,
...launcherFiles,
},
handler: `${LAUNCHER_FILENAME}.launcher`,
runtime,
runtime: nodeVersion.runtime,
});

return { output: lambda, watch };
@@ -300,7 +300,8 @@ export async function build({
const nodeVersion = await getNodeVersion(
entrypointDir,
minNodeRange,
config
config,
meta
);
const spawnOpts = getSpawnOptions(meta, nodeVersion);

@@ -453,7 +454,12 @@ export async function build({

if (!config.zeroConfig && entrypoint.endsWith('.sh')) {
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);
await runShellScript(path.join(workPath, entrypoint), [], spawnOpts);
validateDistDir(distPath, meta.isDev, config);
@@ -1,5 +1,8 @@
const getWritableDirectory = require('../../packages/now-build-utils/fs/get-writable-directory.js');
const glob = require('../../packages/now-build-utils/fs/glob.js');
const {
getLatestNodeVersion,
glob,
getWriteableDirectory,
} = require('@now/build-utils');

function runAnalyze(wrapper, context) {
if (wrapper.analyze) {
@@ -16,6 +19,11 @@ async function runBuildLambda(inputPath) {
const nowJson = require(nowJsonRef.fsPath);
expect(nowJson.builds.length).toBe(1);
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();
const entrypoint = build.src.replace(/^\//, ''); // strip leftmost slash
expect(inputFiles[entrypoint]).toBeDefined();
@@ -26,15 +34,15 @@ async function runBuildLambda(inputPath) {
const analyzeResult = runAnalyze(wrapper, {
files: inputFiles,
entrypoint,
config: build.config
config: build.config,
});

const workPath = await getWritableDirectory();
const workPath = await getWriteableDirectory();
const buildResult = await wrapper.build({
files: inputFiles,
entrypoint,
config: build.config,
workPath
workPath,
});
const { output } = buildResult;

@@ -43,7 +51,7 @@ async function runBuildLambda(inputPath) {
buildResult.output = Object.keys(output).reduce(
(result, path) => ({
...result,
[path.replace(/\\/g, '/')]: output[path]
[path.replace(/\\/g, '/')]: output[path],
}),
{}
);
@@ -52,7 +60,7 @@ async function runBuildLambda(inputPath) {
return {
analyzeResult,
buildResult,
workPath
workPath,
};
}

0 comments on commit ecb0c08

Please sign in to comment.
You can’t perform that action at this time.