Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 45915e4

Browse files
committed
refactor: handle inconsistencies across the entire codebase
1 parent 3f29af4 commit 45915e4

File tree

27 files changed

+288
-90
lines changed

27 files changed

+288
-90
lines changed

index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ export async function runTasks (args: string[]) {
3434

3535
let state: CliState = {
3636
boilerplate: argv.boilerplate || 'web',
37-
db: !!argv.db,
3837
}
3938

39+
// tslint:disable-next-line: max-line-length quotemark
40+
console.log(" _ _ _ _ \n / \\ __| | ___ _ __ (_)___ | |___ \n / _ \\ / _` |/ _ \\| '_ \\| / __|_ | / __|\n / ___ \\ (_| | (_) | | | | \\__ \\ |_| \\__ \\\n/_/ \\_\\__,_|\\___/|_| |_|_|___/\\___/|___/\n")
41+
4042
const projectRoot = argv._[0].trim()
4143

4244
/**

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
"build/index.js"
1313
],
1414
"scripts": {
15-
"postinstall": "node build/bin/run.js",
1615
"mrm": "mrm --preset=@adonisjs/mrm-preset",
1716
"pretest": "npm run lint",
1817
"test": "node japaFile.js",
1918
"lint": "tslint --project tsconfig.json",
2019
"clean": "del build",
21-
"compile": "npm run lint && npm run clean && tsc && copyfiles \"templates/**/*.txt\" build",
20+
"compile": "npm run lint && npm run clean && tsc && copyfiles \"templates/**/*.txt\" \"templates/**/*.edge\" build",
2221
"build": "npm run compile",
2322
"commit": "git-cz",
2423
"release": "np",
@@ -69,7 +68,9 @@
6968
"@adonisjs/sink": "^2.3.1",
7069
"@poppinss/application": "^1.0.10",
7170
"cli-width": "^2.2.0",
71+
"dedent": "^0.7.0",
7272
"fs-extra": "^8.1.0",
73+
"fs-readdir-recursive": "^1.1.0",
7374
"getopts": "^2.2.5",
7475
"ora": "^4.0.2",
7576
"randomstring": "^1.1.5"

src/contracts.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@ export type TaskFn = (
2323
*/
2424
export type CliState = {
2525
boilerplate: 'web' | 'api',
26-
db: boolean,
2726
}
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,41 @@ import { CliState } from '../contracts'
1111

1212
export const packages: {
1313
[K in CliState['boilerplate']]: {
14-
[pkg: string]: { version: string, providers: string[] },
14+
[pkg: string]: { version: string },
1515
}
1616
} = {
1717
web: {
1818
'@adonisjs/core': {
1919
version: 'latest',
20-
providers: ['@adonisjs/core'],
2120
},
2221
'@adonisjs/fold': {
2322
version: '6',
24-
providers: [],
23+
},
24+
'@adonisjs/ace': {
25+
version: '6',
2526
},
2627
'@adonisjs/bodyparser': {
2728
version: '3',
28-
providers: ['@adonisjs/bodyparser'],
2929
},
3030
'@adonisjs/session': {
3131
version: '2',
32-
providers: ['@adonisjs/session'],
3332
},
3433
'@adonisjs/view': {
3534
version: 'latest',
36-
providers: ['@adonisjs/view'],
3735
},
3836
},
3937
api: {
4038
'@adonisjs/core': {
4139
version: 'latest',
42-
providers: ['@adonisjs/core'],
40+
},
41+
'@adonisjs/ace': {
42+
version: '6',
4343
},
4444
'@adonisjs/fold': {
4545
version: '6',
46-
providers: [],
4746
},
4847
'@adonisjs/bodyparser': {
4948
version: '3',
50-
providers: ['@adonisjs/bodyparser'],
5149
},
5250
},
5351
}

src/schematics/rcMetaFiles.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* adonis-ts-boilerplate
3+
*
4+
* (c) Harminder Virk <virk@adonisjs.com>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { CliState } from '../contracts'
11+
12+
export const metaFiles: {
13+
[K in CliState['boilerplate']]: string[]
14+
} = {
15+
web: [
16+
'.env',
17+
'.adonisrc.json',
18+
'.gitignore',
19+
'resources/views/**/*.edge',
20+
'public/**',
21+
],
22+
api: [
23+
'.env',
24+
'.adonisrc.json',
25+
'.gitignore',
26+
],
27+
}

tasks/copyTemplates.ts

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,65 +8,24 @@
88
*/
99

1010
import { join } from 'path'
11+
import readDir from 'fs-readdir-recursive'
1112
import { TemplateFile, logger } from '@adonisjs/sink'
1213

1314
import { TaskFn } from '../src/contracts'
14-
import { packages } from '../src/boilerplate/packages'
15-
16-
const templates = [
17-
'server.txt',
18-
'start/app.txt',
19-
'start/kernel.txt',
20-
'start/routes.txt',
21-
'app/Exceptions/Handler.txt',
22-
'providers/AppProvider.txt',
23-
]
2415

2516
/**
2617
* Copies templates to project directory
2718
*/
2819
const task: TaskFn = (absPath, _app, state) => {
29-
const boilerPlatePackages = packages[state.boilerplate]
30-
if (state.boilerplate === 'api') {
31-
templates.push('app/Middleware/SpoofAccept.ts')
32-
}
33-
34-
templates.forEach((template) => {
35-
let data: any = {}
36-
37-
/**
38-
* Defining providers for the `start/app` file
39-
*/
40-
if (template === 'start/app.txt') {
41-
data.providers = []
42-
Object.keys(boilerPlatePackages).forEach((name) => {
43-
if (boilerPlatePackages[name].providers.length) {
44-
data.providers = data.providers.concat(`'${boilerPlatePackages[name].providers}',`)
45-
}
46-
})
47-
48-
data.providers.push(`'./providers/AppProvider',`)
49-
}
50-
51-
/**
52-
* Middleware based upon the type of project
53-
*/
54-
if (template === 'start/kernel.txt') {
55-
data.middleware = state.boilerplate === 'api'
56-
? [`'App/Middleware/SpoofAccept',`, `'Adonis/Addons/BodyParserMiddleware',`]
57-
: [`'Adonis/Addons/BodyParserMiddleware',`]
58-
}
59-
60-
new TemplateFile(
61-
absPath,
62-
template.replace(/\.txt$/, '.ts'),
63-
join(__dirname, '..', 'templates', template),
64-
)
65-
.apply(data)
66-
.commit()
67-
68-
logger.create(template)
69-
})
20+
const files = readDir(join(__dirname, '..', 'templates', state.boilerplate))
21+
22+
files
23+
.forEach((name: string) => {
24+
const outputFileName = name.replace(/\.txt$/, '.ts')
25+
const src = join(__dirname, '..', 'templates', state.boilerplate, name)
26+
new TemplateFile(absPath, outputFileName, src).apply({}).commit()
27+
logger.create(outputFileName)
28+
})
7029
}
7130

7231
export default task

tasks/createGitIgnore.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const task: TaskFn = (absPath) => {
2222
gitignore.add('coverage')
2323
gitignore.add('.vscode')
2424
gitignore.add('.DS_STORE')
25+
gitignore.add('.env')
2526

2627
gitignore.commit()
2728
logger.create('.gitignore')

tasks/createPackageFile.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { PackageFile, logger } from '@adonisjs/sink'
1313

1414
import { TaskFn } from '../src/contracts'
1515
import { logInstall } from '../src/spinner'
16-
import { packages } from '../src/boilerplate/packages'
16+
import { packages } from '../src/schematics/packages'
1717

1818
/**
1919
* Creates the `package.json` file in the project root and installs
@@ -26,14 +26,13 @@ const task: TaskFn = async (absPath, _app, state) => {
2626
pkg.set('version', '0.0.0')
2727
pkg.set('private', true)
2828

29-
pkg.setScript('build', 'adonis build')
30-
pkg.setScript('start', 'adonis serve --dev')
31-
32-
const useYarn = process.env.npm_execpath && process.env.npm_execpath.includes('yarn')
29+
pkg.setScript('build', 'node ace build')
30+
pkg.setScript('start', 'node ace serve --watch')
3331

3432
/**
35-
* Use yarn when executed as `yarn create`
33+
* Set by `yarn create`
3634
*/
35+
const useYarn = process.env.npm_execpath && process.env.npm_execpath.includes('yarn')
3736
if (useYarn) {
3837
pkg.yarn(true)
3938
}

tasks/createRcFile.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,26 @@
99

1010
import { RcFile, logger } from '@adonisjs/sink'
1111
import { TaskFn } from '../src/contracts'
12+
import { metaFiles } from '../src/schematics/rcMetaFiles'
1213

1314
/**
1415
* Creates the `.adonisrc.json` file in the project root
1516
*/
1617
const task: TaskFn = (absPath, _app, state) => {
1718
const rcFile = new RcFile(absPath)
1819

20+
rcFile.set('typescript', true)
21+
rcFile.set('commands', {})
1922
rcFile.setExceptionHandler('App/Exceptions/Handler')
2023
rcFile.setAutoload('App', 'app')
2124
rcFile.setAutoload('Contracts', 'contracts')
2225

2326
rcFile.setPreload('start/routes')
2427
rcFile.setPreload('start/kernel')
2528

26-
rcFile.addMetaFile('.env')
27-
rcFile.addMetaFile('.adonisrc.json')
28-
rcFile.addMetaFile('.gitignore')
29-
30-
/**
31-
* Extra files for the web boilerplate
32-
*/
33-
if (state.boilerplate === 'web') {
34-
rcFile.addMetaFile('resources/views/**/*.edge')
35-
rcFile.addMetaFile('public/**')
36-
}
29+
metaFiles[state.boilerplate].forEach((file) => {
30+
rcFile.addMetaFile(file)
31+
})
3732

3833
rcFile.commit()
3934
logger.create('.adonisrc.json')

tasks/createTsConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const task: TaskFn = (absPath) => {
2020
tsconfig.set('exclude', ['node_modules', 'build'])
2121
tsconfig.set('extends', './node_modules/adonis-preset-ts/tsconfig')
2222
tsconfig.set('compilerOptions', {
23+
outDir: 'build',
2324
paths: {
2425
'App/*': ['./app/*'],
2526
'Contracts/*': ['./contracts/*'],

0 commit comments

Comments
 (0)