Skip to content

Commit

Permalink
fix: concat missing global types (#306)
Browse files Browse the repository at this point in the history
api extractor currently removes global declared types so we need to concat them manually after every build
  • Loading branch information
exreplay authored Jan 5, 2021
1 parent f8c73a8 commit bb1091a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/@averjs/mailer/api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"mainEntryPointFilePath": "./dist/packages/@averjs/<unscopedPackageName>/lib/index.d.ts",
"dtsRollup": {
"publicTrimmedFilePath": "./dist/<unscopedPackageName>.d.ts"

}
}
3 changes: 2 additions & 1 deletion packages/@averjs/mailer/lib/global.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-namespace */
import Email from 'email-templates';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
/* concat start */
declare global {
namespace Express {
export interface Request {
Expand All @@ -18,3 +18,4 @@ declare global {
}
}
}
/* concat end */
6 changes: 4 additions & 2 deletions packages/@averjs/websocket/lib/global.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-disable @typescript-eslint/no-namespace */
import { Server } from 'socket.io';
/* concat start */
import { Server as SocketIoServer } from 'socket.io';

declare global {
namespace Express {
interface Request {
io: Server;
io: SocketIoServer;
}
}
}
/* concat end */

export {};
16 changes: 16 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export default class Build {
fs.rmdirSync(path.resolve(pkg.path, './dist/packages'), {
recursive: true
});

this.appendGlobalTypes(pkg.path, pkg.pkg.types);
}

buildSpinner.succeed(`Built package ${pkg.pkg.name} successfully`);
Expand All @@ -134,4 +136,18 @@ export default class Build {
}
}
}

appendGlobalTypes(pkgPath: string, dtsFile = '') {
const globalPath = path.resolve(pkgPath, './lib/global.ts');
const dtsPath = path.resolve(pkgPath, dtsFile);

if (fs.existsSync(globalPath)) {
const globalTypes = fs.readFileSync(globalPath, 'utf-8');
const content = /\/\* concat start \*\/(?<content>(.|\n)*?)\/\* concat end \*\//.exec(
globalTypes
)?.groups?.content;
const dtsContent = fs.readFileSync(dtsPath, 'utf-8');
fs.writeFileSync(dtsPath, `${dtsContent}${content || ''}`, 'utf-8');
}
}
}

0 comments on commit bb1091a

Please sign in to comment.