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 pathpackage.ts
155 lines (142 loc) · 4.62 KB
/
package.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*!
* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved.
*/
import { Command, flags } from '@oclif/command';
import * as glob from 'fast-glob';
import * as fs from 'fs';
import * as Listr from 'listr';
import * as path from 'path';
import * as rm from 'rimraf';
import { boxMessage, checkOldDependencies, getWarnings, parsePackageJson } from '../utils';
export default class Package extends Command {
static description = 'Copies the specified files to the deployment folder';
static examples = [
'$ sap-cloud-sdk package',
'$ sap-cloud-sdk package -i="index.html"',
'$ sap-cloud-sdk package --include="package.json,package-lock.json,index.js,dist/**/*" --exclude="**/*.java"'
];
static flags = {
help: flags.help({
char: 'h',
description: 'Show help for the package command.'
}),
output: flags.string({
char: 'o',
default: 'deployment',
description: 'Output and deployment folder'
}),
ci: flags.boolean({
default: false,
description: 'Add node_modules in production environments to respect the `build once` principle.'
}),
include: flags.string({
char: 'i',
default: 'package.json,package-lock.json,index.js,dist/**/*',
description: 'Comma seperated list of files or globs to include'
}),
exclude: flags.string({
char: 'e',
default: '',
description: 'Comma separated list of files or globs to exclude'
}),
verbose: flags.boolean({
char: 'v',
description: 'Show more detailed output.'
})
};
static args = [
{
name: 'projectDir',
description: 'Path to the project directory that shall be packaged.'
}
];
async run() {
const { flags, args } = this.parse(Package);
const projectDir = args.projectDir || '.';
const outputDir = path.resolve(projectDir, flags.output);
function copyFiles(filePaths: string[]): void {
filePaths.forEach(filepath => {
const outputFilePath = path.resolve(outputDir, path.relative(projectDir, filepath));
fs.mkdirSync(path.dirname(outputFilePath), { recursive: true });
fs.copyFileSync(filepath, outputFilePath);
});
}
const tasks = new Listr([
{
title: `Overwrite ${flags.output}`,
task: () => {
try {
if (fs.existsSync(outputDir)) {
rm.sync(outputDir);
}
fs.mkdirSync(outputDir);
} catch (error) {
this.error(error, { exit: 1 });
}
}
},
{
title: 'Copying files',
task: async () => {
const include = await glob(flags.include.split(','), {
dot: true,
absolute: true,
cwd: projectDir
});
const exclude: string[] = flags.exclude.length
? await glob(flags.exclude.split(','), {
dot: true,
absolute: true,
cwd: projectDir
})
: [];
const filtered = include.filter(filepath => !exclude.includes(filepath));
copyFiles(filtered);
}
},
{
title: 'Copying node_modules for ci',
enabled: () => flags.ci,
task: async () => {
const nodeModuleFiles = await glob('node_modules/**/*', {
dot: true,
absolute: true,
cwd: projectDir
});
copyFiles(nodeModuleFiles);
}
},
{
title: 'Check the SAP Cloud SDK dependencies',
task: async () => {
const { dependencies, devDependencies } = await parsePackageJson(projectDir);
checkOldDependencies(dependencies);
checkOldDependencies(devDependencies);
}
}
]);
await tasks.run();
this.printSuccessMessage();
}
private printSuccessMessage() {
const warnings = getWarnings();
const body = [
'🚀 Please migrate to new packages.',
'Please find how to migrate here:',
'https://github.com/SAP/cloud-sdk/blob/master/knowledge-base/how-to-switch-to-os-cloud-sdk.md'
];
if (warnings) {
if (this.hasOldSDKWarnings(warnings)) {
this.log(boxMessage(['⚠️ Package finished with warnings:', ...warnings, '', ...body]));
} else {
this.log(boxMessage(['⚠️ Package finished with warnings:', ...warnings]));
}
} else {
this.log(boxMessage(['✅ Package finished successfully.']));
}
}
private hasOldSDKWarnings(warnings: string[]) {
const regex = RegExp('Old SAP Cloud SDK: .* is detected.');
return warnings.map(warning => regex.test(warning)).filter(value => value).length > 0;
}
}