手書きnpmバッグ——markdown回転html

3779 ワード

demo
基本共有コース
まず、パッケージを公開します.前のチュートリアルは明確で、基本的にすべてをカバーしています.パッケージの具体的な機能を拡張する以外に、まずしなければならないのはプロセスです.
  • 新しいフォルダを作成し、packageを作成します.jsonファイル端末入力
  • npm init -y
    

    バカな操作で、名前の競合のエラーに注意します.
  • indexを新規作成します.jsファイルは前回のチュートリアルに基づいて、コマンドツールを作成します.index.js冒頭の言葉:
  • #!/usr/bin/env node 
    
  • package.jsonのオブジェクトに追加されたプロパティ:
  • "bin": {
            "mdtohtml": "./index.js"
        }
    

    考えてみましょう
    ディレクトリにコマンドを入力します
    mdtohtml file.md out.html
    

    ファイルを1つ得る.mdファイル、読み取り、文字列、処理文字列、htmlへの書き込み.
    ここで問題があります.今発表したばかりで、開発中です.コードを修正して発表しなければなりません.また実行します.呼び出しコマンドのポインタ原理により、ローカルにダウンロードされた/usr/local/lib/node_が呼び出されます.modules/markdowntoohtml/index.js(mac)では、このファイルを修正し、コマンドテストを直接呼び出して、できるようになったら、結果をパッケージのファイルにコピーして、パブリッシュします.
    実装機能の開始
    ユーザーが入力したコマンドからターゲットファイルを取得する必要があります
    まず、a.mdのようなファイル名を入力したに違いありません.その後、このファイルの絶対パスを得るにはどうすればいいですか.1つのモジュールargv機能は、indexでパラメータを入力することができます.jsはconsoleに書き込む.log(process.argv)テストでコマンドを実行
    mdtohtml a.md b.html
    

    配列ですね.3、4番目は私たちのパラメータですね.だから伝参は簡単だjsに入力
    if (process.argv.length < 4) {
        console.log('       ,,,')
    } else {
        var inputPath = process.argv[2]
        var outputPath = process.argv[3]
    }
    

    ファイルを読み書きする
    nodejs内蔵モジュール、indexが必要です.js書き込み
    var fs = require('fs')
    if (process.argv.length < 4) {
        console.log('       ,,,')
    } else {
        var inputPath = process.argv[2]
        var outputPath = process.argv[3]
       
        fs.readFile(inpath, 'utf8', function(err, str) {
            if (err) {
                console.log('read failed,,,')
            } else {
                console.log(str)
               
                fs.writeFile(outpath, result, function(err) {
                    if (err) {
                        return console.log('read failed,,,')
                    } else {
                        console.log('writed,,,,')
                    }
    
                })
            }
        })
    }
    

    3つの処理すべき問題
  • ファイルパスの取得inpath取得は、モジュール機能processを導入する.cwd()は、現在存在するディレクトリのパスを取得し、inputPathを加えるのではないでしょうか.もう一つ問題があります.macはwinシステムの経路表現とは違いますね.これは問題ではありません.もう一つのモジュールpathを導入します.どうせ問題の差は多くありません.
  • です.
    var path = require('path')
    inpath   path.resolve(process.cwd(),inputPath)
    
  • 同じ理屈で書かれたファイルoutpasshは似ています:
  • path.resolve(process.cwd(),outputPath)
    
  • strデータを読みました.html形式に処理します.つまり、上の列のresultです.それは何ですか.対応するバッグが必要で、npmで調べてmarkdown-itのバッグを得て、そのチュートリアルを見て、直接ローカルディレクトリの下にインストールして、それからindex.js書き込み
  • var mdit = require('markdown-it')
    var md = new mdit()
    result     md.render(str)
    

    最終的なすべてのコードは
    
    var fs = require('fs')
    var path = require('path')
    console.log(process.argv)
    var mdit = require('markdown-it')
    var md = new mdit()
    
    if (process.argv.length < 4) {
        console.log('       ,,,')
    } else {
        var inputPath = process.argv[2]
        var outputPath = process.argv[3]
        console.log(process.cwd())
        fs.readFile(path.resolve(process.cwd(), inputPath), 'utf8', function(err, str) {
            if (err) {
                console.log('read failed,,,')
            } else {
                console.log(str)
                var result = md.render(str)
                fs.writeFile(path.resolve(process.cwd(), outputPath), result, function(err) {
                    if (err) {
                        return console.log('read failed,,,')
                    } else {
                        console.log('writed,,,,')
                    }
    
                })
            }
        })
    }
    

    テストしてみる
    端末でa.mdが存在する経路の下で、入力
    mdtohtml a.md b.html
    

    結果: