Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Config file #115

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ testem.log
# System Files
.DS_Store
Thumbs.db

# Production logs
/logs
14 changes: 0 additions & 14 deletions IDEA.md

This file was deleted.

22 changes: 0 additions & 22 deletions bin/ng-wiz.js

This file was deleted.

25 changes: 25 additions & 0 deletions bin/ng-wiz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env node
import path = require('path');
import opn = require('opn');
//
import { runServer as ngWiz } from '../server/server';
import { ngWizConfig } from '../server/config/ng-wiz-config';

const STATIC_FILES_LOCATION = path.join(__dirname, '../ngWiz');
const config = ngWizConfig();
let launchBrowser = config.launchBrowser;

process.argv.forEach((val, index, array) => {
if (val === '-l') {
launchBrowser = true;
}
if (val === '-dl') {
launchBrowser = false;
}
});

// run the app
ngWiz(config.port, STATIC_FILES_LOCATION)
// Opens the url in the default browser
.then(() => launchBrowser ? opn(`http://localhost:${ config.port }`) : null)
.catch((error) => console.error(error));
20 changes: 20 additions & 0 deletions bin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"noLib": false,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "../bin",
},
"include": [
"**/*.ts"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "npm run build && node ./bin/ng-wiz.js -o",
"start": "npm run build && node ./dist/bin/ng-wiz.js",
"start:client": "ng serve",
"start:client-no-progress": "ng serve --progress=false",
"start:server": "ts-node server/server.ts",
"build": "ng build && tsc -p ./server/",
"build": "ng build && tsc -p ./",
"install": "npm run build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"bin": {
"ng-wiz": "bin/ng-wiz.js"
"ng-wiz": "dist/bin/ng-wiz.js"
},
"private": true,
"dependencies": {
Expand Down
10 changes: 10 additions & 0 deletions server/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"port": "3000",
"launchBrowser": "true",
"_COMMENT_logDirectory": "LEAVE EMPTY FOR DEFAULT %APPDATA%/npm/ngWiz/logs",
"logDirectory": "",
"_COMMENT_logDateFormat": "formatting: https://devhints.io/moment#formatting-1",
"logDateFormat": "DDMMYYTHHmmZZ",
"ngServeLaunchBrowser": "true",
"ngServeProt": "4200"
}
19 changes: 19 additions & 0 deletions server/config/ng-wiz-config.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface NgWizConfig {
// The port ngWiz server is listening to.
port: number;

// Launch ngWiz browser window when ngWiz starts.
launchBrowser: boolean;

// Directory to output log files.
logDirectory: string;

// Date format for logging.
logDateFormat: string;

// Launch browser when running development server with "ng serve -o"
ngServeLaunchBrowser: boolean;

// The port the development server run at
ngServePort: number;
}
56 changes: 56 additions & 0 deletions server/config/ng-wiz-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import fs = require('fs');
import path = require('path');
//
import getAppDataPath from 'appdata-path';
//
import { NgWizConfig } from './ng-wiz-config.interface';

export function ngWizConfig(): NgWizConfig {
try {
return handleJSONFile(fs.readFileSync('./config/config.json'));
} catch {
return defaultConfig;
}
}

const defaultConfig = {
port: 3000,
launchBrowser: true,
logDirectory: getAppDataPath(`npm${path.sep}node_modules${path.sep}ngWiz${path.sep}logs`),
logDateFormat: 'DDMMYYTHHmmZZ',
ngServeLaunchBrowser: true,
ngServePort: 4200
};

