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

Commit 78637cf

Browse files
tpscrptKingDarBoja
andauthored
feat/fix: CLI Option tsconfigFilePath, code sanity fixes (#8)
* feat/fix: CLI Option tsconfigFilePath, code sanity fixes - add yarn.lock which had uncommitted changes (prettier); - rename serverlessTypes.ts to types.ts (convention); - move watchFiles into private method of index.ts main class -- utility below; - add cli option tsconfigFilePath for more flexibility in specifying tsconfig to use; - compute which tsconfig to use based on options, custom:typeScript:... or default (save in this.tsconfigFilePath of main class) - only pass this.tsconfigFilePath as a string to getTypescriptConfig; - run yarn lint on the project * chore: enable GH action on pull request * chore: set prettier endOfLine as auto * chore: update TypeScript dev dependency to 3.8.3 * tests: properly pass tsconfig path on test suite * styles: apply prettier to index Co-authored-by: KingDarBoja <stevenbojato04@gmail.com>
1 parent 351c52b commit 78637cf

11 files changed

Lines changed: 113 additions & 105 deletions

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"singleQuote": true,
3-
"trailingComma": "all"
3+
"trailingComma": "all",
4+
"endOfLine": "auto"
45
}

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"mock-fs": "4.10.4",
6262
"prettier": "^2.0.2",
6363
"ts-jest": "25.2.1",
64-
"typescript": "^3.6.4"
64+
"typescript": "^3.8.3"
6565
},
6666
"dependencies": {
6767
"fs-extra": "^8.1.0",

src/index.ts

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@ import {
77
ServerlessTSInstance,
88
ServerlessTSOptions,
99
ServerlessTSFunction,
10-
} from './serverlessTypes';
11-
import { extractFileNames, getTypescriptConfig, run } from './typescript';
12-
import { watchFiles } from './watchFiles';
10+
} from './types';
11+
import {
12+
extractFileNames,
13+
getTypescriptConfig,
14+
run,
15+
getSourceFiles,
16+
} from './typescript';
17+
import { unwatchFile, watchFile, Stats } from 'fs-extra';
1318

1419
const SERVERLESS_FOLDER = '.serverless';
1520
const BUILD_FOLDER = '.build';
1621

