Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8833

Node.jsで実行できるスクリプトをTypescriptで作る【備忘】

$
0
0

Node.jsで実行できるスクリプトをTypescriptで作る【備忘】

Typescriptはまだまだ初心者ですが備忘録として残しておこうかと思った次第。

● 完成系のイメージ

チームメンバーのスキル的にnpmでインストールは無い方が無難かなと思いまして、
nodeでjsファイルを指定して実行する方針にしました。
なので binではなく mainで 出力先の./dist/index.jsとしています。
※ 実際のスキル感次第で変更してもいいかなと思ってます。

実行時
$ node cli-tool opt1

// 実行結果
-> Hello, opt1!

● 開発環境

ディレクトリ

ディレクトリ構成
cli-tool
  ├── dist/
  │   └── index.js
  ├── node_modules/
  ├── src/
  │   ├── main.ts
  │   └── component.ts # いい感じにファイルを分割する
  ├── package.json
  ├── tsconfig.json
  └── yarn.lock

コンフィグ

# 作業ディレクトリ確認$ pwd 
// -> cli-tool/

# モジュールインストール$ yarn add --dev @zeit/ncc typescript

# tsconfig作成$ ./node_modules/.bin/tsc --init

型定義なんかも入れて、package.jsonはこんな感じです。

package.json
{"name":"cli-tool","version":"1.0.0","license":"UNLICENSED","main":"./dist/index.js","scripts":{"build":"ncc build src/main.ts --minify","watch":"ncc build src/main.ts --watch",},"dependencies":{"cli-color":"^2.0.0","command-line-args":"^5.1.1","command-line-usage":"^6.1.0","source-map-support":"^0.5.19"},"devDependencies":{"@types/cli-color":"^2.0.0","@types/command-line-args":"^5.0.0","@types/command-line-usage":"^5.0.1","@types/node":"^14.14.5","@types/source-map-support":"^0.5.3","@zeit/ncc":"^0.22.3","typescript":"^4.0.5"}}

tsconfig.jsonは参考記事から変わらず

tsconfig.json
{"compilerOptions":{/*Visithttps://aka.ms/tsconfig.jsontoreadmoreaboutthisfile*//*BasicOptions*/"target":"ES2018",/*SpecifyECMAScripttargetversion:'ES3'(default),'ES5','ES2015','ES2016','ES2017','ES2018','ES2019','ES2020',or'ESNEXT'.*/"module":"commonjs",/*Specifymodulecodegeneration:'none','commonjs','amd','system','umd','es2015','es2020',or'ESNext'.*/"declaration":false,/*Generatescorresponding'.d.ts'file.*/"declarationMap":false,/*Generatesasourcemapforeachcorresponding'.d.ts'file.*/"outDir":"./dist",/*Redirectoutputstructuretothedirectory.*/outDir.*//*StrictType-CheckingOptions*/"strict":true,/*Enableallstricttype-checkingoptions.*//*AdditionalChecks*/"noUnusedLocals":true,/*Reporterrorsonunusedlocals.*/"noUnusedParameters":true,/*Reporterrorsonunusedparameters.*/"noImplicitReturns":true,/*Reporterrorwhennotallcodepathsinfunctionreturnavalue.*/"noFallthroughCasesInSwitch":true,/*Reporterrorsforfallthroughcasesinswitchstatement.*//*ModuleResolutionOptions*/"esModuleInterop":true,/*EnablesemitinteroperabilitybetweenCommonJSandESModulesviacreationofnamespaceobjectsforallimports.Implies'allowSyntheticDefaultImports'.*//*ExperimentalOptions*/"experimentalDecorators":true,/*EnablesexperimentalsupportforES7decorators.*/"emitDecoratorMetadata":true,/*Enablesexperimentalsupportforemittingtypemetadatafordecorators.*//*AdvancedOptions*/"skipLibCheck":true,/*Skiptypecheckingofdeclarationfiles.*/"forceConsistentCasingInFileNames":true/*Disallowinconsistently-casedreferencestothesamefile.*/},"include":["src/**/*"]}
main.ts
#!/usr/bin/env node
"use strict";// ------------// メイン処理// ------------constoptionArg={argv:process.argv};consttest=()=>{constargv=optionArg.argv;constname=argv[2];if(name){console.log(`Hello, ${name}!`);}else{console.log(`alert:require argv.`);}};constmain=()=>{test();};if(require.main===module){main();}

● 参考記事


Viewing all articles
Browse latest Browse all 8833

Trending Articles