Skip to content

Commit

Permalink
feat(cli): add build command to @tensei/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
bahdcoder committed Sep 25, 2021
1 parent c8b75e8 commit 1771538
Show file tree
Hide file tree
Showing 36 changed files with 548 additions and 5,124 deletions.
4 changes: 2 additions & 2 deletions examples/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ module.exports = tensei()
])
.databaseConfig({
type: 'sqlite',
dbName: 'db.sqlite',
debug: true
dbName: 'db.sqlite'
// debug: true
})
.boot(async ctx => {
await seed(ctx)
Expand Down
50 changes: 25 additions & 25 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{
"name": "@examples/typescript",
"version": "0.9.1",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@mikro-orm/sqlite": "^4.4.0",
"@tensei/cms": "^0.9.1",
"@tensei/core": "^0.9.1",
"@tensei/graphql": "^0.9.1",
"@tensei/mde": "^0.9.1",
"@tensei/media": "^0.9.1",
"@tensei/social-auth": "^0.9.1"
},
"scripts": {
"example:dev": "yarn ts-node-dev --respawn src/index.ts",
"build": "tsc"
},
"devDependencies": {
"@types/node": "^15.12.4",
"nodemon": "^2.0.7",
"ts-node": "^10.0.0",
"ts-node-dev": "^1.1.8",
"typescript": "^4.3.4"
},
"private": true
"name": "@examples/typescript",
"version": "0.9.1",
"main": "src/index.ts",
"license": "MIT",
"dependencies": {
"@mikro-orm/sqlite": "^4.4.0",
"@tensei/cms": "^0.9.1",
"@tensei/core": "^0.9.1",
"@tensei/graphql": "^0.9.1",
"@tensei/mde": "^0.9.1",
"@tensei/media": "^0.9.1",
"@tensei/social-auth": "^0.9.1"
},
"scripts": {
"example:dev": "yarn ts-node-dev --respawn src/index.ts",
"build": "tsc"
},
"devDependencies": {
"@types/node": "^15.12.4",
"nodemon": "^2.0.7",
"ts-node": "^10.0.0",
"ts-node-dev": "^1.1.8",
"typescript": "^4.3.4"
},
"private": true
}
8 changes: 4 additions & 4 deletions examples/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ export default tensei()
dateTime('Published At').creationRules('required'),
belongsTo('Category').alwaysLoad(),
array('Procedure')
.of('string')
.of('decimal')
.rules('min:3', 'max:10')
.creationRules('required'),
.creationRules('required', 'max:24'),
array('Prices')
.nullable()
.of('decimal')
.of('string')
.rules('max:10', 'min:2')
.creationRules('required')
.creationRules('required', 'max:500')
])
.icon('library')
.displayField('Title'),
Expand Down
17 changes: 15 additions & 2 deletions packages/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
#!/usr/bin/env node

import { Kernel, handleError, serve, app } from './src'
import { register } from '@adonisjs/require-ts'
import {
Kernel,
handleError,
serve,
app,
getProjectDirectory,
build
} from './src'

// Set cli env variable
process.env.TENSEI_MODE = 'cli'
;(async () => {
// Register ts hook
register(getProjectDirectory(), {
cache: true
})

let tensei = await app()

const { commands } = tensei.ctx

const kernel = new Kernel(tensei)

kernel.register([serve, ...commands])
kernel.register([serve, build, ...commands])

kernel.flag(
'help',
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"scripts": {
"prettier": "prettier --write './**/*.{js,json,ts,css}'",
"build": "tsc --p tsconfig.json",
"dev": "tsc --watch --p tsconfig.json"
"dev": "tsc --watch --p tsconfig.json",
"test": "node japaFile.js"
},
"dependencies": {
"@adonisjs/require-ts": "^2.0.8",
Expand All @@ -31,11 +32,13 @@
"change-case": "^4.1.1",
"check-node-version": "^4.0.3",
"concurrently": "^5.3.0",
"cpy": "^8.1.2",
"decache": "^4.6.0",
"edge.js": "^1.1.4",
"execa": "^5.0.0",
"fs-extra": "^9.1.0",
"getopts": "^2.3.0",
"has-yarn": "^2.1.0",
"hygen": "^6.0.4",
"latest-version": "^5.1.0",
"level": "^7.0.0",
Expand All @@ -55,5 +58,8 @@
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"japa": "^3.1.1"
}
}
55 changes: 55 additions & 0 deletions packages/cli/src/BaseCommands/Build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import hasYarn from 'has-yarn'
import { command } from '@tensei/common'
import { getProjectDirectory } from '../utils/get-app'

import { Compiler } from './Compiler/Compiler'

export const build = command('build')
.flag({
name: 'production',
alias: 'prod',
description: 'Build for production',
type: 'boolean'
})
.flag({
name: 'ignoreTsErrors',
description: 'Ignore typescript errors and complete the build process.',
type: 'boolean'
})
.flag({
name: 'client',
description: 'Select a package manager (npm or yarn).',
type: 'string'
})
.describe('Compile project from Typescript to Javascript.')
.run(async function () {
const appRoot = getProjectDirectory()

const client = this.flagValues.client || hasYarn(appRoot) ? 'yarn' : 'npm'

if (client !== 'npm' && client !== 'yarn') {
this.logger.warning('--client must be set to "npm" or "yarn"')
this.exitCode = 1
return
}

const stopOnError = !this.flagValues.ignoreTsErrors

try {
const compiler = new Compiler(appRoot, this.logger, 'tsconfig.json')

const compiled = this.flagValues.production
? await compiler.compileForProduction(stopOnError, client)
: await compiler.compile(stopOnError)

/**
* Set exitCode based upon the compiled status
*/
if (!compiled) {
this.exitCode = 1
}
} catch (error) {
this.logger.fatal(error)
this.exitCode = 1
}
})
Loading

0 comments on commit 1771538

Please sign in to comment.