Honchar Denys
Honchar Denys

Reputation: 1518

Angular 7 CLI programmatically

What I am trying to success is to have server which start angular 7 app. One other example where that would be needed would be to make more complex generation, like to add more files right after angular generate.

const client = require('@angular/cli');
client.serve(__dirname);
client.generate('service', 'services/user');

This code obviously is not working as .serve or .generate is not a function and I search docs for programmatically solution and couldn't find any. Have anyone succeed that?

Upvotes: 3

Views: 867

Answers (1)

Tobias Oberrauch
Tobias Oberrauch

Reputation: 226

I wrote a node script, that can serve an angular application.

You can run this script in the directory like your angular app (node ../angular-cli.js):

Serve

const angular = require('@angular/cli');
const run = angular.default;

const options = {
    cliArgs: [
        'serve',
    ]
};
run(options)
    .then(function () {
        console.log('then', arguments);
    })
    .catch(function () {
        console.log('catch', arguments);
    })
    .finally(function () {
        console.log('finally', arguments);
    });

Generate

const angular = require('@angular/cli');
const run = angular.default;

const options = {
    cliArgs: [
        'generate',
        'module',
        'test',
        '--routing=true',
    ]
};
run(options)
    .then(function () {
        console.log('then', arguments);
    })
    .catch(function () {
        console.log('catch', arguments);
    })
    .finally(function () {
        console.log('finally', arguments);
    });

Upvotes: 1

Related Questions