function handleJSONFile(data): NgWizConfig {
const config: NgWizConfig = defaultConfig;
const configFile = JSON.parse(data);

// The port ngWiz server is listening to.
configFile.port ? config.port = configFile.port : config.port = defaultConfig.port;

// Launch ngWiz browser window when ngWiz starts.
configFile.launchBrowser ? config.launchBrowser = configFile.launchBrowser : config.launchBrowser = defaultConfig.launchBrowser;

// Directory to output log files.
configFile.logDateFormat ? config.logDateFormat = configFile.logDateFormat : config.logDateFormat = defaultConfig.logDateFormat;

// Date format for logging.
if (configFile.logDirectory !== null && configFile.logDirectory !== undefined && configFile.logDirectory !== '') {
config.logDirectory = configFile.logDirectory;
} else {
config.logDirectory = defaultConfig.logDirectory;
}

// Launch browser when running development server with "ng serve -o"
if (configFile.ngServeLaunchBrowser) {
config.ngServeLaunchBrowser = configFile.ngServeLaunchBrowser;
} else {
config.ngServeLaunchBrowser = defaultConfig.ngServeLaunchBrowser;
}

// The port the development server run at
configFile.ngservePort ? config.ngServePort = configFile.ngServePort : config.ngServePort = defaultConfig.ngServePort;

return config;
}
14 changes: 7 additions & 7 deletions server/ngWizLogger.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { configure, getLogger, Logger } from 'log4js';
import { getAppDataPath } from 'appdata-path';
//
import moment = require('moment');
import fs = require('fs');
import path = require('path');
//
import { ngWizConfig } from './config/ng-wiz-config';

const LOG_FILES_DIR = getAppDataPath(`ngWiz${path.sep}logs`);
const dateInFormat = moment().format('DDMMYYTHHmmZZ'); // [171118T1510+0200]
const logFileName = `${LOG_FILES_DIR}/[${dateInFormat}].debug.log`;
const config = ngWizConfig();
const dateInFormat = moment().format(config.logDateFormat);
const logFileName = `${config.logDirectory}/[${dateInFormat}].debug.log`;

