From ad7cf4a06df250fb72e8b1a639f270ecfcf3fa59 Mon Sep 17 00:00:00 2001 From: QuiiBz Date: Sun, 3 Jul 2022 12:09:41 +0200 Subject: [PATCH 1/3] fix(next): dev server starting when importing a file using get-project-dir --- packages/next/bin/next.ts | 13 +------------ packages/next/cli/next-build.ts | 2 +- packages/next/cli/next-dev.ts | 2 +- packages/next/cli/next-export.ts | 2 +- packages/next/cli/next-info.ts | 2 +- packages/next/cli/next-lint.ts | 2 +- packages/next/cli/next-start.ts | 2 +- packages/next/cli/next-telemetry.ts | 2 +- packages/next/lib/commands.ts | 12 ++++++++++++ packages/next/lib/get-project-dir.ts | 2 +- 10 files changed, 21 insertions(+), 20 deletions(-) create mode 100644 packages/next/lib/commands.ts diff --git a/packages/next/bin/next.ts b/packages/next/bin/next.ts index dd467cb224a5..ca871c202d9f 100755 --- a/packages/next/bin/next.ts +++ b/packages/next/bin/next.ts @@ -3,6 +3,7 @@ import * as log from '../build/output/log' import arg from 'next/dist/compiled/arg/index.js' import { NON_STANDARD_NODE_ENV } from '../lib/constants' import { shouldUseReactRoot } from '../server/utils' +import { commands } from '../lib/commands'; ;['react', 'react-dom'].forEach((dependency) => { try { // When 'npm link' is used it checks the clone location. Not the project. @@ -15,18 +16,6 @@ import { shouldUseReactRoot } from '../server/utils' }) const defaultCommand = 'dev' -export type cliCommand = (argv?: string[]) => void -export const commands: { [command: string]: () => Promise } = { - build: () => Promise.resolve(require('../cli/next-build').nextBuild), - start: () => Promise.resolve(require('../cli/next-start').nextStart), - export: () => Promise.resolve(require('../cli/next-export').nextExport), - dev: () => Promise.resolve(require('../cli/next-dev').nextDev), - lint: () => Promise.resolve(require('../cli/next-lint').nextLint), - telemetry: () => - Promise.resolve(require('../cli/next-telemetry').nextTelemetry), - info: () => Promise.resolve(require('../cli/next-info').nextInfo), -} - const args = arg( { // Types diff --git a/packages/next/cli/next-build.ts b/packages/next/cli/next-build.ts index af5ae2e51feb..d29852939bed 100755 --- a/packages/next/cli/next-build.ts +++ b/packages/next/cli/next-build.ts @@ -2,7 +2,7 @@ import { existsSync } from 'fs' import arg from 'next/dist/compiled/arg/index.js' import * as Log from '../build/output/log' -import { cliCommand } from '../bin/next' +import { cliCommand } from '../lib/commands' import build from '../build' import { printAndExit } from '../server/lib/utils' import isError from '../lib/is-error' diff --git a/packages/next/cli/next-dev.ts b/packages/next/cli/next-dev.ts index 5e0c22bb7f0b..9048fd028aed 100755 --- a/packages/next/cli/next-dev.ts +++ b/packages/next/cli/next-dev.ts @@ -5,7 +5,7 @@ import { startServer } from '../server/lib/start-server' import { printAndExit } from '../server/lib/utils' import * as Log from '../build/output/log' import { startedDevelopmentServer } from '../build/output' -import { cliCommand } from '../bin/next' +import { cliCommand } from '../lib/commands' import isError from '../lib/is-error' import { getProjectDir } from '../lib/get-project-dir' import { CONFIG_FILES } from '../shared/lib/constants' diff --git a/packages/next/cli/next-export.ts b/packages/next/cli/next-export.ts index 151108bfcf79..9a5282c93a8e 100755 --- a/packages/next/cli/next-export.ts +++ b/packages/next/cli/next-export.ts @@ -4,7 +4,7 @@ import { existsSync } from 'fs' import arg from 'next/dist/compiled/arg/index.js' import exportApp from '../export' import { printAndExit } from '../server/lib/utils' -import { cliCommand } from '../bin/next' +import { cliCommand } from '../lib/commands' import { trace } from '../trace' import isError from '../lib/is-error' import { getProjectDir } from '../lib/get-project-dir' diff --git a/packages/next/cli/next-info.ts b/packages/next/cli/next-info.ts index 0a7b9673bac1..c9c468b26ff5 100755 --- a/packages/next/cli/next-info.ts +++ b/packages/next/cli/next-info.ts @@ -6,7 +6,7 @@ import chalk from 'next/dist/compiled/chalk' import arg from 'next/dist/compiled/arg/index.js' import fetch from 'next/dist/compiled/node-fetch' import { printAndExit } from '../server/lib/utils' -import { cliCommand } from '../bin/next' +import { cliCommand } from '../lib/commands' import isError from '../lib/is-error' const nextInfo: cliCommand = async (argv) => { diff --git a/packages/next/cli/next-lint.ts b/packages/next/cli/next-lint.ts index 439805dcfd3d..699c586e5e0b 100755 --- a/packages/next/cli/next-lint.ts +++ b/packages/next/cli/next-lint.ts @@ -4,7 +4,7 @@ import arg from 'next/dist/compiled/arg/index.js' import { join } from 'path' import chalk from 'next/dist/compiled/chalk' -import { cliCommand } from '../bin/next' +import { cliCommand } from '../lib/commands' import { ESLINT_DEFAULT_DIRS } from '../lib/constants' import { runLintCheck } from '../lib/eslint/runLintCheck' import { printAndExit } from '../server/lib/utils' diff --git a/packages/next/cli/next-start.ts b/packages/next/cli/next-start.ts index 02bb29fa903e..fc7cfe39369b 100755 --- a/packages/next/cli/next-start.ts +++ b/packages/next/cli/next-start.ts @@ -3,10 +3,10 @@ import arg from 'next/dist/compiled/arg/index.js' import { startServer } from '../server/lib/start-server' import { printAndExit } from '../server/lib/utils' -import { cliCommand } from '../bin/next' import * as Log from '../build/output/log' import isError from '../lib/is-error' import { getProjectDir } from '../lib/get-project-dir' +import { cliCommand } from '../lib/commands' const nextStart: cliCommand = (argv) => { const validArgs: arg.Spec = { diff --git a/packages/next/cli/next-telemetry.ts b/packages/next/cli/next-telemetry.ts index 34f6daccc84d..2df798e3ce4e 100755 --- a/packages/next/cli/next-telemetry.ts +++ b/packages/next/cli/next-telemetry.ts @@ -2,7 +2,7 @@ import chalk from 'next/dist/compiled/chalk' import arg from 'next/dist/compiled/arg/index.js' import { printAndExit } from '../server/lib/utils' -import { cliCommand } from '../bin/next' +import { cliCommand } from '../lib/commands' import { Telemetry } from '../telemetry/storage' import isError from '../lib/is-error' diff --git a/packages/next/lib/commands.ts b/packages/next/lib/commands.ts new file mode 100644 index 000000000000..927b537913fe --- /dev/null +++ b/packages/next/lib/commands.ts @@ -0,0 +1,12 @@ +export type cliCommand = (argv?: string[]) => void + +export const commands: { [command: string]: () => Promise } = { + build: () => Promise.resolve(require('../cli/next-build').nextBuild), + start: () => Promise.resolve(require('../cli/next-start').nextStart), + export: () => Promise.resolve(require('../cli/next-export').nextExport), + dev: () => Promise.resolve(require('../cli/next-dev').nextDev), + lint: () => Promise.resolve(require('../cli/next-lint').nextLint), + telemetry: () => + Promise.resolve(require('../cli/next-telemetry').nextTelemetry), + info: () => Promise.resolve(require('../cli/next-info').nextInfo), +} \ No newline at end of file diff --git a/packages/next/lib/get-project-dir.ts b/packages/next/lib/get-project-dir.ts index f132c846d0ce..a64aae5f9c3e 100644 --- a/packages/next/lib/get-project-dir.ts +++ b/packages/next/lib/get-project-dir.ts @@ -1,6 +1,6 @@ import fs from 'fs' import path from 'path' -import { commands } from '../bin/next' +import { commands } from './commands' import * as Log from '../build/output/log' import { detectTypo } from './detect-typo' From f2e81b3f3ddd010370ece6905517862875ce9200 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Sun, 7 Aug 2022 11:56:51 -0500 Subject: [PATCH 2/3] Add test case for get-project-dir --- test/unit/get-project-dir.test.ts | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 test/unit/get-project-dir.test.ts diff --git a/test/unit/get-project-dir.test.ts b/test/unit/get-project-dir.test.ts new file mode 100644 index 000000000000..3953a0e4075e --- /dev/null +++ b/test/unit/get-project-dir.test.ts @@ -0,0 +1,5 @@ +describe('get-project-dir', () => { + it('should not start dev server on require', async () => { + require('next/dist/lib/get-project-dir') + }) +}) From 220083c74bcda2e1178c7dc740d6c883059dadff Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Sun, 7 Aug 2022 12:05:05 -0500 Subject: [PATCH 3/3] lint-fix --- packages/next/bin/next.ts | 2 +- packages/next/lib/commands.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/bin/next.ts b/packages/next/bin/next.ts index 02a67b97c135..cbb26a0be798 100755 --- a/packages/next/bin/next.ts +++ b/packages/next/bin/next.ts @@ -2,7 +2,7 @@ import * as log from '../build/output/log' import arg from 'next/dist/compiled/arg/index.js' import { NON_STANDARD_NODE_ENV } from '../lib/constants' -import { commands } from '../lib/commands'; +import { commands } from '../lib/commands' ;['react', 'react-dom'].forEach((dependency) => { try { // When 'npm link' is used it checks the clone location. Not the project. diff --git a/packages/next/lib/commands.ts b/packages/next/lib/commands.ts index 927b537913fe..63a41430f856 100644 --- a/packages/next/lib/commands.ts +++ b/packages/next/lib/commands.ts @@ -9,4 +9,4 @@ export const commands: { [command: string]: () => Promise } = { telemetry: () => Promise.resolve(require('../cli/next-telemetry').nextTelemetry), info: () => Promise.resolve(require('../cli/next-info').nextInfo), -} \ No newline at end of file +}