1
1
/* eslint-disable no-await-in-loop */
2
- /* eslint-disable @typescript-eslint/no-require-imports */
3
- /* eslint-disable @typescript-eslint/no-var-requires */
4
-
5
- import Serverless , {
6
- FunctionDefinitionHandler ,
7
- FunctionDefinitionImage ,
8
- } from "serverless" ;
9
- import Ncc from "@vercel/ncc" ;
10
- import { mkdirSync } from "fs" ;
11
2
12
3
import { chunk , getRootPath } from "@techmmunity/utils" ;
13
- import { writeZip } from "utils/zip/write" ;
14
-
15
- const isNodeRuntime = ( runtime : string ) => runtime . match ( / n o d e / ) ;
16
-
17
- const getAllNodeFunctions = ( serverless : Serverless ) => {
18
- const functions = serverless . service . getAllFunctions ( ) ;
19
-
20
- return functions . filter ( funcName => {
21
- const func = serverless . service . getFunction ( funcName ) ;
22
-
23
- const funcAsImage = func as FunctionDefinitionImage ;
24
-
25
- /*
26
- * If `uri` is provided or simple remote image path, it means the
27
- * image isn't built by Serverless so we shouldn't take care of it
28
- */
29
- if ( typeof funcAsImage . image === "string" ) {
30
- return false ;
31
- }
32
-
33
- return isNodeRuntime (
34
- func . runtime || serverless . service . provider . runtime || "nodejs" ,
35
- ) ;
36
- } ) ;
37
- } ;
38
-
39
- const getHandlerFile = ( handler : any ) => {
40
- // Check if handler is a well-formed path based handler.
41
- const handlerEntries = / ( .* ) \. .* ?$ / . exec ( handler ) ;
42
- // TODO Remove
43
- console . log ( "handlerEntries" , handlerEntries ) ;
44
-
45
- const [ , handlerEntry ] = handlerEntries ! ;
46
-
47
- return handlerEntry ;
48
- } ;
4
+ import { Context } from "types/context" ;
5
+ import { getAllNodeFunctions } from "./helpers/get-all-node-functions" ;
6
+ import { createServerlessFolder } from "./helpers/create-serverless-folder" ;
7
+ import { compileAndZip } from "./helpers/compile-and-zip" ;
49
8
50
9
const CONCURRENCY = 3 ;
51
10
52
- export const createArtifacts = async ( serverless : Serverless ) => {
53
- const packageJson = require ( getRootPath ( "package.json" ) ) ;
54
-
55
- // TODO Remove
56
- console . log ( "packageJson" , packageJson ) ;
11
+ export const createArtifacts = async ( context : Context ) => {
12
+ const functions = getAllNodeFunctions ( context ) ;
57
13
58
- const externals = [
59
- ...Object . keys ( packageJson . dependencies ) ,
60
- ...Object . keys ( packageJson . devDependencies ) ,
61
- ] ;
62
- // TODO Remove
63
- console . log ( "externals" , externals ) ;
64
-
65
- const functions = getAllNodeFunctions ( serverless ) ;
66
- // TODO Remove
67
- console . log ( "functions" , functions ) ;
14
+ const serverlessFolderPath = getRootPath ( ".serverless" ) ;
68
15
69
- const chunks = chunk ( functions , CONCURRENCY ) ;
16
+ createServerlessFolder ( serverlessFolderPath ) ;
70
17
71
- const results : Array < any > = [ ] ;
18
+ const concurrency =
19
+ context . serverless . service . custom ?. ncc ?. concurrency || CONCURRENCY ;
72
20
73
- const serverlessFolderPath = getRootPath ( ".serverless" ) ;
74
- // TODO Remove
75
- console . log ( "serverlessFolderPath" , serverlessFolderPath ) ;
21
+ const chunks = chunk ( functions , concurrency ) ;
76
22
77
- mkdirSync ( serverlessFolderPath ) ;
23
+ const results : Array < PromiseSettledResult < any > > = [ ] ;
78
24
79
25
for ( const c of chunks ) {
80
26
const result = await Promise . allSettled (
81
- c . map ( funcName => {
82
- const func = serverless . service . getFunction (
27
+ c . map ( funcName =>
28
+ compileAndZip ( {
29
+ context,
83
30
funcName,
84
- ) as FunctionDefinitionHandler ;
85
-
86
- const handlerFile = getHandlerFile ( func . handler ) ;
87
- // TODO Remove
88
- console . log ( "handlerFile" , handlerFile ) ;
89
-
90
- const file = `./${ handlerFile } .ts` ;
91
-
92
- /**
93
- * Promise chain is necessary
94
- */
95
- return Ncc ( file , {
96
- externals,
97
- quiet : true ,
98
- minify : true ,
99
- } ) . then ( ( { code } ) => {
100
- const fileName = file . split ( "/" ) . pop ( ) ! . split ( "." ) . shift ( ) ! ;
101
- // TODO Remove
102
- console . log ( "fileName" , fileName ) ;
103
-
104
- return writeZip ( {
105
- fileName,
106
- content : code ,
107
- outputPath : serverlessFolderPath ,
108
- } ) . then ( ( ) => {
109
- // TODO Remove
110
- console . log ( "artifact" , `${ serverlessFolderPath } /${ fileName } .zip` ) ;
111
- // Only sets the artifact if successfully writes the zip file
112
- func . package = {
113
- artifact : `${ serverlessFolderPath } /${ fileName } .zip` ,
114
- } ;
115
- } ) ;
116
- } ) ;
117
- } ) ,
31
+ serverlessFolderPath,
32
+ } ) ,
33
+ ) ,
118
34
) ;
119
35
120
36
results . push ( ...result ) ;
@@ -123,9 +39,12 @@ export const createArtifacts = async (serverless: Serverless) => {
123
39
/**
124
40
* Log all errors
125
41
*/
126
- results . filter ( Boolean ) . forEach ( ( ) => {
127
- serverless . cli . log ( "Fail to compile a file" , undefined , {
128
- color : "red" ,
42
+ results
43
+ . filter ( ( { status } ) => status !== "fulfilled" )
44
+ . forEach ( ( err : any ) => {
45
+ context . serverless . cli . log ( "Fail to compile a file" , undefined , {
46
+ color : "red" ,
47
+ } ) ;
48
+ console . error ( err ) ;
129
49
} ) ;
130
- } ) ;
131
50
} ;
0 commit comments