Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(application): add option: platform #1610

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
190 changes: 190 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"test:dev": "NODE_ENV=test npm run -s test -- --watchAll",
"prerelease": "npm run build",
"release": "release-it",
"prepare": "husky install"
"prepare": "husky install",
"create-app":"npm run build && schematics .:application my-project --directory=dist/my-project --dry-run=false"
},
"repository": {
"type": "git",
Expand All @@ -43,6 +44,7 @@
"dependencies": {
"@angular-devkit/core": "16.2.8",
"@angular-devkit/schematics": "16.2.8",
"@schematics/angular": "^17.0.1",
"comment-json": "4.2.3",
"jsonc-parser": "3.2.0",
"pluralize": "8.0.0"
Expand Down
46 changes: 40 additions & 6 deletions src/lib/application/application.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import {
import * as path from 'path';
import { ApplicationOptions } from './application.schema';

let runner: SchematicTestRunner;

beforeAll(()=>{
runner = new SchematicTestRunner(
'.',
path.join(process.cwd(), 'src/collection.json')
);
})

describe('Application Factory', () => {
const runner: SchematicTestRunner = new SchematicTestRunner(
'.',
path.join(process.cwd(), 'src/collection.json'),
);
describe('when only the name is supplied', () => {
it('should manage basic (ie., cross-platform) name', async () => {
const options: ApplicationOptions = {
name: 'project',
};
name: 'project'
}

const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
Expand Down Expand Up @@ -452,3 +458,31 @@ describe('Application Factory', () => {
].sort());
});
});


describe('providing the platform option',() =>{
test('should contain the nessasery fastifay adaptors',(done)=>{
const options: ApplicationOptions = {
name: 'project',
platform: 'fastify',
install: 'false'
};
runner.runSchematic('application', options).then(tree=>{

// add FastifyAdapter() to NestFactory.create()
expect(tree.read('/project/src/main.ts')?.toString('utf8'))
.toMatch(/NestFactory.create<\s*NestFastifyApplication\s*>\s*\(\s*AppModule\s*,\s*new\s+FastifyAdapter\(\s*\)\s*\)/)

// import from '@nestjs/platform-fastify'
expect(tree.read('/project/src/main.ts')?.toString('utf8'))
.toMatch(/import\s*{\s*FastifyAdapter\s*,\s*NestFastifyApplication\s*}\s*from\s*['"]@nestjs\/platform-fastify['"]/)

// add fastify dependencies to package.json
expect(tree.read('/project/package.json')?.toString()).toContain('"@nestjs/platform-fastify"');
expect(tree.read('/project/package.json')?.toString()).not.toContain('"@nestjs/platform-express"');

done();
})
.catch((error) => done(error));
})
})
45 changes: 43 additions & 2 deletions src/lib/application/application.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
Source,
template,
url,
chain,
SchematicContext
} from '@angular-devkit/schematics';
import { basename, parse } from 'path';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
Expand All @@ -19,6 +21,11 @@ import {
DEFAULT_VERSION,
} from '../defaults';
import { ApplicationOptions } from './application.schema';
import { addPackageJsonDependency, NodeDependencyType } from '../../utils/dependencies.utils';
import {
removePackageJsonDependency,
} from '@schematics/angular/utility/dependencies';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';

export function main(options: ApplicationOptions): Rule {
options.name = normalizeToKebabOrSnakeCase(options.name.toString());
Expand All @@ -27,9 +34,43 @@ export function main(options: ApplicationOptions): Rule {
!options.directory || options.directory === 'undefined'
? options.name
: options.directory;
options.platform = <'fastify' | 'express'>options.platform?.toLowerCase() || "express"

return chain([
()=>{
options = transform(options);
return mergeWith(generate(options, path));
},
(tree)=>{
if(options.platform.toLowerCase()==='fastify'){
const pkgPath = `${path}/package.json`;
['@nestjs/platform-fastify', 'fastify'].map((el) =>
addPackageJsonDependency(
tree,
{
type: NodeDependencyType.Default,
name: el,
// todo: add compatible versions
version: '',
overwrite: false,
},
pkgPath
)
);

['@nestjs/platform-express', '@types/express'].map((el) =>
removePackageJsonDependency(tree, el, pkgPath)
);
}
return tree
},
(tree, ctx:SchematicContext)=>{
options.install!=="false" && ctx.addTask(new NodePackageInstallTask());
}

])


options = transform(options);
return mergeWith(generate(options, path));
}

function transform(options: ApplicationOptions): ApplicationOptions {
Expand Down
13 changes: 13 additions & 0 deletions src/lib/application/application.schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ export interface ApplicationOptions {
* @default "spec"
*/
specFileSuffix?: string;


/**
* the underlying http platform
* @default "express"
*/
platform?: 'fastify' | 'express';

/**
* wether to install the dependencies
* @default true
*/
install?: 'true' | 'false'
}
9 changes: 8 additions & 1 deletion src/lib/application/files/ts/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
<% if(platform==="fastify"){ %>
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify'
<% } %>

async function bootstrap() {
const app = await NestFactory.create(AppModule);
<% if(platform==="fastify"){ %>
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
<% } else { %>
const app = await NestFactory.create(AppModule);
<% } %>
await app.listen(3000);
}
bootstrap();