1
- const path = require ( "path" ) ;
1
+ const { join , normalize } = require ( "path" ) ;
2
2
const { copySync, ensureDirSync } = require ( "fs-extra" ) ;
3
+ const handlebars = require ( "handlebars" ) ;
3
4
const {
4
5
readdirSync,
5
6
lstatSync,
8
9
writeFileSync
9
10
} = require ( "fs" ) ;
10
11
11
- const CODE_GEN_OUTPUT_DIR = path . normalize (
12
- path . join (
12
+ const CODE_GEN_OUTPUT_DIR = normalize (
13
+ join (
13
14
__dirname ,
14
15
".." ,
15
16
".." ,
@@ -21,69 +22,101 @@ const CODE_GEN_OUTPUT_DIR = path.normalize(
21
22
)
22
23
) ;
23
24
24
- const unOverridables = [
25
- "package.json" ,
26
- "tsconfig.es.json" ,
27
- "tsconfig.json" ,
28
- "tsconfig.test.json"
29
- ] ;
25
+ /**
26
+ * templates are a black list of files we don't want codegen artifact
27
+ * to override in the clients folder
28
+ */
29
+ const templates = readdirSync ( join ( __dirname , "templates" ) )
30
+ . filter ( name => / .s a m p l e $ / . test ( name ) )
31
+ . reduce ( ( accumulator , curr ) => {
32
+ const templatePath = join ( __dirname , "templates" , curr ) ;
33
+ const template = readFileSync ( templatePath ) . toString ( ) ;
34
+ accumulator [ curr . replace ( / .s a m p l e $ / , "" ) ] = template ;
35
+ return accumulator ;
36
+ } , { } ) ;
30
37
31
38
async function copyToClients ( clientsDir ) {
32
39
for ( const modelName of readdirSync ( CODE_GEN_OUTPUT_DIR ) ) {
33
40
if ( modelName === "source" ) continue ;
34
- const artifactPath = path . join (
41
+ const artifactPath = join (
35
42
CODE_GEN_OUTPUT_DIR ,
36
43
modelName ,
37
44
"typescript-codegen"
38
45
) ;
39
- const packageManifestPath = path . join ( artifactPath , "package.json" ) ;
46
+ const packageManifestPath = join ( artifactPath , "package.json" ) ;
40
47
if ( ! existsSync ( packageManifestPath ) ) {
41
48
console . error ( `${ modelName } generates empty client, skip.` ) ;
42
49
continue ;
43
50
}
51
+
44
52
const packageManifest = JSON . parse (
45
53
readFileSync ( packageManifestPath ) . toString ( )
46
54
) ;
47
- const packageName = packageManifest . name . replace ( "@aws-sdk/" , "" ) ;
55
+ const packageName = packageManifest . name ;
48
56
console . log ( `copying ${ packageName } from ${ artifactPath } to ${ clientsDir } ` ) ;
49
- const destPath = path . join ( clientsDir , packageName ) ;
50
- for ( const packageSub of readdirSync ( artifactPath ) ) {
51
- const packageSubPath = path . join ( artifactPath , packageSub ) ;
52
- const destSubPath = path . join ( destPath , packageSub ) ;
53
- if ( unOverridables . indexOf ( packageSub ) >= 0 ) {
54
- if ( ! existsSync ( destSubPath ) )
55
- copySync ( packageSubPath , destSubPath , { overwrite : true } ) ;
56
- else if ( packageSub === "package.json" ) {
57
+ const destPath = join ( clientsDir , packageName . replace ( "@aws-sdk/" , "" ) ) ;
58
+
59
+ //Data used to generate files from template
60
+ const templateData = {
61
+ year : new Date ( ) . getFullYear ( ) ,
62
+ packageName
63
+ } ;
64
+
65
+ for ( const packageSub of [
66
+ ...readdirSync ( artifactPath ) ,
67
+ ...Object . keys ( templates )
68
+ ] ) {
69
+ const packageSubPath = join ( artifactPath , packageSub ) ;
70
+ const destSubPath = join ( destPath , packageSub ) ;
71
+
72
+ if ( Object . keys ( templates ) . indexOf ( packageSub ) >= 0 ) {
73
+ if ( packageSub === "package.json" ) {
57
74
/**
58
75
* Copy package.json content in detail.
59
76
* Basically merge the generated package.json and dest package.json
60
77
* but prefer the values from dest when they contain the same key
61
78
* */
62
- const destManifest = JSON . parse ( readFileSync ( destSubPath ) . toString ( ) ) ;
63
- const updatedManifest = {
64
- ...packageManifest ,
65
- ...destManifest ,
66
- scripts : {
67
- ...packageManifest . scripts ,
68
- ...destManifest . scripts
69
- } ,
70
- dependencies : {
71
- ...packageManifest . dependencies ,
72
- ...destManifest . dependencies
73
- } ,
74
- devDependencies : {
75
- ...packageManifest . devDependencies ,
76
- ...destManifest . devDependencies
77
- }
78
- } ;
79
+ const destManifest = JSON . parse (
80
+ existsSync ( destSubPath )
81
+ ? readFileSync ( destSubPath ) . toString ( )
82
+ : handlebars . compile ( templates [ packageSub ] ) ( templateData )
83
+ ) ;
84
+ const updatedManifest = mergeManifest ( packageManifest , destManifest ) ;
79
85
writeFileSync ( destSubPath , JSON . stringify ( updatedManifest , null , 2 ) ) ;
86
+ } else if ( ! existsSync ( destSubPath ) ) {
87
+ //for files not yet exists and we have a template for it; generate from template
88
+ const file = handlebars . compile ( templates [ packageSub ] ) ( templateData ) ;
89
+ writeFileSync ( destSubPath , file ) ;
90
+ } else {
91
+ //for files we have template but we already have a new version in clients folder, always prefer current one
92
+ //PASS
80
93
}
81
94
} else {
95
+ //For things not in codegen artifact black list, overwrite the existing ones.
82
96
if ( lstatSync ( packageSubPath ) . isDirectory ( ) ) ensureDirSync ( destSubPath ) ;
83
97
copySync ( packageSubPath , destSubPath , { overwrite : true } ) ;
84
98
}
85
99
}
86
100
}
87
101
}
88
102
103
+ const mergeManifest = ( source , dest ) => {
104
+ return {
105
+ ...source ,
106
+ ...dest ,
107
+ scripts : {
108
+ ...source . scripts ,
109
+ ...dest . scripts
110
+ } ,
111
+ dependencies : {
112
+ ...source . dependencies ,
113
+ ...dest . dependencies
114
+ } ,
115
+ devDependencies : {
116
+ ...source . devDependencies ,
117
+ ...dest . devDependencies
118
+ }
119
+ } ;
120
+ } ;
121
+
89
122
module . exports = { copyToClients, CODE_GEN_OUTPUT_DIR } ;
0 commit comments