[ACA-3821] Add lite-serve with VSCode launcher (#1584)

* Add lite-serve with VSCode launcher

* Make lite-serve smarter to work with Basic authtype as well

* Change and unify app-config-replace script
This commit is contained in:
Popovics András
2020-08-08 13:12:47 +02:00
committed by GitHub
parent d7e61e8fcb
commit 32502e60b9
19 changed files with 973 additions and 213 deletions

View File

@@ -4,51 +4,73 @@ const program = require('commander');
require('dotenv').config({ path: process.env.ENV_FILE });
const fs = require('fs');
const API_CONTENT_HOST = process.env.API_CONTENT_HOST || 'api';
const OAUTH_HOST = process.env.OAUTH_HOST || 'keycloak';
const API_HOST = process.env.API_HOST || null;
const API_CONTENT_HOST = process.env.API_CONTENT_HOST || API_HOST;
const API_PROCESS_HOST = process.env.API_PROCESS_HOST || API_HOST;
const API_AOS_HOST = process.env.AOS_HOST || API_HOST;
const OAUTH_HOST = process.env.OAUTH_HOST || 'oauth-host-default-replaced-value';
const IDENTITY_HOST = process.env.IDENTITY_HOST || 'identity-host-default-replaced-value';
const NOTIFICATION_LAST = parseInt(process.env.NOTIFICATION_LAST, 10) || 2000;
const options = {
apiHost: {
flags: '-a, --api-host',
description: "set apiHost's and ecmHost's value with API_CONTENT_HOST",
set: appConfig => {
description:
'set apiHost=API_HOST, bpmHost=API_PROCESS_HOST, ecmHost=API_CONTENT_HOST, aosHost = API_AOS_HOST if present or fall back to API_HOST in each case',
set: (appConfig) => {
appConfig.apiHost = API_HOST;
appConfig.bpmHost = API_PROCESS_HOST;
appConfig.ecmHost = API_CONTENT_HOST;
appConfig.aosHost = API_CONTENT_HOST + '/alfresco/aos';
appConfig.aosHost = API_AOS_HOST;
}
},
indentityHost: {
flags: '-i, --indentity-host',
description: "set identityHost's value with IDENTITY_HOST",
set: (appConfig) => {
appConfig.authType = 'OAUTH';
appConfig.identityHost = IDENTITY_HOST;
}
},
oauthHost: {
flags: '-o, --oauth-host',
description: "set oauth2.host's value with OAUTH_HOST",
set: appConfig => {
set: (appConfig) => {
appConfig.authType = 'OAUTH';
appConfig.oauth2.host = OAUTH_HOST;
}
},
notification: {
flags: '-n, --notification',
description: "set notificationDefaultDuration's value with <duration> and switch on showNotificationHistory",
set: (appConfig) => {
appConfig.showNotificationHistory = true;
appConfig.notificationDefaultDuration = NOTIFICATION_LAST;
}
}
};
program
.version('0.0.1')
.requiredOption(
'-c, --config <path>',
'path to the app.config.json to reset its values with env vars'
);
program.version('0.0.1').requiredOption('-c, --config <path>', 'path to the app.config.json to reset its values with env vars');
Object.keys(options).forEach(option => {
Object.keys(options).forEach((option) => {
program.option(options[option].flags, options[option].description);
});
program.parse(process.argv);
fs.readFile(program.config, (err, appConfigString) => {
if (err) throw err;
let appConfig = JSON.parse(appConfigString);
if (err) {
throw err;
}
Object.keys(options).forEach(option => {
const appConfig = JSON.parse(appConfigString);
Object.keys(options).forEach((option) => {
if (program[option]) {
options[option].set(appConfig);
}
});
let appConfigReplacedJson = JSON.stringify(appConfig, null, 4);
const appConfigReplacedJson = JSON.stringify(appConfig, null, 4);
fs.writeFileSync(program.config, appConfigReplacedJson);
});

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2005-2019 Alfresco Software, Ltd. All rights reserved.
*
* License rights for this program may be obtained from Alfresco Software, Ltd.
* pursuant to a written agreement and any use of this program without such an
* agreement is prohibited.
*/
import { ParamType } from './params';
export type CommanderProcessor = (arg1: any, arg2: any) => void;
export type CommanderOptionParams = [string, string, CommanderProcessor?, any?];
export interface BaseParamOptions {
name: string;
alias: string;
title: string;
required: boolean;
processor?: CommanderProcessor;
default?: any;
}
export abstract class BaseParam {
protected type: ParamType;
constructor(protected options: BaseParamOptions) {}
abstract get commanderOption(): CommanderOptionParams;
abstract get inquirerOption(): any;
get isRequired() {
return this.options.required;
}
get name() {
return this.options.name;
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2005-2019 Alfresco Software, Ltd. All rights reserved.
*
* License rights for this program may be obtained from Alfresco Software, Ltd.
* pursuant to a written agreement and any use of this program without such an
* agreement is prohibited.
*/
import { ParamType } from './params';
import { BaseParamOptions, CommanderOptionParams, BaseParam } from './base-param';
export class BooleanParam extends BaseParam {
protected type = ParamType.confirm;
get commanderOption(): CommanderOptionParams {
const optionParams: CommanderOptionParams = [`-${this.options.alias}, --${this.options.name}`, this.options.title];
if (this.options.processor !== undefined) {
optionParams.push(this.options.processor);
} else {
optionParams.push((value, previousValue) => {
return value !== undefined ? !!value : previousValue;
});
}
if (this.options.default !== undefined) {
optionParams.push(this.options.default);
}
return optionParams;
}
get inquirerOption() {
return {
type: this.type,
name: this.options.name,
message: this.options.title
};
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2005-2019 Alfresco Software, Ltd. All rights reserved.
*
* License rights for this program may be obtained from Alfresco Software, Ltd.
* pursuant to a written agreement and any use of this program without such an
* agreement is prohibited.
*/
import { ParamType } from './params';
import { BaseParamOptions, CommanderOptionParams, BaseParam } from './base-param';
export interface ComplexCheckboxChoice {
name: string;
value?: any;
checked?: boolean;
short?: string;
}
export interface ChekboxParamOptions extends BaseParamOptions {
choices: string[] | ComplexCheckboxChoice[];
default?: any[];
}
export class CheckboxParam extends BaseParam {
protected type = ParamType.checkbox;
constructor(protected options: ChekboxParamOptions) {
super(options);
}
get commanderOption(): CommanderOptionParams {
const optionParams: CommanderOptionParams = [`-${this.options.alias}, --${this.options.name} <${this.options.name}>`, this.options.title];
if (this.options.processor !== undefined) {
optionParams.push(this.options.processor);
} else {
optionParams.push((value, previousValue) => {
return value !== undefined ? value.split(',') : previousValue;
});
}
if (this.options.default !== undefined) {
optionParams.push(this.options.default);
}
return optionParams;
}
get inquirerOption() {
return {
type: this.type,
name: this.options.name,
message: this.options.title,
choices: this.options.choices,
default: this.options.default
};
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2005-2019 Alfresco Software, Ltd. All rights reserved.
*
* License rights for this program may be obtained from Alfresco Software, Ltd.
* pursuant to a written agreement and any use of this program without such an
* agreement is prohibited.
*/
import commander, { program } from 'commander';
import * as inquirer from 'inquirer';
import { BooleanParam } from './boolean-param';
import { InputParam } from './input-param';
import { ListParam } from './list-param';
import { CheckboxParam } from './checkbox-param';
import * as logger from '../../tools/helpers/logger';
export type CliParam = ListParam | InputParam | BooleanParam | CheckboxParam;
export class CliReader {
private program: commander.Command;
constructor(name: string, usage: string, description: string, version: string) {
this.program = program;
this.program
.name(name)
.usage(usage)
.description(description)
.version(version)
.option('-d, --debug <level>', 'Set the output information level (silly, debug, verbose, info, warn, error)', 'info');
}
*getReader(params: CliParam[], cliArgs: string[]): Generator {
params.forEach((param) => {
program.option.apply(this.program, param.commanderOption);
});
this.program.parse(cliArgs);
logger.level = this.program.debug;
const commanderParams = params
.filter((param) => this.program[param.name] !== undefined)
.reduce((paramsAcc, param) => ({ ...paramsAcc, [param.name]: this.program[param.name] }), {});
yield commanderParams;
let unsetRequiredParams;
if (Object.keys(commanderParams).length) {
unsetRequiredParams = params.filter((param) => param.isRequired && this.program[param.name] === undefined).map((param) => param.inquirerOption);
} else {
unsetRequiredParams = params.map((param) => param.inquirerOption);
}
return yield inquirer.prompt(unsetRequiredParams).then((inquiredParams) => ({ ...commanderParams, ...inquiredParams }));
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2005-2019 Alfresco Software, Ltd. All rights reserved.
*
* License rights for this program may be obtained from Alfresco Software, Ltd.
* pursuant to a written agreement and any use of this program without such an
* agreement is prohibited.
*/
import { ParamType } from './params';
import { BaseParam, BaseParamOptions, CommanderOptionParams } from './base-param';
export class InputParam extends BaseParam {
protected type = ParamType.input;
get commanderOption(): CommanderOptionParams {
const optionParams: CommanderOptionParams = [`-${this.options.alias}, --${this.options.name} <${this.options.name}>`, this.options.title];
if (this.options.processor !== undefined) {
optionParams.push(this.options.processor);
}
if (this.options.default !== undefined) {
optionParams.push(this.options.default);
}
return optionParams;
}
get inquirerOption() {
return {
type: this.type,
name: this.options.name,
message: this.options.title
};
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2005-2019 Alfresco Software, Ltd. All rights reserved.
*
* License rights for this program may be obtained from Alfresco Software, Ltd.
* pursuant to a written agreement and any use of this program without such an
* agreement is prohibited.
*/
import { ParamType } from './params';
import { BaseParamOptions, CommanderOptionParams, BaseParam } from './base-param';
export interface ComplexListChoice {
name: string;
value?: any;
disabled?: boolean;
short?: string;
}
export interface ListParamOptions extends BaseParamOptions {
choices: string[] | ComplexListChoice[];
pageSize?: number;
}
export class ListParam extends BaseParam {
protected type = ParamType.list;
constructor(protected options: ListParamOptions) {
super(options);
}
get commanderOption(): CommanderOptionParams {
const optionParams: CommanderOptionParams = [`-${this.options.alias}, --${this.options.name} <${this.options.name}>`, this.options.title];
if (this.options.processor !== undefined) {
optionParams.push(this.options.processor);
}
if (this.options.default !== undefined) {
optionParams.push(this.options.default);
}
return optionParams;
}
get inquirerOption() {
return {
type: this.type,
name: this.options.name,
message: this.options.title,
choices: this.options.choices,
pageSize: this.options.pageSize
};
}
}

View File

@@ -0,0 +1,14 @@
/*
* Copyright 2005-2019 Alfresco Software, Ltd. All rights reserved.
*
* License rights for this program may be obtained from Alfresco Software, Ltd.
* pursuant to a written agreement and any use of this program without such an
* agreement is prohibited.
*/
export enum ParamType {
input = 'input',
list = 'list',
checkbox = 'checkbox',
confirm = 'confirm'
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2005-2019 Alfresco Software, Ltd. All rights reserved.
*
* License rights for this program may be obtained from Alfresco Software, Ltd.
* pursuant to a written agreement and any use of this program without such an
* agreement is prohibited.
*/
import * as angularJson from 'angular.json';
export const getApps = () => {
return Object.keys(angularJson.projects)
.map((projectName) => ({ name: projectName, ...angularJson.projects[projectName] }))
.filter((project) => project.projectType === 'application')
.filter((project) => project.name.indexOf('e2e') === -1);
};
export const getE2Es = () => {
return Object.keys(angularJson.projects)
.map((projectName) => ({ name: projectName, ...angularJson.projects[projectName] }))
.filter((project) => project.projectType === 'application')
.filter((project) => project.name.endsWith('-e2e'));
};
export const getLibs = () => {
return Object.keys(angularJson.projects)
.map((projectName) => ({ name: projectName, ...angularJson.projects[projectName] }))
.filter((project) => project.projectType === 'library');
};

159
scripts/npm/lite-serve.ts Executable file
View File

@@ -0,0 +1,159 @@
/*
* Copyright 2005-2019 Alfresco Software, Ltd. All rights reserved.
*
* License rights for this program may be obtained from Alfresco Software, Ltd.
* pursuant to a written agreement and any use of this program without such an
* agreement is prohibited.
*/
import { getApps } from './helpers/project-selectors';
import { existsSync } from 'fs';
import { resolve } from 'path';
import { spawn } from 'child_process';
import { yellow, green, red } from 'chalk';
import { ListParam, ComplexListChoice } from 'clireader/list-param';
import { CheckboxParam } from '../clireader/checkbox-param';
import { CliReader, CliParam } from 'clireader/cli-reader';
import * as logger from '../../tools/helpers/logger';
import * as ora from 'ora';
require('dotenv').config({ path: process.env.ENV_FILE });
const API_HOST = process.env.API_HOST;
const OAUTH_HOST = process.env.OAUTH_HOST;
const IDENTITY_HOST = process.env.IDENTITY_HOST;
const NOTIFICATION_LAST = process.env.NOTIFICATION_LAST;
interface LiteServeRunnerInputs {
app: string;
configRewrite: string[];
}
export default class LiteServeRunner {
private cliReader: CliReader;
private inputParams: CliParam[];
constructor(private cliArgs: any[]) {
this.cliReader = new CliReader('lite-serve', '[options]', 'Start lite-serve for previously built app', '0.0.1');
this.inputParams = [
new ListParam({
name: 'app',
alias: 'a',
title: `Which ${yellow('prebuilt')} application to serve with lite-serve?`,
required: true,
choices: this.getAppList.bind(this),
pageSize: 30
}),
new CheckboxParam({
name: 'configRewrite',
alias: 'c',
title: 'Rewrite app.config.json with the following environment vars?',
required: false,
choices: [
...(API_HOST !== undefined ? [{ name: `API_HOST=${API_HOST}`, value: 'a', short: 'API_HOST', checked: true }] : []),
...(OAUTH_HOST !== undefined
? [{ name: `OAUTH_HOST=${OAUTH_HOST} ${red('+ authType=OAUTH also!!!')}`, value: 'o', short: 'OAUTH_HOST', checked: true }]
: []),
...(IDENTITY_HOST !== undefined
? [{ name: `IDENTITY_HOST=${IDENTITY_HOST} ${red('+ authType=OAUTH also!!!')}`, value: 'i', short: 'IDENTITY_HOST', checked: true }]
: []),
...(NOTIFICATION_LAST !== undefined
? [{ name: `NOTIFICATION_LAST=${NOTIFICATION_LAST}`, value: 'n', short: 'NOTIFICATION_LAST', checked: true }]
: [])
]
})
];
}
run() {
const readerGenerator = this.cliReader.getReader(this.inputParams, this.cliArgs);
const program: Object = readerGenerator.next().value;
const builtApps = this.getAppList().filter((app) => !app.disabled);
if (!builtApps.length) {
logger.error('No prebuilt app found.');
process.exit(0);
}
const inputInquirer = <Promise<Object>>readerGenerator.next().value;
return inputInquirer.then(this.appConfigReplace.bind(this)).then(this.spawnLiteServer.bind(this)).catch(logger.error.bind(logger));
}
private appConfigReplace(inputParams: LiteServeRunnerInputs) {
return new Promise((resolvePromise, reject) => {
if (!inputParams['configRewrite'] || !inputParams['configRewrite'].length) {
logger.verbose(green('No rewrite has been made'));
resolvePromise(inputParams);
return;
}
let appPath = getApps()
.filter((app) => app.name === inputParams.app)
.map((project) => this.getOutputPath(project))[0];
appPath = resolve(appPath, 'app.config.json');
const rewriteFlags = `-${inputParams['configRewrite'].join('')}`;
const spinner = ora(`Rewriting ${appPath} with flags: ${rewriteFlags}`).start();
const replace = spawn(process.cwd() + '/scripts/app-config-replace.js', [`--config=${appPath}`, rewriteFlags]);
replace.stdout.on('data', (data) => logger.verbose(data.toString()));
replace.stderr.on('data', (data) => {
logger.error(data.toString());
reject();
});
replace.on('exit', (code) => {
spinner.succeed();
logger.verbose(green(`Rewrite ${appPath} succeeded!`));
resolvePromise(inputParams);
});
});
}
private spawnLiteServer(inputParams: LiteServeRunnerInputs) {
const spinner = ora(`Starting lite-serve, please wait and don't move!`).start();
return new Promise((resolvePromise, reject) => {
const liteServe = spawn('npm', ['run', '--quiet', 'ng', 'run', `${inputParams.app}:lite-serve:standalone`]);
process.on('SIGINT', () => {
liteServe.kill('SIGINT');
spinner.text = red('Lite-serve terminated.');
spinner.fail();
});
liteServe.stdout.on('data', (data) => {
if (data.toString().includes(`lite-serve serving folder`)) {
spinner.text = green('Lite-serve is running.');
}
logger.verbose(data.toString());
});
liteServe.stderr.on('data', (data) => {
logger.error(data.toString());
reject();
});
liteServe.on('exit', (code) => {
logger.verbose('Lite-serve process exited with code ' + code.toString());
resolvePromise();
});
});
}
private getOutputPath(project) {
return resolve(process.cwd(), project.architect.build.options.outputPath);
}
private getAppList(): ComplexListChoice[] {
return getApps().map((project) => {
if (existsSync(this.getOutputPath(project))) {
return {
name: project.name,
disabled: false
};
}
return {
name: `${project.name} (not built)`,
disabled: true
};
});
}
}

20
scripts/run Executable file
View File

@@ -0,0 +1,20 @@
#!/usr/bin/env node
const path = require('path');
const tsConfigPath = path.resolve(__dirname, 'tsconfig.json');
const tsConfig = require(tsConfigPath);
require('ts-node').register({ project: tsConfigPath });
require('tsconfig-paths').register({
project: tsConfigPath,
baseUrl: path.resolve(__dirname),
paths: tsConfig.compilerOptions.paths
});
const handlerParam = process.argv[2];
const RunnerClass = require(`./npm/${handlerParam}.ts`).default;
const runnerArgs = [ ...process.argv ];
runnerArgs.splice(2, 1);
const runner = new RunnerClass(runnerArgs);
runner.run();

32
scripts/tsconfig.json Normal file
View File

@@ -0,0 +1,32 @@
{
"compilerOptions": {
"outDir": "./dist/scripts",
"module": "commonjs",
"target": "es5",
"baseUrl": ".",
"sourceMap": false,
"declaration": false,
"moduleResolution": "node",
"resolveJsonModule": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"allowSyntheticDefaultImports": true,
"typeRoots": [
"node_modules/@types",
"node_modules/commander/typings/index.d.ts"
],
"lib": ["es2018", "dom"],
"types": ["node", "jasmine", "jest"],
"paths": {
"angular.json": ["../angular.json"]
}
},
"exclude": ["node_modules"],
"angularCompilerOptions": {
"preserveWhitespaces": false
}
}