forked from OfficeDev/microsoft-teams-library-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-dts.js
77 lines (65 loc) · 1.95 KB
/
generate-dts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const libraryName = 'microsoftTeams';
const fs = require('fs');
const rimraf = require('rimraf');
const timeout = 2000;
function DtsBundlePlugin() {}
DtsBundlePlugin.prototype.apply = function(compiler) {
const self = this;
compiler.plugin('done', (_compilation, callback) => {
const dtsBuilder = require('dts-builder');
dtsBuilder.generateBundles([
{
name: libraryName,
alias: libraryName,
sourceDir: './dts',
destDir: './dist',
},
]);
console.log(
'Waiting for 2 seconds so that the dts can be merged before proceeding. If this fails the either increase the wait time or just re-run the task.',
);
setTimeout(() => patchDTS(callback), timeout);
});
};
function patchDTS(callback) {
const self = this;
console.log('Replacing the references to teamsJs and regularizing it.');
fs.readFile('./dist/microsoftTeams.d.ts', 'utf8', (err, data) => {
if (err) {
return console.log(err);
}
const result = replace(data)(/declare module 'microsoftTeams'/gm, "declare module '@microsoft/teams-js'")(
/^import microsoftTeams.*/g,
'',
)(/^var _default: void;/, '')(/export default _default;/, '')(/^\s*[\r\n]/gm, '')();
fs.writeFile('./dist/microsoftTeams.d.ts', result, 'utf8', err => {
if (err) {
return console.log(err);
}
fs.rename('./dist/microsoftTeams.d.ts', './/dist/MicrosoftTeams.d.ts', err => {
if (err) {
console.log('ERROR: ' + err);
throw err;
}
rimraf('./dts', () => {
if (callback) {
callback();
}
});
});
});
});
}
function replace(source) {
let current = source;
// tslint:disable-next-line:only-arrow-functions
return function stage(regex, value) {
if (arguments.length === 0) {
return current;
} else {
current = current.replace(regex, value);
return stage;
}
};
}
module.exports = DtsBundlePlugin;