-
Notifications
You must be signed in to change notification settings - Fork 55
/
file-generator.ts
66 lines (60 loc) · 1.52 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
import { readFileSync } 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 {
try {
const fileContent = readFileSync(resolve(fromPath), { encoding: 'utf8' });
toDirectory.createSourceFile(toRelativePath, fileContent, {
overwrite
});
} catch (err) {
logger.error(err);
}
}
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;
}