-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): add e2e project generator
- Loading branch information
Showing
26 changed files
with
444 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
checkFilesDoNotExist, | ||
cleanupProject, | ||
killPort, | ||
newProject, | ||
promisifiedTreeKill, | ||
runCLI, | ||
runCommandUntil, | ||
uniq, | ||
} from '@nrwl/e2e/utils'; | ||
import { p } from 'vitest/dist/index-5aad25c1'; | ||
|
||
describe('Node Applications + webpack', () => { | ||
beforeEach(() => newProject()); | ||
|
||
afterEach(() => cleanupProject()); | ||
|
||
async function runE2eTests(appName: string) { | ||
process.env.PORT = '5000'; | ||
const childProcess = await runCommandUntil(`serve ${appName}`, (output) => { | ||
return output.includes('http://localhost:5000'); | ||
}); | ||
const result = runCLI(`e2e ${appName}-e2e`); | ||
expect(result).toContain('Setting up...'); | ||
expect(result).toContain('Tearing down..'); | ||
expect(result).toContain('Successfully ran target e2e'); | ||
|
||
await promisifiedTreeKill(childProcess.pid, 'SIGKILL'); | ||
await killPort(5000); | ||
process.env.PORT = ''; | ||
} | ||
|
||
it('should generate an app using webpack', async () => { | ||
const expressApp = uniq('expressapp'); | ||
const fastifyApp = uniq('fastifyapp'); | ||
const koaApp = uniq('koaapp'); | ||
|
||
runCLI( | ||
`generate @nrwl/node:app ${expressApp} --framework=express --no-interactive` | ||
); | ||
runCLI( | ||
`generate @nrwl/node:app ${fastifyApp} --framework=fastify --no-interactive` | ||
); | ||
runCLI( | ||
`generate @nrwl/node:app ${koaApp} --framework=koa --no-interactive` | ||
); | ||
|
||
// Use esbuild by default | ||
checkFilesDoNotExist(`apps/${expressApp}/webpack.config.js`); | ||
checkFilesDoNotExist(`apps/${fastifyApp}/webpack.config.js`); | ||
checkFilesDoNotExist(`apps/${koaApp}/webpack.config.js`); | ||
|
||
expect(() => runCLI(`lint ${expressApp}`)).not.toThrow(); | ||
expect(() => runCLI(`lint ${fastifyApp}`)).not.toThrow(); | ||
expect(() => runCLI(`lint ${koaApp}`)).not.toThrow(); | ||
expect(() => runCLI(`lint ${expressApp}-e2e`)).not.toThrow(); | ||
expect(() => runCLI(`lint ${fastifyApp}-e2e`)).not.toThrow(); | ||
expect(() => runCLI(`lint ${koaApp}-e2e`)).not.toThrow(); | ||
|
||
await runE2eTests(expressApp); | ||
await runE2eTests(fastifyApp); | ||
await runE2eTests(koaApp); | ||
}, 300_000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
packages/node/src/generators/application/files/connect/src/main.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import connect from 'connect'; | ||
|
||
const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>; | ||
|
||
const app = connect(); | ||
|
||
app.use((req, res) => { | ||
res.end(JSON.stringify({ 'message': 'Hello API' })); | ||
}); | ||
|
||
app.listen(<%= port %>, () => { | ||
// Server is running | ||
}) | ||
app.listen(port, () => { | ||
console.log(`[ ready ] http://localhost:${port}`); | ||
}) |
10 changes: 6 additions & 4 deletions
10
packages/node/src/generators/application/files/express/src/main.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import express from 'express'; | ||
const app = express(); | ||
|
||
const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>; | ||
|
||
const app = express(); | ||
|
||
app.get('/', (req, res) => { | ||
res.send({ 'message': 'Hello API'}); | ||
}); | ||
|
||
app.listen(<%= port %>, () => { | ||
// Server is running | ||
}); | ||
app.listen(port, () => { | ||
console.log(`[ ready ] http://localhost:${port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
packages/node/src/generators/application/files/koa/src/main.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import koa from 'koa'; | ||
|
||
const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>; | ||
|
||
const app = new koa(); | ||
|
||
app.use(async ctx =>{ | ||
ctx.body = { 'message': 'Hello API'}; | ||
}); | ||
|
||
app.listen(<%= port %>, () => { | ||
// Server is running | ||
}); | ||
app.listen(port, () => { | ||
console.log(`[ ready ] http://localhost:${port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/node/src/generators/e2e-project/e2e-project.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import * as path from 'path'; | ||
import { | ||
addDependenciesToPackageJson, | ||
addProjectConfiguration, | ||
convertNxGenerator, | ||
extractLayoutDirectory, | ||
formatFiles, | ||
generateFiles, | ||
GeneratorCallback, | ||
getWorkspaceLayout, | ||
joinPathFragments, | ||
names, | ||
offsetFromRoot, | ||
readProjectConfiguration, | ||
Tree, | ||
} from '@nrwl/devkit'; | ||
import { Linter, lintProjectGenerator } from '@nrwl/linter'; | ||
|
||
import { Schema } from './schema'; | ||
import { axiosVersion } from '../../utils/versions'; | ||
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; | ||
|
||
export async function e2eProjectGenerator(host: Tree, _options: Schema) { | ||
const tasks: GeneratorCallback[] = []; | ||
const options = normalizeOptions(host, _options); | ||
const appProject = readProjectConfiguration(host, options.project); | ||
|
||
addProjectConfiguration(host, options.projectName, { | ||
root: options.projectRoot, | ||
targets: { | ||
e2e: { | ||
executor: '@nrwl/jest:jest', | ||
outputs: ['{workspaceRoot}/coverage/{projectRoot}'], | ||
options: { | ||
jestConfig: `${options.projectRoot}/jest.config.ts`, | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (options.projectType === 'server') { | ||
generateFiles( | ||
host, | ||
path.join(__dirname, 'files/server'), | ||
options.projectRoot, | ||
{ | ||
...options, | ||
...names(options.projectName), | ||
offsetFromRoot: offsetFromRoot(options.projectRoot), | ||
tmpl: '', | ||
} | ||
); | ||
} else if (options.projectType === 'cli') { | ||
const mainFile = appProject.targets.build?.options?.outputPath; | ||
generateFiles( | ||
host, | ||
path.join(__dirname, 'files/cli'), | ||
options.projectRoot, | ||
{ | ||
...options, | ||
...names(options.projectName), | ||
mainFile, | ||
offsetFromRoot: offsetFromRoot(options.projectRoot), | ||
tmpl: '', | ||
} | ||
); | ||
} | ||
|
||
// axios is more than likely used in the application code, so install it as a regular dependency. | ||
const installTask = addDependenciesToPackageJson( | ||
host, | ||
{ axios: axiosVersion }, | ||
{} | ||
); | ||
tasks.push(installTask); | ||
|
||
if (options.linter === 'eslint') { | ||
const linterTask = await lintProjectGenerator(host, { | ||
project: options.projectName, | ||
linter: Linter.EsLint, | ||
skipFormat: true, | ||
tsConfigPaths: [joinPathFragments(options.projectRoot, 'tsconfig.json')], | ||
eslintFilePatterns: [`${options.projectRoot}/**/*.{js,ts}`], | ||
setParserOptionsProject: false, | ||
skipPackageJson: false, | ||
rootProject: options.rootProject, | ||
}); | ||
tasks.push(linterTask); | ||
} | ||
|
||
await formatFiles(host); | ||
|
||
return runTasksInSerial(...tasks); | ||
} | ||
|
||
function normalizeOptions( | ||
tree: Tree, | ||
options: Schema | ||
): Omit<Schema, 'name'> & { projectRoot: string; projectName: string } { | ||
const { layoutDirectory, projectDirectory } = extractLayoutDirectory( | ||
options.directory | ||
); | ||
const appsDir = layoutDirectory ?? getWorkspaceLayout(tree).appsDir; | ||
const name = options.name ?? `${options.project}-e2e`; | ||
|
||
const appDirectory = projectDirectory | ||
? `${names(projectDirectory).fileName}/${names(name).fileName}` | ||
: names(name).fileName; | ||
|
||
const projectName = appDirectory.replace(new RegExp('/', 'g'), '-'); | ||
|
||
const projectRoot = options.rootProject | ||
? '.' | ||
: joinPathFragments(appsDir, appDirectory); | ||
|
||
return { ...options, projectRoot, projectName, port: options.port ?? 3000 }; | ||
} | ||
|
||
export default e2eProjectGenerator; | ||
export const e2eProjectSchematic = convertNxGenerator(e2eProjectGenerator); |
17 changes: 17 additions & 0 deletions
17
packages/node/src/generators/e2e-project/files/cli/jest.config.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: '<%= projectName %>', | ||
preset: '<%= offsetFromRoot %>/jest.preset.js', | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: '<rootDir>/tsconfig.spec.json', | ||
}, | ||
}, | ||
setupFiles: ['<rootDir>/src/test-setup.ts'], | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': 'ts-jest', | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '<%= offsetFromRoot %>/coverage/<%= projectName %>', | ||
}; |
Oops, something went wrong.