Skip to content

Commit 69657ad

Browse files
committed
Upgrade to Serverless Framework v3 and the new design
1 parent 4a9e3dd commit 69657ad

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@
6262
"typescript": "^3.9.10"
6363
},
6464
"dependencies": {
65+
"@serverless/utils": "pre-6",
6566
"fs-extra": "^7.0.1",
6667
"globby": "^10.0.2",
6768
"lodash": "^4.17.21"
6869
},
6970
"peerDependencies": {
70-
"serverless": "2",
7171
"typescript": ">=2.2.2"
7272
},
7373
"jest": {

src/Serverless.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ declare namespace Serverless {
6363
}
6464
>
6565

66+
interface Progress {
67+
update(message: string): void
68+
remove(): void
69+
}
70+
71+
interface Utils {
72+
log: ((message: string) => void) & {
73+
verbose(message: string): void
74+
}
75+
progress: {
76+
create(opts?: { message?: string }): Progress;
77+
}
78+
}
79+
6680
interface PluginManager {
6781
spawn(command: string): Promise<void>
6882
}

src/index.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ export class TypeScriptPlugin {
1616
serverless: Serverless.Instance
1717
options: Serverless.Options
1818
hooks: { [key: string]: Function }
19+
utils: Serverless.Utils
1920
commands: Serverless.CommandsDefinition
21+
watchProgress?: Serverless.Progress
2022

21-
constructor(serverless: Serverless.Instance, options: Serverless.Options) {
23+
constructor(serverless: Serverless.Instance, options: Serverless.Options, utils: Serverless.Utils) {
2224
this.serverless = serverless
2325
this.options = options
26+
this.utils = utils
2427

2528
this.commands = {
2629
invoke: {
@@ -81,9 +84,13 @@ export class TypeScriptPlugin {
8184
delete require.cache[module]
8285
})
8386
}
87+
this.watchProgress = this.watchProgress ?? this.utils.progress.create()
8488
},
8589
'after:invoke:local:invoke': async () => {
8690
if (this.options.watch) {
91+
if (this.watchProgress) {
92+
this.watchProgress.update('Watching for TypeScript changes')
93+
}
8794
await this.watchFunction()
8895
}
8996
}
@@ -131,8 +138,7 @@ export class TypeScriptPlugin {
131138
return
132139
}
133140

134-
this.serverless.cli.log(`Watch function ${this.options.function}...`)
135-
this.serverless.cli.log('Waiting for changes...')
141+
this.utils.log.verbose(`Watching function ${this.options.function}`)
136142

137143
this.isWatching = true
138144
await new Promise((resolve, reject) => {
@@ -147,15 +153,17 @@ export class TypeScriptPlugin {
147153
return
148154
}
149155

150-
this.serverless.cli.log(`Watching typescript files...`)
156+
this.utils.log.verbose(`Watching typescript files`)
151157

152158
this.isWatching = true
153159
watchFiles(this.rootFileNames, this.originalServicePath, this.compileTs.bind(this))
154160
}
155161

156162
async compileTs(): Promise<string[]> {
157163
this.prepare()
158-
this.serverless.cli.log('Compiling with Typescript...')
164+
const progress = this.utils.progress.create({
165+
message: 'Compiling TypeScript code'
166+
})
159167

160168
if (!this.originalServicePath) {
161169
// Save original service path and functions
@@ -173,13 +181,13 @@ export class TypeScriptPlugin {
173181
const tsconfig = typescript.getTypescriptConfig(
174182
this.originalServicePath,
175183
tsConfigFileLocation,
176-
this.isWatching ? null : this.serverless.cli
184+
!this.isWatching
177185
)
178186

179187
tsconfig.outDir = BUILD_FOLDER
180188

181189
const emitedFiles = await typescript.run(this.rootFileNames, tsconfig)
182-
this.serverless.cli.log('Typescript compiled.')
190+
progress.remove()
183191
return emitedFiles
184192
}
185193

src/serverless-logs.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare module '@serverless/utils/log' {
2+
export const log: ((message: string) => void) & {
3+
verbose(message: string): void
4+
warning(message: string): void
5+
}
6+
}

src/typescript.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as ts from 'typescript'
22
import * as fs from 'fs-extra'
33
import * as _ from 'lodash'
44
import * as path from 'path'
5+
import {log} from '@serverless/utils/log'
56

67
export function makeDefaultTypescriptConfig() {
78
const defaultTypescriptConfig: ts.CompilerOptions = {
@@ -38,8 +39,7 @@ export function extractFileNames(cwd: string, provider: string, functions?: { [k
3839

3940
// Check that the file indeed exists.
4041
if (!fs.existsSync(path.join(cwd, main))) {
41-
console.log(`Cannot locate entrypoint, ${main} not found`)
42-
throw new Error('Typescript compilation failed')
42+
throw new Error(`Typescript compilation failed: Cannot locate entrypoint, ${main} not found`)
4343
}
4444

4545
return [main]
@@ -65,8 +65,7 @@ export function extractFileNames(cwd: string, provider: string, functions?: { [k
6565
}
6666

6767
// Can't find the files. Watch will have an exception anyway. So throw one with error.
68-
console.log(`Cannot locate handler - ${fileName} not found`)
69-
throw new Error('Typescript compilation failed. Please ensure handlers exists with ext .ts or .js')
68+
throw new Error(`Typescript compilation failed. Please ensure handlers exists with ext .ts or .js.\nCannot locate handler: ${fileName} not found.`)
7069
})
7170
}
7271

@@ -80,15 +79,15 @@ export async function run(fileNames: string[], options: ts.CompilerOptions): Pro
8079

8180
allDiagnostics.forEach(diagnostic => {
8281
if (!diagnostic.file) {
83-
console.log(diagnostic)
82+
log.verbose(JSON.stringify(diagnostic))
8483
}
8584
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start)
8685
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
87-
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`)
86+
log.verbose(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`)
8887
})
8988

9089
if (emitResult.emitSkipped) {
91-
throw new Error('Typescript compilation failed')
90+
throw new Error('TypeScript compilation failed')
9291
}
9392

9493
return emitResult.emittedFiles.filter(filename => filename.endsWith('.js'))
@@ -113,7 +112,7 @@ export function getSourceFiles(
113112
export function getTypescriptConfig(
114113
cwd: string,
115114
tsConfigFileLocation: string = 'tsconfig.json',
116-
logger?: { log: (str: string) => void }
115+
shouldLog?: boolean
117116
): ts.CompilerOptions {
118117
const configFilePath = path.join(cwd, tsConfigFileLocation)
119118

@@ -133,13 +132,13 @@ export function getTypescriptConfig(
133132
throw new Error(JSON.stringify(configParseResult.errors))
134133
}
135134

136-
if (logger) {
137-
logger.log(`Using local tsconfig.json - ${tsConfigFileLocation}`)
135+
if (shouldLog) {
136+
log.verbose(`Using local tsconfig.json - ${tsConfigFileLocation}`)
138137
}
139138

140139
// disallow overrriding rootDir
141-
if (configParseResult.options.rootDir && path.resolve(configParseResult.options.rootDir) !== path.resolve(cwd) && logger) {
142-
logger.log('Warning: "rootDir" from local tsconfig.json is overriden')
140+
if (configParseResult.options.rootDir && path.resolve(configParseResult.options.rootDir) !== path.resolve(cwd) && log) {
141+
log.warning('Typescript: "rootDir" from local tsconfig.json is overridden')
143142
}
144143
configParseResult.options.rootDir = cwd
145144

0 commit comments

Comments
 (0)