Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Download the cx-server from github.com #9

Merged
merged 7 commits into from
Oct 23, 2019
Merged
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
2 changes: 1 addition & 1 deletion src/commands/add-approuter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class AddApprouter extends Command {
cli.action.stop();

cli.action.start('Creating files');
copyFiles(files, await this.getOptions(), this.error);
await copyFiles(files, await this.getOptions()).catch(e => this.error(e, { exit: 2 }));
cli.action.stop();

this.printSuccessMessage();
Expand Down
31 changes: 26 additions & 5 deletions src/commands/add-cx-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
*/

import { Command, flags } from '@oclif/command';
import { OutputFlags } from '@oclif/parser';
import cli from 'cli-ux';
import * as path from 'path';
import { copyFiles, findConflicts, readTemplates } from '../utils/templates';
import { CopyDescriptor } from '../utils/copy-list';
import { copyFiles, findConflicts } from '../utils/templates';

type Flags = OutputFlags<typeof AddCxServer.flags>;

export default class AddCxServer extends Command {
static description = 'Add the scripts to set up a Jenkins master for CI/CD of your project';
Expand All @@ -21,6 +25,11 @@ export default class AddCxServer extends Command {
hidden: true,
description: 'Overwrite files without asking if conflicts are found.'
}),
platform: flags.string({
hidden: true,
default: process.platform,
description: 'The currently running OS.'
}),
help: flags.help({ char: 'h' })
};

Expand All @@ -29,22 +38,34 @@ export default class AddCxServer extends Command {
const options = await this.getOptions();

try {
cli.action.start('Reading templates');
const files = readTemplates({ from: [path.resolve(__dirname, '..', 'templates', 'add-cx-server')], to: flags.projectDir });
cli.action.stop();
const files = [this.copyFromGithub('cx-server', flags), this.copyFromGithub('server.cfg', flags)];

if (flags.platform === 'win32') {
files.push(this.copyFromGithub('cx-server.bat', flags));
}

cli.action.start('Finding potential conflicts');
await findConflicts(files, flags.force, this.error);
cli.action.stop();

cli.action.start('Creating files');
copyFiles(files, options, this.error);
await copyFiles(files, options).catch(e => this.error(e, { exit: 2 }));
cli.action.stop();
} catch (error) {
this.error(error, { exit: 1 });
}
}

private copyFromGithub(fileName: string, flags: Flags): CopyDescriptor {
const githubPrefix = 'https://raw.githubusercontent.com/SAP/devops-docker-cx-server/master/cx-server-companion/life-cycle-scripts/';

return {
sourcePath: new URL(fileName, githubPrefix),
targetFolder: path.resolve(flags.projectDir, 'cx-server'),
fileName: path.resolve(flags.projectDir, 'cx-server', fileName)
};
}

