Skip to content

Commit

Permalink
feat(cli): clone @adonisjs/ace into @tensei/cli package
Browse files Browse the repository at this point in the history
  • Loading branch information
bahdcoder committed Sep 24, 2021
1 parent 2f1e6a9 commit c8b75e8
Show file tree
Hide file tree
Showing 54 changed files with 8,297 additions and 865 deletions.
3 changes: 2 additions & 1 deletion examples/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = tensei()
.root(__dirname)
.resources([
resource('Post')
.canInsert(({ user }) => {})
.fields([
text('Title').notNullable().rules('required'),
textarea('Description').nullable(),
Expand All @@ -45,7 +46,7 @@ module.exports = tensei()
text('Name').rules('required'),
belongsToMany('Category'),
belongsToMany('Post'),
belongsTo('Team')
belongsTo('Team').nullable()
])
])
.plugins([
Expand Down
3 changes: 2 additions & 1 deletion examples/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"tensei": "tensei"
},
"devDependencies": {
"nodemon": "^2.0.7"
"nodemon": "^2.0.7",
"why-is-node-running": "^2.2.0"
},
"private": true
}
37 changes: 37 additions & 0 deletions packages/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node

import { Kernel, handleError, serve, app } from './src'

// Set cli env variable
process.env.TENSEI_MODE = 'cli'
;(async () => {
let tensei = await app()

const { commands } = tensei.ctx

const kernel = new Kernel(tensei)

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

kernel.flag(
'help',
(value, _, command) => {
if (!value) {
return
}

kernel.printHelp(command)
process.exit(0)
},
{ type: 'boolean' }
)

kernel.onExit(() => {
if (kernel.error) {
handleError(kernel.error)
}
process.exit(kernel.exitCode)
})

kernel.handle(process.argv.splice(2))
})()
6 changes: 6 additions & 0 deletions packages/cli/japaFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require('@adonisjs/require-ts/build/register')

const { configure } = require('japa')
configure({
files: ['test/**/*.spec.ts'],
})
23 changes: 12 additions & 11 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,39 @@
"_templates/"
],
"bin": {
"tensei-cli": "./build/index.js"
"tensei": "./build/cli.js"
},
"scripts": {
"prettier": "prettier --write './**/*.{js,json,ts,css}'",
"build": "tsc --p tsconfig.json",
"dev": "tsc --watch --p tsconfig.json"
},
"dependencies": {
"@adonisjs/require-ts": "^2.0.8",
"@poppinss/chokidar-ts": "^3.3.2",
"@poppinss/cliui": "^2.2.5",
"@poppinss/prompts": "^1.2.3",
"@poppinss/utils": "^3.2.0",
"@types/change-case": "^2.3.1",
"@types/chokidar": "^2.1.3",
"@types/commander": "^2.12.2",
"@types/concurrently": "^5.2.1",
"@types/fs-extra": "^9.0.6",
"@types/listr": "^0.14.2",
"@types/node": "^14.14.10",
"@types/nodemon": "^1.19.0",
"@types/yargs": "^15.0.12",
"chalk": "^4.1.0",
"change-case": "^4.1.1",
"check-node-version": "^4.0.3",
"chokidar": "^3.5.1",
"commander": "^6.2.0",
"concurrently": "^5.3.0",
"decache": "^4.6.0",
"edge.js": "^1.1.4",
"execa": "^5.0.0",
"fs-extra": "^9.1.0",
"getopts": "^2.3.0",
"hygen": "^6.0.4",
"latest-version": "^5.1.0",
"listr": "^0.14.3",
"level": "^7.0.0",
"nodemon": "^2.0.7",
"yargs": "^16.2.0"
"slash": "^3.0.0",
"term-size": "2.2.1"
},
"config": {
"commitizen": {
Expand All @@ -53,6 +55,5 @@
},
"publishConfig": {
"access": "public"
},
"gitHead": "92a29de45627693db340d3b4a503f52eddc2fb27"
}
}
Empty file.
76 changes: 76 additions & 0 deletions packages/cli/src/BaseCommands/Compiler/Ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* @adonisjs/assembler
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { join } from 'path'
import tsStatic from 'typescript'
import { logger as uiLogger } from '@poppinss/cliui'
import { TypescriptCompiler } from '@poppinss/chokidar-ts'
import { resolveFrom } from '@poppinss/utils/build/helpers'

const TSCONFIG_FILE_NAME = 'tsconfig.json'
const DEFAULT_BUILD_DIR = 'build'

/**
* Exposes the API to work with the Typescript compiler API
*/
export class Ts {
/**
* Reference to the typescript compiler
*/
public tsCompiler = new TypescriptCompiler(
this.appRoot,
this.tsconfig,
require(resolveFrom(this.appRoot, 'typescript/lib/typescript'))
)

constructor(
private appRoot: string,
private logger: typeof uiLogger,
private tsconfig = TSCONFIG_FILE_NAME
) {}

/**
* Render ts diagnostics
*/
public renderDiagnostics(
diagnostics: tsStatic.Diagnostic[],
host: tsStatic.CompilerHost
) {
console.error(
this.tsCompiler.ts.formatDiagnosticsWithColorAndContext(diagnostics, host)
)
}

/**
* Parses the tsconfig file
*/
public parseConfig(): undefined | tsStatic.ParsedCommandLine {
const { error, config } = this.tsCompiler.configParser().parse()

if (error) {
this.logger.error(`unable to parse ${this.tsconfig}`)
this.renderDiagnostics([error], this.tsCompiler.ts.createCompilerHost({}))
return
}

if (config && config.errors.length) {
this.logger.error(`unable to parse ${this.tsconfig}`)
this.renderDiagnostics(
config.errors,
this.tsCompiler.ts.createCompilerHost(config.options)
)
return
}

config!.options.rootDir = config!.options.rootDir || this.appRoot
config!.options.outDir =
config!.options.outDir || join(this.appRoot, DEFAULT_BUILD_DIR)
return config
}
}
7 changes: 7 additions & 0 deletions packages/cli/src/BaseCommands/Help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { command } from '@tensei/common'

export const help = command('help')
.describe('See help for all commands.')
.run(function () {
this.kernel.printHelp()
})
48 changes: 48 additions & 0 deletions packages/cli/src/BaseCommands/Serve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { command } from '@tensei/common'
import { getWatcherHelpers } from '@adonisjs/require-ts'
import { logger as uiLogger, sticker } from '@poppinss/cliui'
import { app, kill } from '../utils/get-app'
import { Ts } from './Compiler/Ts'

export const serve = command('serve')
.stayAlive()
.describe('Start the HTTP server and watch for file changes.')
.run(async function () {
const ts = new Ts(process.cwd(), this.logger)

console.log('========@process.cwd()', process.cwd())

const config = ts.parseConfig()

if (!config) {
this.logger.warning(
'Cannot start watcher because of errors in the config file'
)

return
}

const watcher = ts.tsCompiler.watcher(config, 'raw')

watcher.on('watcher:ready', () => {
this.logger.info('watching file system for changes')
})

await this.kernel.application.listen()

watcher.on('source:change', async ({ absPath, relativePath }) => {
this.logger.action('update').succeeded(relativePath)

let tensei = await app()

this.logger.info('Restarting server ...')

await kill(this.kernel.application.server)

this.kernel.application = tensei

await this.kernel.application.listen()

this.logger.success('Server restarted successfully.')
})
})
Loading

0 comments on commit c8b75e8

Please sign in to comment.