This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathadd-cds.ts
113 lines (101 loc) · 3.55 KB
/
add-cds.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*!
* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved.
*/
import { Command, flags } from '@oclif/command';
import cli from 'cli-ux';
import * as Listr from 'listr';
import { installDependencies, modifyGitIgnore, modifyPackageJson } from '../utils';
import { copyFiles, findConflicts, getCopyDescriptors, getProjectNameFromManifest, getTemplatePaths } from '../utils/';
export default class AddCds extends Command {
static description = 'Setup your Cloud Foundry app to use a CDS service';
static examples = ['$ sap-cloud-sdk add-cds'];
static flags = {
// visible
force: flags.boolean({
description: 'Do not fail if a file or npm script already exist and overwrite it.'
}),
help: flags.help({
char: 'h',
description: 'Show help for the add-cds command.'
}),
verbose: flags.boolean({
char: 'v',
description: 'Show more detailed output.'
}),
// hidden
projectName: flags.string({
hidden: true,
description: 'Give project name which is used for the Cloud Foundry mainfest.yml.'
}),
skipInstall: flags.boolean({
hidden: true,
description: 'Skip installing npm dependencies. If you use this, make sure to install manually afterwards.'
})
};
static args = [
{
name: 'projectDir',
description: 'Path to the project directory in which the cds sources should be added.'
}
];
async run() {
const { flags, args } = this.parse(AddCds);
const projectDir = args.projectDir || '.';
try {
const options = await this.getOptions();
const tasks = new Listr([
{
title: 'Creating files',
task: async () => {
const copyDescriptors = getCopyDescriptors(projectDir, getTemplatePaths(['add-cds']));
await findConflicts(copyDescriptors, flags.force).catch(e => this.error(flags.verbose ? e.stack : e.message, { exit: 11 }));
await copyFiles(copyDescriptors, options);
}
},
{
title: 'Adding dependencies to package.json',
task: async () =>
modifyPackageJson({ projectDir, force: flags.force, addCds: true }).catch(e =>
this.error(flags.verbose ? e.stack : e.message, { exit: 12 })
)
},
{
title: 'Installing dependencies',
task: async () => installDependencies(projectDir, flags.verbose).catch(e => this.error(flags.verbose ? e.stack : e.message, { exit: 13 })),
enabled: () => !flags.skipInstall
},
{
title: 'Modifying `.gitignore`',
task: () => modifyGitIgnore(projectDir, true)
}
]);
await tasks.run();
this.printSuccessMessage();
} catch (e) {
this.error(flags.verbose ? e.stack : e.message, { exit: 1 });
}
}
private async getOptions() {
const projectName = getProjectNameFromManifest(this);
const options: { [key: string]: string } = {
projectName: projectName || (await cli.prompt('Enter project name as maintained in Cloud Foundry'))
};
return options;
}
private printSuccessMessage() {
this.log(
[
'✅ Successfully added a cds service to your project.',
'',
'Generated service needs to be exposed.',
'For express apps you can do this by adding the following snippet to your code:',
'cds',
' .connect()',
" .serve('CatalogService')",
' .in(<your-express-app>)',
'',
'For other frameworks please refer to the documentation.'
].join('\n')
);
}
}