@@ -31,13 +31,45 @@ const gitignores = `.nimbella
31
31
__deployer__.zip
32
32
__pycache__/
33
33
node_modules
34
+ package-lock.json
34
35
.DS_Store
35
36
`
36
37
38
+ const ignoreForTypescript = 'lib/\n'
39
+
40
+ // A canned package.json for a minimal typescript project
41
+ const packageJsonForTypescript = `{
42
+ "main": "lib/hello.js",
43
+ "devDependencies": {
44
+ "typescript": "^4"
45
+ },
46
+ "scripts": {
47
+ "build": "tsc -b"
48
+ }
49
+ }
50
+ `
51
+
52
+ // A canned tsconfig.json for typescript project
53
+ const tsconfigJSON = `{
54
+ "compilerOptions": {
55
+ "baseUrl": ".",
56
+ "esModuleInterop": true,
57
+ "importHelpers": true,
58
+ "module": "commonjs",
59
+ "outDir": "lib",
60
+ "rootDir": "src",
61
+ "target": "es2019"
62
+ },
63
+ "include": [
64
+ "src/**/*"
65
+ ]
66
+ }
67
+ `
68
+
37
69
// Working function used by project create
38
70
export async function createProject ( project : string , flags : any , logger : any ) : Promise < void > {
39
71
const { overwrite, language } = flags
40
- const { kind, sampleText } = languageToKindAndSample ( language , logger )
72
+ const { kind, sampleText, ts } = languageToKindAndSample ( language , logger )
41
73
const validKind = await isKindAValidRuntime ( kind )
42
74
if ( ! validKind ) {
43
75
logger . handleError ( `${ language } is not a supported language` )
@@ -54,15 +86,23 @@ export async function createProject(project: string, flags: any, logger: any): P
54
86
}
55
87
}
56
88
createProjectPackage ( samplePackage )
57
- if ( kind ) {
58
- generateSample ( kind , projectConfig , sampleText , samplePackage )
59
- }
89
+ const actionDir = generateSample ( kind , projectConfig , sampleText , samplePackage , ts )
60
90
// Write the config.
61
91
renameActionsToFunctions ( projectConfig )
62
92
const data = yaml . safeDump ( projectConfig )
63
93
fs . writeFileSync ( configFile , data )
64
94
// Add the .gitignore
65
- fs . writeFileSync ( gitignoreFile , gitignores )
95
+ const ignores = gitignores + ( ts ? ignoreForTypescript : '' )
96
+ fs . writeFileSync ( gitignoreFile , ignores )
97
+ // Add typescript-specific information
98
+ if ( ts ) {
99
+ const pjFile = path . join ( actionDir , 'package.json' )
100
+ fs . writeFileSync ( pjFile , packageJsonForTypescript )
101
+ const tscFile = path . join ( actionDir , 'tsconfig.json' )
102
+ fs . writeFileSync ( tscFile , tsconfigJSON )
103
+ const includeFile = path . join ( actionDir , '.include' )
104
+ fs . writeFileSync ( includeFile , 'lib\n' )
105
+ }
66
106
const msgs = [
67
107
`A sample project called '${ project } ' was created for you.` ,
68
108
'You may deploy it by running the command shown on the next line:' ,
@@ -97,22 +137,29 @@ function configTemplate(): DeployStructure {
97
137
98
138
// Convert a user-specified language name to a runtime kind plus a sample.
99
139
// Handle the error case of user requesting an unsupported language.
100
- function languageToKindAndSample ( language : string , logger : any ) : { kind : string , sampleText : string } {
140
+ function languageToKindAndSample ( language : string , logger : any ) : { kind : string , sampleText : string , ts : boolean } {
101
141
language = language . toLowerCase ( )
102
142
if ( ! languages . includes ( language ) ) {
103
143
logger . handleError ( `${ language } is not a supported language` )
104
144
}
105
- const kind = languageToKind ( language )
106
- return { kind, sampleText : samples [ language ] }
145
+ const { kind, ts } = languageToKind ( language )
146
+ return { kind, sampleText : samples [ language ] , ts }
107
147
}
108
148
109
149
// Generate a sample. The sample is called 'hello'.
110
- function generateSample ( kind : string , config : DeployStructure , sampleText : string , samplePackage : string ) {
150
+ function generateSample ( kind : string , config : DeployStructure , sampleText : string , samplePackage : string , ts : boolean ) : string {
111
151
const [ runtime ] = kind . split ( ':' )
112
- const suffix = fileExtensionForRuntime ( runtime , false )
152
+ const suffix = ts ? 'ts' : fileExtensionForRuntime ( runtime , false )
113
153
const actionDir = path . join ( samplePackage , 'hello' )
114
154
fs . mkdirSync ( actionDir , { recursive : true } )
115
- const file = path . join ( actionDir , `hello.${ suffix } ` )
155
+ let file : string
156
+ if ( ts ) {
157
+ const srcDir = path . join ( actionDir , 'src' )
158
+ fs . mkdirSync ( srcDir )
159
+ file = path . join ( srcDir , `hello.${ suffix } ` )
160
+ } else {
161
+ file = path . join ( actionDir , `hello.${ suffix } ` )
162
+ }
116
163
fs . writeFileSync ( file , sampleText )
117
164
const sampPkg = config . packages . find ( pkg => pkg . name === 'sample' )
118
165
const action : ActionSpec = {
@@ -127,6 +174,7 @@ function generateSample(kind: string, config: DeployStructure, sampleText: strin
127
174
limits : limitsFor ( runtime )
128
175
}
129
176
sampPkg . actions . push ( action )
177
+ return actionDir
130
178
}
131
179
132
180
// Set time limits based on the runtime. Most runtimes are fine with the default
@@ -142,30 +190,33 @@ function limitsFor(runtime: string): any {
142
190
143
191
function languageToKind ( language : string ) {
144
192
let runtime = language
193
+ let ts = false
145
194
switch ( language ) {
195
+ case 'ts' :
196
+ case 'typescript' :
197
+ ts = true
198
+ runtime = 'nodejs'
199
+ break
146
200
case 'js' :
147
201
case 'javascript' :
148
202
runtime = 'nodejs'
149
203
break
150
- case 'ts' :
151
- runtime = 'typescript'
152
- break
153
204
case 'py' :
154
205
runtime = 'python'
155
206
break
156
- case 'rb' :
157
- runtime = 'ruby'
158
- break
159
- case 'rs' :
160
- runtime = 'rust'
161
- break
207
+ // case 'rb':
208
+ // runtime = 'ruby'
209
+ // break
210
+ // case 'rs':
211
+ // runtime = 'rust'
212
+ // break
162
213
case 'golang' :
163
214
runtime = 'go'
164
215
break
165
216
default :
166
217
break
167
218
}
168
- return `${ runtime } :default`
219
+ return { kind : `${ runtime } :default` , ts }
169
220
}
170
221
171
222
// Test whether a path in the file system is a project based on some simple heuristics. The path is known to exist.
0 commit comments