export class NgWizLogger {

Expand All @@ -33,11 +33,11 @@ export class NgWizLogger {
}

private deleteOldLogs(log: Logger): void {
fs.readdir(LOG_FILES_DIR, (err, files) => {
fs.readdir(config.logDirectory, (err, files) => {
files.forEach(file => {

if (path.extname(file) === '.log') {
const filePath = LOG_FILES_DIR + path.sep + file;
const filePath = config.logDirectory + path.sep + file;
fs.stat(filePath, (statsErr, stats) => {
const lastModify = moment(stats.mtime);

Expand Down
15 changes: 10 additions & 5 deletions server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { NgWizLogger } from './ngWizLogger';
import { CommandStatusResponse } from './models/command-status-response.interface';
import { IsAngularProjectResponse } from 'server/models/is-angular-project-response.interface';
import { GetProjectsResponse } from './models/get-projects-response.interface';
import { ngWizConfig } from './config/ng-wiz-config';

module.exports = runServer;

function runServer(PORT: number, STATIC_FILES_LOCATION: string): Promise<express.Application> {
export function runServer(PORT: number, STATIC_FILES_LOCATION: string): Promise<express.Application> {
const app: express.Application = express();
const logger = new NgWizLogger('debug');
const config = ngWizConfig();

let ngServeCommandId = null;

Expand All @@ -33,6 +33,11 @@ function runServer(PORT: number, STATIC_FILES_LOCATION: string): Promise<express
next();
});

app.get('/config', (req, res) => {
logger.log.debug('Client request for config', config);
res.send(config);
});

app.get('/isServing', (req, res) => {
logger.log.debug(`Client ask if currently running "ng serve", answering ${!!ngServeCommandId}`);
res.send(!!ngServeCommandId);
Expand All @@ -52,10 +57,10 @@ function runServer(PORT: number, STATIC_FILES_LOCATION: string): Promise<express
app.get('/stopServing', (req, res) => {
logger.log.debug(`Request to stop "ng serve" command`);
if (processRunner.runningProcesses[ngServeCommandId]) {
const port = processRunner.runningProcesses[ngServeCommandId].command.match(/\s([0-9]{4,5})\s?/g)[0].replace(/\s/g, '');
const commandPort = processRunner.runningProcesses[ngServeCommandId].command.match(/\s([0-9]{4,5})\s?/g)[0].replace(/\s/g, '');
const killProcess = {
id: 'killer',
params: `for /f "tokens=5" %a in ('netstat -ano ^| find "${port}" ^| find "LISTENING"') do taskkill /f /pid %a`
params: `for /f "tokens=5" %a in ('netstat -ano ^| find "${commandPort}" ^| find "LISTENING"') do taskkill /f /pid %a`
};
const ngServeStopper = timer(0, 500).subscribe(() => {
if (processRunner.runningProcesses[ngServeCommandId].status === AngularCliProcessStatus.done) {
Expand Down
16 changes: 8 additions & 8 deletions src/app/home/components/serve/serve.component.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Component, Output, EventEmitter, Input } from '@angular/core';
import { Component, Output, EventEmitter, Input, OnInit } from '@angular/core';
//
import * as _ from 'lodash';
//
import { AngularCliCommand } from '../../models/angular-cli-command.interface';
import { NgserveOptions } from '../../default-values/ng-serve-options';
import { CommandService } from './../../services/command/command.service';
import { ErrorService } from './../../services/error/error.service';
import { NgWizConfig } from '../../../../../server/config/ng-wiz-config.interface';

@Component({
selector: 'app-serve',
templateUrl: './serve.component.html',
styleUrls: ['./serve.component.css']
})
export class ServeComponent {
export class ServeComponent implements OnInit {

private readonly MINIMUM_PORT = 1000;
private readonly MAXIMUM_PORT = 99999;
Expand All @@ -21,13 +20,14 @@ export class ServeComponent {
@Output() serveStopper = new EventEmitter<string>();
@Input() isServing: boolean;
@Input() isStoppingServeCommand: boolean;
@Input() config: NgWizConfig;
command: AngularCliCommand;
options = new NgserveOptions();

constructor(
private commandService: CommandService,
private errorService: ErrorService
) {}
ngOnInit() {
this.options.optionalFlags.port.value = this.config.ngServePort;
this.options.optionalFlags.open.isActive = this.config.ngServeLaunchBrowser;
}

startServing(): void {
this.sendCommand.emit(this.options.createCommandString());
Expand Down
3 changes: 2 additions & 1 deletion src/app/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ <h2>{{ currentWorkingDir }}</h2>
(sendCommand)="sendServeCommand($event)"
(serveStopper)="stopServing($event)"
[isStoppingServeCommand]="isStoppingServeCommand"
[isServing]="isServing">
[isServing]="isServing"
[config]="config">
</app-serve>
<app-generate (sendCommand)="sendCommand($event)"></app-generate>
<button
Expand Down
10 changes: 10 additions & 0 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AngularCommandType } from './models/angular-command-type.enum';
import { CommandStatusResponse } from './models/command-status-response.interface';
import { IsAngularProjectResponse } from './models/is-angular-project-response.interface';
import { GetProjectsResponse } from './models/get-projects-response.interface';
import { NgWizConfig } from './../../../server/config/ng-wiz-config.interface';

@Component({
selector: 'app-home',
Expand All @@ -29,17 +30,26 @@ export class HomeComponent implements OnInit {
availableProjects: string[] = [];
isStoppingServeCommand = false;
currentWorkingDir: string;
config: NgWizConfig;

constructor(
private commandService: CommandService,
private errorService: ErrorService
) {}

ngOnInit() {
this.getConfig();
this.keepAlive();
this.checkIfRunningServeCommand();
}

getConfig(): void {
this.commandService.getConfig().subscribe(config => {
this.config = config;
console.log('config: ', this.config);
});
}

checkAngularProject(): void {
this.isReadyForWork = false;
this.commandService.isAngularProject().pipe(
Expand Down
5 changes: 5 additions & 0 deletions src/app/home/services/command/command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Observable } from 'rxjs';
//
import { CommandRequest } from '../../models/angular-command-request';
import { CommandStatusResponse } from '../../models/command-status-response.interface';
import { NgWizConfig } from '../../../../../server/config/ng-wiz-config.interface';


@Injectable({
Expand All @@ -17,6 +18,10 @@ export class CommandService {

constructor(private http: HttpClient) { }

getConfig(): Observable<NgWizConfig> {
return this.http.get<NgWizConfig>(`${this.BASE_URL}:${this.PORT}/config`);
}

isAngularProject() {
return this.http.get(`${this.BASE_URL}:${this.PORT}/isAngularProject`);
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"outDir": "./dist",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
Expand Down