# nest js command line script - console.ts ```typescript= // console.ts import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { UserTask } from './tasks/user.task'; async function bootstrap() { const application = await NestFactory.createApplicationContext( AppModule, ); const command_prefix = process.argv[2].split(':')[0]; const command = process.argv[2].split(':')[1]; const argv = process.argv.filter((v, index) => index > 1); try { switch (command_prefix) { case 'user': const task = new UserTask(application) console.log(await task[command](argv)); break; default: console.log('Command not found'); process.exit(1); } } catch(err: unknown) { if (err instanceof TypeError) { console.log('Command not found') } else { console.log(err); } } await application.close(); process.exit(0); } bootstrap(); ``` - user.task.ts ```typescript= import { UserService } from '../user/user.service'; export class FinMindTask { private readonly service: UserService; constructor(app) { this.service = app.get(UserService) } async generateExcel(): Promise<any> { return this.service.generateExcel(); } } ``` - package.json ```json= { "scripts": { "execute": "ts-node -r tsconfig-paths/register ./src/console.ts" } } ``` 接下來就可以用 `$ npm run execute user:generateExcel` 來透過指令執行 service 內的東西
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up