@foal/cli
Version:
CLI tool for FoalTS
67 lines (66 loc) • 2.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectAngularCommandService = void 0;
const path_1 = require("path");
const safe_1 = require("colors/safe");
/**
* Service for connecting an Angular frontend to a FoalTS application.
*/
class ConnectAngularCommandService {
generator;
constructor(generator) {
this.generator = generator;
}
/**
* Configure an Angular project to interact with a FoalTS application.
*
* @param {string} path - The path to the Angular project directory
* @returns {void}
*/
run(path) {
if (!this.generator.exists(path)) {
if (process.env.P1Z7kEbSUUPMxF8GqPwD8Gx_FOAL_CLI_TEST !== 'true') {
console.log((0, safe_1.red)(` The directory ${path} does not exist.`));
}
return;
}
if (!this.generator.exists((0, path_1.join)(path, 'angular.json'))) {
if (process.env.P1Z7kEbSUUPMxF8GqPwD8Gx_FOAL_CLI_TEST !== 'true') {
console.log((0, safe_1.red)(` The directory ${path} is not an Angular project (missing angular.json).`));
}
return;
}
if (!this.generator.exists((0, path_1.join)(path, 'package.json'))) {
if (process.env.P1Z7kEbSUUPMxF8GqPwD8Gx_FOAL_CLI_TEST !== 'true') {
console.log((0, safe_1.red)(` The directory ${path} is not an Angular project (missing package.json).`));
}
return;
}
this.generator
.cd(path)
.copyTemplate('angular/proxy.conf.json', 'src/proxy.conf.json')
.modify('package.json', content => {
const pkg = JSON.parse(content);
pkg.scripts.build = 'ng build --prod';
return JSON.stringify(pkg, null, 2);
})
.modify('angular.json', content => {
const config = JSON.parse(content);
const projectName = Object.keys(config.projects)[0];
// Proxy configuration
config.projects[projectName].architect ||= {};
config.projects[projectName].architect.serve ||= {};
config.projects[projectName].architect.serve.options ||= {};
config.projects[projectName].architect.serve.options.proxyConfig = 'src/proxy.conf.json';
// Output build directory
const outputPath = (0, path_1.join)((0, path_1.relative)(path, process.cwd()), 'public')
// Make projects generated on Windows build on Unix.
.replace(/\\/g, '/');
config.projects[projectName].architect.build ||= {};
config.projects[projectName].architect.build.options ||= {};
config.projects[projectName].architect.build.options.outputPath = outputPath;
return JSON.stringify(config, null, 2);
});
}
}
exports.ConnectAngularCommandService = ConnectAngularCommandService;