Monday, 24 April 2017

loop in node via command line windows

I'm new to node and I want to loop trough commands in the command line using node. the commands look like:

node dist/main.js dist/index.html dynamic /
node dist/main.js dist/index.html dynamic page.html
node dist/main.js dist/index.html dynamic page2.html

I am using angular4 universal and to rerender my pages I have to put these commands in the command prompt. It wouldn't be a problem if I didn't have like 20 pages already and still more to come. My hand gets sore hehe..

How do I do this?

Thanks in regards!

the main.js file

 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);
});

This code is from the blog: "Angular v4 Universal Demystified"



via Sam van beastlo

No comments:

Post a Comment