Skip to content

Commit 91e64fa

Browse files
committed
Create option httpFramework in project generator
1 parent a310925 commit 91e64fa

4 files changed

Lines changed: 51 additions & 12 deletions

File tree

src/generators/ProjectGenerator.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import path from 'path';
33
import commander from 'commander';
44
import inquirer from 'inquirer';
55

6+
import { replaceMaskFile } from '../utils/replaceMask';
67
import copyFolder from '../utils/copyFolder';
78
import createPackageJson from '../stages/CreatePackageJson';
89
import installDependencies from '../stages/InstallDependencies';
910
import initializeGit from '../stages/InitializeGit';
1011
import Log from '../Log';
1112

12-
const createProject = (projectName: string, packageManager: 'yarn' | 'npm') => {
13+
const createProject = (
14+
projectName: string,
15+
packageManager: 'yarn' | 'npm',
16+
httpFramework: 'koa' | 'express' | 'hapi'
17+
) => {
1318
Log.Instance.infoHeap(`Creating the project`);
1419

1520
const source = path.join(__dirname, '/../../templates/project');
@@ -24,8 +29,12 @@ const createProject = (projectName: string, packageManager: 'yarn' | 'npm') => {
2429
path.join(target, 'gitignore'),
2530
path.join(target, '.gitignore')
2631
);
32+
replaceMaskFile(path.join(target, 'config/app.ts'), {
33+
projectName,
34+
httpFramework
35+
});
2736

28-
createPackageJson(target, projectName);
37+
createPackageJson(target, projectName, httpFramework);
2938
installDependencies(target, packageManager);
3039
initializeGit(projectName);
3140

@@ -51,22 +60,33 @@ const createProjectWithOptions = () => {
5160
type: 'list',
5261
default: 'npm',
5362
choices: ['npm', 'yarn']
63+
},
64+
{
65+
name: 'httpFramework',
66+
message: 'Http Framework:',
67+
type: 'list',
68+
default: 'koa',
69+
choices: ['koa', 'express', 'hapi']
5470
}
5571
])
5672
.then((answers: any) => {
5773
Log.Instance.jump();
58-
createProject(answers.projectName, answers.packageManager);
74+
createProject(
75+
answers.projectName,
76+
answers.packageManager,
77+
answers.httpFramework
78+
);
5979
});
6080
};
6181

6282
commander
6383
.name(`recife-cli project`)
6484
.arguments('[project-name]')
65-
.option('-p, --package-manager <packageManager>', 'Package Manager')
85+
.option('-p, --package-manager <packageManager>', 'Package Manager', 'npm')
86+
.option('-h, --http-framework <httpFramework>', 'Http Framework', 'koa')
6687
.action((name, cmd) => {
67-
console.log(cmd);
6888
if (name) {
69-
createProject(name, cmd.packageManager || 'npm');
89+
createProject(name, cmd.packageManager, cmd.httpFramework);
7090
} else {
7191
createProjectWithOptions();
7292
}

src/stages/CreatePackageJson.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ import fs from 'fs';
22
import path from 'path';
33
import Log from '../Log';
44

5-
const createPackageJson = (target: string, name: string) => {
5+
const versions = {
6+
koa: '^0.1.0',
7+
express: '^0.1.0',
8+
hapi: '^0.1.0'
9+
};
10+
11+
const createPackageJson = (
12+
target: string,
13+
name: string,
14+
httpFramework: 'koa' | 'express' | 'hapi'
15+
) => {
616
Log.Instance.infoHeap(`Creating file package.json`);
717

8-
const basePackageJson = {
18+
let basePackageJson: any = {
919
name: name,
1020
version: '0.0.1',
1121
license: 'MIT',
@@ -16,8 +26,7 @@ const createPackageJson = (target: string, name: string) => {
1626
},
1727
dependencies: {
1828
recife: '^0.7.0',
19-
typescript: '^3.*',
20-
'recife-koa': '^0.1.0'
29+
typescript: '^3.*'
2130
},
2231
browserslist: {
2332
production: ['>0.2%', 'not dead', 'not op_mini all'],
@@ -29,6 +38,9 @@ const createPackageJson = (target: string, name: string) => {
2938
}
3039
};
3140

41+
basePackageJson.dependencies[`recife-${httpFramework}`] =
42+
versions[httpFramework];
43+
3244
fs.writeFileSync(
3345
path.join(target, 'package.json'),
3446
JSON.stringify(basePackageJson, null, 2)

src/utils/replaceMask.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
import fs from 'fs';
2+
3+
export const replaceMaskFile = (path: string, values: any) => {
4+
const contentFile = fs.readFileSync(path).toString();
5+
fs.writeFileSync(path, replaceMask(contentFile, values));
6+
};
7+
18
const replaceMask = (content: string, values: any): string => {
29
Object.keys(values).map(key => {
310
content = content.split(`[[${key}]]`).join(values[key]);

templates/project/config/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { AppConfig } from 'recife';
22

33
export const config: AppConfig = {
4-
appName: 'APP_NAME',
4+
appName: '[[projectName]]',
55
basePath: 'src',
66
port: 8100,
77
host: 'localhost',
8-
httpFramework: 'koa'
8+
httpFramework: '[[httpFramework]]'
99
};

0 commit comments

Comments
 (0)