Skip to content

Added support for Google Cloud Functions #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ All files from `package/include` will be included in the final build file. See [

## Usage

### Google Cloud Functions

When using with Google Cloud Functions via the [serverless-google-cloudfunctions](https://github.com/serverless/serverless-google-cloudfunctions)
plugin, you simply have to provide a `main` field in your `package.json`:

```js
{
// ...
"main": "handler.js",
// ..
}
```

And this plugin will automatically compile your typescript correctly. Note
that the field must refer to the compiled file name, namely, ending with a `.js`
extension.

If a `main` field was not found, then this plugin will use `index.js`. Before
compilation begins, it will check to see that the file indicated exists with a
`.ts` extension before actually trying to compile it.

### Automatic compilation

The normal Serverless deploy procedure will automatically compile with Typescript:
Expand Down
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "handler.js",
"dependencies": {
"lodash": "^4.17.4"
},
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ServerlessPlugin {
}

get rootFileNames() {
return typescript.extractFileNames(this.functions)
return typescript.extractFileNames(this.originalServicePath, this.serverless.service.provider.name, this.functions)
}

prepare() {
Expand Down Expand Up @@ -118,15 +118,14 @@ export class ServerlessPlugin {
this.serverless.config.servicePath = path.join(this.originalServicePath, buildFolder)
}

const tsFileNames = typescript.extractFileNames(this.functions)
const tsconfig = typescript.getTypescriptConfig(
this.originalServicePath,
this.isWatching ? null : this.serverless.cli
)

tsconfig.outDir = buildFolder

const emitedFiles = await typescript.run(tsFileNames, tsconfig)
const emitedFiles = await typescript.run(this.rootFileNames, tsconfig)
await this.copyExtras()
return emitedFiles
}
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export interface ServerlessInstance {
servicePath: string
}
service: {
provider: {
name: string
}
functions: { [key: string]: ServerlessFunction }
package: ServerlessPackage
getAllFunctions: () => string[]
Expand Down
30 changes: 29 additions & 1 deletion src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,35 @@ export function makeDefaultTypescriptConfig() {
return defaultTypescriptConfig
}

export function extractFileNames(functions: { [key: string]: ServerlessFunction }): string[] {
export function extractFileNames(cwd: string, provider: string, functions?: { [key: string]: ServerlessFunction }): string[] {

// The Google provider will use the entrypoint not from the definition of the
// handler function, but instead from the package.json:main field, or via a
// index.js file. This check reads the current package.json in the same way
// that we already read the tsconfig.json file, by inspecting the current
// working directory. If the packageFile does not contain a valid main, then
// it instead selects the index.js file.
if (provider === 'google') {
const packageFilePath = path.join(cwd, 'package.json')
if (fs.existsSync(packageFilePath)) {

// Load in the package.json file.
const packageFile = JSON.parse(fs.readFileSync(packageFilePath).toString())

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

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

return [main]
}
}

return _.values(functions)
.map(fn => fn.handler)
.map(h => {
Expand Down
16 changes: 13 additions & 3 deletions tests/typescript.extractFileName.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {extractFileNames} from '../src/typescript'
import {ServerlessFunction} from '../src/types'
import * as path from 'path'

const functions: { [key: string]: ServerlessFunction } = {
hello: {
Expand All @@ -26,9 +27,9 @@ const functions: { [key: string]: ServerlessFunction } = {
}

describe('extractFileName', () => {
it('get function filenames from serverless service', () => {
it('get function filenames from serverless service for a non-google provider', () => {
expect(
extractFileNames(functions),
extractFileNames(process.cwd(), 'aws', functions),
).toEqual(
[
'my-folder/hello.ts',
Expand All @@ -37,5 +38,14 @@ describe('extractFileName', () => {
],
)
})
})

it('get function filename from serverless service for a google provider', () => {
expect(
extractFileNames(path.join(process.cwd(), 'example'), 'google')
).toEqual(
[
'handler.ts'
]
)
})
})