Skip to content
This repository was archived by the owner on Dec 28, 2021. It is now read-only.

Commit dfb5625

Browse files
committed
refactor: generate type declarations
1 parent bd34b92 commit dfb5625

File tree

8 files changed

+87
-85
lines changed

8 files changed

+87
-85
lines changed

src/Serverless.d.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/index.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import * as fs from 'fs-extra'
33
import * as _ from 'lodash'
44
import * as globby from 'globby'
55

6-
import * as typescript from './typescript'
7-
import watchFiles from './watchFiles'
6+
import { ServerlessTSInstance, ServerlessTSOptions, ServerlessTSFunction } from './serverlessTypes'
7+
import { extractFileNames, getTypescriptConfig, run } from './typescript'
8+
import { watchFiles } from './watchFiles'
89

910
const SERVERLESS_FOLDER = '.serverless'
1011
const BUILD_FOLDER = '.build'
@@ -13,11 +14,11 @@ export class TypeScriptPlugin {
1314
private originalServicePath: string
1415
private isWatching: boolean
1516

16-
serverless: Serverless.Instance
17-
options: Serverless.Options
17+
serverless: ServerlessTSInstance
18+
options: ServerlessTSOptions
1819
hooks: { [key: string]: Function }
1920

20-
constructor(serverless: Serverless.Instance, options: Serverless.Options) {
21+
constructor(serverless: ServerlessTSInstance, options: ServerlessTSOptions) {
2122
this.serverless = serverless
2223
this.options = options
2324

@@ -75,7 +76,7 @@ export class TypeScriptPlugin {
7576
}
7677
}
7778

78-
get functions(): { [key: string]: Serverless.Function } {
79+
get functions(): { [key: string]: ServerlessTSFunction } {
7980
const { options } = this
8081
const { service } = this.serverless
8182

@@ -89,7 +90,7 @@ export class TypeScriptPlugin {
8990
}
9091

9192
get rootFileNames(): string[] {
92-
return typescript.extractFileNames(
93+
return extractFileNames(
9394
this.originalServicePath,
9495
this.serverless.service.provider.name,
9596
this.functions
@@ -145,15 +146,15 @@ export class TypeScriptPlugin {
145146
this.serverless.config.servicePath = path.join(this.originalServicePath, BUILD_FOLDER)
146147
}
147148

148-
const tsconfig = typescript.getTypescriptConfig(
149+
const tsconfig = getTypescriptConfig(
149150
this.originalServicePath,
150151
this.serverless,
151152
this.isWatching ? null : this.serverless.cli
152153
)
153154

154155
tsconfig.outDir = BUILD_FOLDER
155156

156-
const emitedFiles = typescript.run(this.rootFileNames, tsconfig)
157+
const emitedFiles = run(this.rootFileNames, tsconfig)
157158
this.serverless.cli.log('Typescript compiled.')
158159
return emitedFiles
159160
}
@@ -277,5 +278,3 @@ export class TypeScriptPlugin {
277278
})
278279
}
279280
}
280-
281-
module.exports = TypeScriptPlugin

src/serverlessTypes.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export interface ServerlessTSInstance {
2+
cli: {
3+
log(str: string): void
4+
}
5+
config: {
6+
servicePath: string
7+
}
8+
service: Partial<ServerlessTSService>
9+
pluginManager: ServerlessTSPluginManager
10+
}
11+
12+
export interface ServerlessTSService {
13+
provider: {
14+
name: string
15+
}
16+
custom: {
17+
typeScript: {
18+
tsconfigFilePath: string | undefined
19+
}
20+
}
21+
functions: {
22+
[key: string]: ServerlessTSFunction
23+
}
24+
package: ServerlessTSPackage
25+
getAllFunctions(): string[]
26+
}
27+
28+
export interface ServerlessTSOptions {
29+
function?: string
30+
watch?: boolean
31+
extraServicePath?: string
32+
}
33+
34+
export interface ServerlessTSFunction {
35+
handler: string
36+
package: ServerlessTSPackage
37+
}
38+
39+
export interface ServerlessTSPackage {
40+
include: string[]
41+
exclude: string[]
42+
artifact?: string
43+
individually?: boolean
44+
}
45+
46+
export interface ServerlessTSPluginManager {
47+
spawn(command: string): Promise<void>
48+
}

src/typescript.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import * as ts from 'typescript'
2-
import * as fs from 'fs-extra'
2+
import * as fse from 'fs-extra'
33
import * as _ from 'lodash'
44
import * as path from 'path'
55

6+
import { ServerlessTSFunction, ServerlessTSInstance } from './serverlessTypes'
7+
68
export const makeDefaultTypescriptConfig = (): ts.CompilerOptions => {
79
const defaultTypescriptConfig: ts.CompilerOptions = {
810
preserveConstEnums: true,
@@ -18,7 +20,7 @@ export const makeDefaultTypescriptConfig = (): ts.CompilerOptions => {
1820
return defaultTypescriptConfig
1921
}
2022

21-
export const extractFileNames = (cwd: string, provider: string, functions?: { [key: string]: Serverless.Function }): string[] => {
23+
export const extractFileNames = (cwd: string, provider: string, functions?: { [key: string]: ServerlessTSFunction }): string[] => {
2224
// The Google provider will use the entrypoint not from the definition of the
2325
// handler function, but instead from the package.json:main field, or via a
2426
// index.js file. This check reads the current package.json in the same way
@@ -27,17 +29,17 @@ export const extractFileNames = (cwd: string, provider: string, functions?: { [k
2729
// it instead selects the index.js file.
2830
if (provider === 'google') {
2931
const packageFilePath = path.join(cwd, 'package.json')
30-
if (fs.existsSync(packageFilePath)) {
32+
if (fse.existsSync(packageFilePath)) {
3133

3234
// Load in the package.json file.
33-
const packageFile = JSON.parse(fs.readFileSync(packageFilePath).toString())
35+
const packageFile = JSON.parse(fse.readFileSync(packageFilePath).toString())
3436

3537
// Either grab the package.json:main field, or use the index.ts file.
3638
// (This will be transpiled to index.js).
3739
const main = packageFile.main ? packageFile.main.replace(/\.js$/, '.ts') : 'index.ts'
3840

3941
// Check that the file indeed exists.
40-
if (!fs.existsSync(path.join(cwd, main))) {
42+
if (!fse.existsSync(path.join(cwd, main))) {
4143
console.log(`Cannot locate entrypoint, ${main} not found`)
4244
throw new Error('Typescript compilation failed')
4345
}
@@ -55,12 +57,12 @@ export const extractFileNames = (cwd: string, provider: string, functions?: { [k
5557
const fileName = h.substring(0, fnNameLastAppearanceIndex)
5658

5759
// Check if the .ts files exists. If so return that to watch
58-
if (fs.existsSync(path.join(cwd, fileName + 'ts'))) {
60+
if (fse.existsSync(path.join(cwd, fileName + 'ts'))) {
5961
return fileName + 'ts'
6062
}
6163

6264
// Check if the .js files exists. If so return that to watch
63-
if (fs.existsSync(path.join(cwd, fileName + 'js'))) {
65+
if (fse.existsSync(path.join(cwd, fileName + 'js'))) {
6466
return fileName + 'js'
6567
}
6668

@@ -112,21 +114,21 @@ export const getSourceFiles = (
112114

113115
export const getTypescriptConfig = (
114116
cwd: string,
115-
serverless?: Partial<Serverless.Instance>,
117+
serverless?: Partial<ServerlessTSInstance>,
116118
logger?: { log: (str: string) => void }
117119
): ts.CompilerOptions => {
118120
let configFilePath = path.join(cwd, 'tsconfig.json')
119121

120122
if (serverless && serverless.service.custom && serverless.service.custom.typeScript && serverless.service.custom.typeScript.tsconfigFilePath) {
121123
configFilePath = path.join(cwd, serverless.service.custom.typeScript.tsconfigFilePath)
122-
if (!fs.existsSync(configFilePath)) {
124+
if (!fse.existsSync(configFilePath)) {
123125
throw new Error(`Custom Typescript Config File not found at "${configFilePath}"`)
124126
}
125127
}
126128

127-
if (fs.existsSync(configFilePath)) {
129+
if (fse.existsSync(configFilePath)) {
128130

129-
const configFileText = fs.readFileSync(configFilePath).toString()
131+
const configFileText = fse.readFileSync(configFilePath).toString()
130132
const result = ts.parseConfigFileTextToJson(configFilePath, configFileText)
131133
if (result.error) {
132134
throw new Error(JSON.stringify(result.error))

src/watchFiles.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import * as typescript from './typescript'
21
import { watchFile, unwatchFile, Stats } from 'fs'
32

4-
const watchFiles = (
3+
import { ServerlessTSInstance } from './serverlessTypes'
4+
import { getSourceFiles, getTypescriptConfig } from './typescript'
5+
6+
export const watchFiles = (
57
rootFileNames: string[],
68
originalServicePath: string,
7-
serverless: Serverless.Instance,
9+
serverless: ServerlessTSInstance,
810
cb: () => Promise<void>
911
): void => {
10-
const tsConfig = typescript.getTypescriptConfig(originalServicePath, serverless)
11-
let watchedFiles = typescript.getSourceFiles(rootFileNames, tsConfig)
12+
const tsConfig = getTypescriptConfig(originalServicePath, serverless)
13+
let watchedFiles = getSourceFiles(rootFileNames, tsConfig)
1214

1315
watchedFiles.forEach(fileName => {
1416
watchFile(fileName, { persistent: true, interval: 250 }, watchCallback)
@@ -23,7 +25,7 @@ const watchFiles = (
2325
cb()
2426

2527
// use can reference not watched yet file or remove reference to already watched
26-
const newWatchFiles = typescript.getSourceFiles(rootFileNames, tsConfig)
28+
const newWatchFiles = getSourceFiles(rootFileNames, tsConfig)
2729
watchedFiles.forEach(fileName => {
2830
if (!newWatchFiles.includes(fileName)) {
2931
unwatchFile(fileName, watchCallback)
@@ -39,5 +41,3 @@ const watchFiles = (
3941
watchedFiles = newWatchFiles
4042
}
4143
}
42-
43-
export default watchFiles

tests/typescript.extractFileName.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import {extractFileNames} from '../src/typescript'
21
import * as path from 'path'
32

4-
const functions: { [key: string]: Serverless.Function } = {
3+
import { extractFileNames } from '../src/typescript'
4+
import { ServerlessTSFunction } from '../src/serverlessTypes'
5+
6+
const functions: { [key: string]: ServerlessTSFunction } = {
57
hello: {
68
handler: 'tests/assets/hello.handler',
79
package: {

tests/typescript.getTypescriptConfig.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getTypescriptConfig, makeDefaultTypescriptConfig} from '../src/typescript'
1+
import { getTypescriptConfig, makeDefaultTypescriptConfig } from '../src/typescript'
22

33
describe('getTypescriptConfig', () => {
44
it('returns default typescript configuration if the one provided does not exist', () => {
@@ -18,7 +18,7 @@ describe('getTypescriptConfig', () => {
1818
tsconfigFilePath: './some-path'
1919
}
2020
}}
21-
} as any),
21+
}),
2222
).toThrowError('Custom Typescript Config File not found')
2323
})
2424

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"compilerOptions": {
33
"module": "commonjs",
4-
"rootDir": ".",
54
"target": "es6",
5+
"declaration": true,
66
"sourceMap": true,
77
"outDir": "dist"
88
},
9+
"include": ["src/**/*.ts"],
910
"exclude": [
1011
"node_modules"
1112
]

0 commit comments

Comments
 (0)