2017-04-24 47 views
0

私はノードが新しく、ノードを使用してコマンドラインでトラフコマンドをループしたいと思います。コマンドは次のようになります。コマンドラインウィンドウ経由のノードのループ

私はangular4 universalを使用しています。私のページを再レンダリングするにはこれらのコマンドをコマンドプロンプトに入力する必要があります。すでに20ページもありませんでしたが、まだまだそれ以上の問題がなければ、問題はありません。私の手は痛みを和らげます。

どうすればいいですか?

ありがとうございます!

main.jsは、このコードはblogからである

import 'zone.js/dist/zone-node'; 
import { renderModuleFactory } from '@angular/platform-server' 
import { enableProdMode } from '@angular/core' 
import { AppServerModuleNgFactory } from './src/app.server.module.ngfactory' 
import * as fs from 'fs'; 
import * as path from 'path'; 
enableProdMode(); 
const args = process.argv.slice(2); 
if (args.length != 3) { 
    process.stdout.write("Usage: node dist/main.js <document> <distDir> <url>\n"); 
    process.exit(); 
} 
const indexFileContent = fs.readFileSync(args[0], 'utf8'); 
renderModuleFactory(AppServerModuleNgFactory, { 
    document: indexFileContent, 
    url: args[2] 
}).then(string => { 
    let destUrl = args[2]; 
    if (destUrl == '/') 
     destUrl = 'index.html' 
    const targetDir = args[1] + '/' + destUrl; 
    targetDir.split('/').forEach((dir, index, splits) => { 
     if (index !== splits.length - 1) { 
      const parent = splits.slice(0, index).join('/'); 
      const dirPath = path.resolve(parent, dir); 
      if (!fs.existsSync(dirPath)) { 
       fs.mkdirSync(dirPath); 
      } 
     } 
    }); 
    fs.writeFileSync(targetDir, string); 
    console.log(targetDir); 
}); 

ファイル:"Angular v4 Universal Demystified"

+0

何をしようとしていますか(あまりにも曖昧です) –

+0

これらのページを手動で作成していますか? - main.jsは何をしていますか? - あなたはこれらの議論を角度cliに配管していますか?私は完全にはあなたの最終的なhtmlファイルを生成することはありません –

+0

はい、それは基本的に私の質問ですが、私はどのようにそれを行うのですか?私はnode.jsが初めてです。コードのすべての行で配列を作ることについて考えましたが、私はどのようにコードの行を知りません... –

答えて

1

専用のノードを使用して、私は私の頭の上をオフに知っている2つの方法があります(あなたは、代わりに使用することができますバッシュ、Pythonスクリプト)childExecを使用する別script.jsを作成main.js

  • 編集

    最初にmain.jsを編集できるとします(後ほどchildExecバージョンで更新します)。

    注:私は

    ラン

    ノードDIST/main.jsのDIST/index.htmlを動的とファイル名の配列をループに焦点を当てるために、コードの非関連部分を削除しました

    メインjsの

    const args = process.argv.slice(2); 
    //if (args.length != 3) { 
    // process.stdout.write("Usage: node dist/main.js <document> <distDir> <url>\n"); 
    // process.exit(); 
    //} 
    
    var arr = ['page.html', 'page2.html'] //etc 
    
    arr.forEach(function(file) { 
        renderModuleFactory(AppServerModuleNgFactory, { 
        document: indexFileContent, 
        url: file // -> this is what we need to change page.html 
        }).then(string => { 
        let destUrl = file; // -> page.html 
        if (destUrl == '/') 
         destUrl = 'index.html' 
        const targetDir = args[1] + '/' + destUrl; 
        targetDir.split('/').forEach((dir, index, splits) => { 
         if (index !== splits.length - 1) { 
          const parent = splits.slice(0, index).join('/'); 
          const dirPath = path.resolve(parent, dir); 
          if (!fs.existsSync(dirPath)) { 
           fs.mkdirSync(dirPath); 
          } 
         } 
        }); 
        fs.writeFileSync(targetDir, string); 
        console.log(targetDir); 
        }); 
    }); 
    

    、について説明:私たちは宣言arr、アレイ内のファイルの配列でarg[2]/<url>を削除するよう

    スクリプトは、ファイルをレンダリングする形式node dist/main.js <document> <distDir> <url>を使用しています。これにより、必要なファイルを手動で入力する必要がなくなります。

  • 関連する問題