private getOptions() {
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class Init extends Command {
cli.action.stop();

cli.action.start('Creating files');
copyFiles(files, options, this.error);
await copyFiles(files, options).catch(e => this.error(e, { exit: 2 }));
cli.action.stop();

cli.action.start('Adding scripts for CI/CD and dependencies to package.json');
Expand Down
32 changes: 0 additions & 32 deletions src/templates/add-cx-server/cx-server/cx-server

This file was deleted.

36 changes: 0 additions & 36 deletions src/templates/add-cx-server/cx-server/cx-server.bat

This file was deleted.

70 changes: 0 additions & 70 deletions src/templates/add-cx-server/cx-server/server.cfg

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/copy-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

export interface CopyDescriptor {
sourcePath: string;
sourcePath: string | URL;
targetFolder: string;
fileName: string;
}
57 changes: 45 additions & 12 deletions src/utils/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cli from 'cli-ux';
import * as fs from 'fs';
import { compile } from 'handlebars';
import * as https from 'https';
import * as path from 'path';
import { CopyDescriptor } from './copy-list';

Expand Down Expand Up @@ -55,24 +56,56 @@ export async function findConflicts(files: CopyDescriptor[], force: boolean, std
}
}

export function copyFiles(files: CopyDescriptor[], options: { [key: string]: any }, stderr: stderr) {
for (const file of files) {
export async function copyFiles(files: CopyDescriptor[], options: { [key: string]: any }) {
return Promise.all(
files.map(file => {
const { sourcePath, targetFolder, fileName } = file;

if (sourcePath instanceof URL) {
return copyRemote(sourcePath, targetFolder, fileName);
} else {
return copyLocal(sourcePath, targetFolder, fileName, options);
}
})
);
}

function copyRemote(sourcePath: URL, targetFolder: string, fileName: string) {
return new Promise((resolve, reject) => {
https
.get(sourcePath, response => {
if (response.statusCode && (response.statusCode < 200 || response.statusCode > 299)) {
reject(new Error('Failed to load page, status code: ' + response.statusCode));
}

response.on('data', content => {
fs.mkdirSync(targetFolder, { recursive: true });
fs.writeFileSync(fileName, content);
resolve();
});
})
.on('error', e => {
reject(e);
});
});
}

function copyLocal(sourcePath: string, targetFolder: string, fileName: string, options: { [key: string]: any }) {
try {
let content: string;

if (path.extname(file.sourcePath) === '.mu') {
const template = compile(fs.readFileSync(file.sourcePath, { encoding: 'utf8' }));
if (path.extname(sourcePath) === '.mu') {
const template = compile(fs.readFileSync(sourcePath, { encoding: 'utf8' }));
content = template(options);
} else {
content = fs.readFileSync(file.sourcePath, { encoding: 'utf8' });
}

try {
fs.mkdirSync(file.targetFolder, { recursive: true });
} catch (error) {
stderr(error.message);
content = fs.readFileSync(sourcePath, { encoding: 'utf8' });
}

fs.writeFileSync(file.fileName, content);
fs.mkdirSync(targetFolder, { recursive: true });
fs.writeFileSync(fileName, content);
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/add-approuter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as path from 'path';
import AddApprouter from '../src/commands/add-approuter';

describe('Add Approuter', () => {
const pathPrefix = path.resolve(__dirname, __filename.replace('.', '-'));
const pathPrefix = path.resolve(__dirname, __filename.replace(/\./g, '-')).replace('-ts', '');

beforeAll(() => {
if (!fs.existsSync(pathPrefix)) {
Expand Down
20 changes: 18 additions & 2 deletions test/add-cx-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as path from 'path';
import AddCxServer from '../src/commands/add-cx-server';

describe('Add CX Server', () => {
const pathPrefix = path.resolve(__dirname, __filename.replace('.', '-'));
const pathPrefix = path.resolve(__dirname, __filename.replace(/\./g, '-')).replace('-ts', '');

beforeAll(() => {
if (!fs.existsSync(pathPrefix)) {
Expand All @@ -44,6 +44,23 @@ describe('Add CX Server', () => {
const files = fs.readdirSync(projectDir);
expect(files).toContain('cx-server');

const approuterFiles = fs.readdirSync(path.resolve(projectDir, 'cx-server'));
expect(approuterFiles).toContain('cx-server');
expect(approuterFiles).toContain('server.cfg');
}, 30000);

it('should add the necessary files on windows', async () => {
const projectDir = path.resolve(pathPrefix, 'add-cx-server');
if (fs.existsSync(projectDir)) {
fs.removeSync(projectDir);
}

const argv = [`--projectDir=${projectDir}`, '--platform=win32'];
await AddCxServer.run(argv);

const files = fs.readdirSync(projectDir);
expect(files).toContain('cx-server');

const approuterFiles = fs.readdirSync(path.resolve(projectDir, 'cx-server'));
expect(approuterFiles).toContain('cx-server');
expect(approuterFiles).toContain('cx-server.bat');
Expand All @@ -66,7 +83,6 @@ describe('Add CX Server', () => {

const approuterFiles = fs.readdirSync(path.resolve(projectDir, 'cx-server'));
expect(approuterFiles).toContain('cx-server');
expect(approuterFiles).toContain('cx-server.bat');
expect(approuterFiles).toContain('server.cfg');
}, 30000);

Expand Down