/
file-generator.ts
73 lines (66 loc) · 1.66 KB
/
file-generator.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
/* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. */
import { readFile } from 'fs';
import { resolve } from 'path';
import { createLogger } from '@sap-cloud-sdk/util';
import { Directory, SourceFile, SourceFileStructure } from 'ts-morph';
const logger = createLogger({
package: 'generator',
messageContext: 'file-generator'
});
export function sourceFile(
directory: Directory,
relativePath: string,
content: SourceFileStructure,
overwrite: boolean
): SourceFile {
const file = directory.createSourceFile(
`${relativePath}.ts`,
addFileComment(content),
{
overwrite
}
);
file.formatText({ insertSpaceAfterCommaDelimiter: true });
return file;
}
export function otherFile(
directory: Directory,
relativePath: string,
content: string,
overwrite: boolean
): SourceFile {
return directory.createSourceFile(relativePath, content, {
overwrite
});
}
export function copyFile(
fromPath: string,
toRelativePath: string,
toDirectory: Directory,
overwrite: boolean
): void {
return readFile(
resolve(fromPath),
{ encoding: 'utf8' },
(err, changeLogContents) => {
if (err) {
logger.error(err);
} else {
toDirectory.createSourceFile(toRelativePath, changeLogContents, {
overwrite
});
}
}
);
}
function addFileComment(content: SourceFileStructure): SourceFileStructure {
content.leadingTrivia = [
'/*',
' * Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved.',
' *',
' * This is a generated file powered by the SAP Cloud SDK for JavaScript.',
' */',
''
].join('\n');
return content;
}