1722
class TypeScriptPlugin {
1823
private originalServicePath: string;
1924
private isWatching: boolean;
25+
private tsconfigFilePath: string;
2026

2127
serverless: ServerlessTSInstance;
2228
options: ServerlessTSOptions;
@@ -25,7 +31,10 @@ class TypeScriptPlugin {
2531
constructor(serverless: ServerlessTSInstance, options: ServerlessTSOptions) {
2632
this.serverless = serverless;
2733
this.options = options;
28-
34+
this.tsconfigFilePath =
35+
options.tsconfigFilePath ||
36+
serverless.service.custom?.typeScript?.tsconfigFilePath ||
37+
'tsconfig.json';
2938
this.hooks = {
3039
'before:run:run': async (): Promise<void> => {
3140
this.compileTs();
@@ -136,7 +145,7 @@ class TypeScriptPlugin {
136145
this.serverless.cli.log(`Watch function ${this.options.function}...`);
137146

138147
this.isWatching = true;
139-
watchFiles(
148+
this.watchFiles(
140149
this.rootFileNames,
141150
this.originalServicePath,
142151
this.serverless,
@@ -154,7 +163,7 @@ class TypeScriptPlugin {
154163
this.serverless.cli.log('Watching typescript files...');
155164

156165
this.isWatching = true;
157-
watchFiles(
166+
this.watchFiles(
158167
this.rootFileNames,
159168
this.originalServicePath,
160169
this.serverless,
@@ -178,7 +187,7 @@ class TypeScriptPlugin {
178187

179188
const tsconfig = getTypescriptConfig(
180189
this.originalServicePath,
181-
this.serverless,
190+
this.tsconfigFilePath,
182191
this.isWatching ? null : this.serverless.cli,
183192
);
184193

@@ -322,6 +331,52 @@ class TypeScriptPlugin {
322331
throw error;
323332
});
324333
}
334+
335+
private watchFiles(
336+
rootFileNames: string[],
337+
originalServicePath: string,
338+
serverless: ServerlessTSInstance,
339+
cb: () => Promise<void>,
340+
): void {
341+
const tsConfig = getTypescriptConfig(
342+
originalServicePath,
343+
this.tsconfigFilePath,
344+
);
345+
let watchedFiles = getSourceFiles(rootFileNames, tsConfig);
346+
347+
const watchCallback = (curr: Stats, prev: Stats): void => {
348+
// Check timestamp
349+
if (+curr.mtime <= +prev.mtime) {
350+
return;
351+
}
352+
353+
cb();
354+
355+
// use can reference not watched yet file or remove reference to already watched
356+
const newWatchFiles = getSourceFiles(rootFileNames, tsConfig);
357+
watchedFiles.forEach((fileName) => {
358+
if (!newWatchFiles.includes(fileName)) {
359+
unwatchFile(fileName, watchCallback);
360+
}
361+
});
362+
363+
newWatchFiles.forEach((fileName) => {
364+
if (!watchedFiles.includes(fileName)) {
365+
watchFile(
366+
fileName,
367+
{ persistent: true, interval: 250 },
368+
watchCallback,
369+
);
370+
}
371+
});
372+
373+
watchedFiles = newWatchFiles;
374+
};
375+
376+
watchedFiles.forEach((fileName) => {
377+
watchFile(fileName, { persistent: true, interval: 250 }, watchCallback);
378+
});
379+
}
325380
}
326381

327382
export = TypeScriptPlugin;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface ServerlessTSOptions {
3030
function?: string;
3131
watch?: boolean;
3232
extraServicePath?: string;
33+
tsconfigFilePath?: string;
3334
}
3435

3536
export interface ServerlessTSFunction {

src/typescript.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as fse from 'fs-extra';
33
import * as _ from 'lodash';
44
import * as path from 'path';
55

6-
import { ServerlessTSFunction, ServerlessTSInstance } from './serverlessTypes';
6+
import { ServerlessTSFunction } from './types';
77

88
export const makeDefaultTypescriptConfig = (): ts.CompilerOptions => {
99
const defaultTypescriptConfig: ts.CompilerOptions = {
@@ -136,27 +136,10 @@ export const getSourceFiles = (
136136

137137
export const getTypescriptConfig = (
138138
cwd: string,
139-
serverless?: Partial<ServerlessTSInstance>,
139+
tsconfigFilePath: string,
140140
logger?: { log: (str: string) => void },
141141
): ts.CompilerOptions => {
142-
let configFilePath = path.join(cwd, 'tsconfig.json');
143-
144-
if (
145-
serverless &&
146-
serverless.service.custom &&
147-
serverless.service.custom.typeScript &&
148-
serverless.service.custom.typeScript.tsconfigFilePath
149-
) {
150-
configFilePath = path.join(
151-
cwd,
152-
serverless.service.custom.typeScript.tsconfigFilePath,
153-
);
154-
if (!fse.existsSync(configFilePath)) {
155-
throw new Error(
156-
`Custom Typescript Config File not found at "${configFilePath}"`,
157-
);
158-
}
159-
}
142+
const configFilePath = path.join(cwd, tsconfigFilePath);
160143

161144
if (fse.existsSync(configFilePath)) {
162145
const configFileText = fse.readFileSync(configFilePath).toString();

src/watchFiles.ts

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

tests/index.functions.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import TypeScriptPlugin from '../src';
2-
import {
3-
ServerlessTSInstance,
4-
ServerlessTSFunctionMap,
5-
} from '../src/serverlessTypes';
2+
import { ServerlessTSInstance, ServerlessTSFunctionMap } from '../src/types';
63

74
const createInstance = (
85
functions: ServerlessTSFunctionMap,

tests/typescript.extractFileName.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from 'path';
22

33
import { extractFileNames } from '../src/typescript';
4-
import { ServerlessTSFunction } from '../src/serverlessTypes';
4+
import { ServerlessTSFunction } from '../src/types';
55

66
const functions: { [key: string]: ServerlessTSFunction } = {
77
hello: {

tests/typescript.getTypescriptConfig.test.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,14 @@ import {
55

66
describe('getTypescriptConfig', () => {
77
it('returns default typescript configuration if the one provided does not exist', () => {
8-
expect(getTypescriptConfig('/ciaone/my-folder')).toEqual(
8+
expect(getTypescriptConfig(process.cwd(), '/ciaone/my-folder')).toEqual(
99
makeDefaultTypescriptConfig(),
1010
);
1111
});
1212

13-
it('should throw an error if configured typescript configuration does not exist', () => {
14-
expect(() =>
15-
getTypescriptConfig(process.cwd(), {
16-
service: {
17-
custom: {
18-
typeScript: {
19-
tsconfigFilePath: './some-path',
20-
},
21-
},
22-
},
23-
}),
24-
).toThrowError('Custom Typescript Config File not found');
25-
});
26-
2713
it('returns configured typescript configuration if provided', () => {
2814
expect(
29-
getTypescriptConfig(process.cwd(), {
30-
service: {
31-
custom: {
32-
typeScript: {
33-
tsconfigFilePath: './tests/custom.tsconfig.json',
34-
},
35-
},
36-
},
37-
}).target,
15+
getTypescriptConfig(process.cwd(), './tests/custom.tsconfig.json').target,
3816
).toEqual(2);
3917
});
4018
});

0 commit comments

Comments
 (0)