forked from nolimits4web/swiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-types.js
124 lines (115 loc) · 4.18 KB
/
build-types.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
/* eslint no-console: "off" */
const path = require('path');
const globby = require('globby');
const elapsed = require('elapsed-time-logger');
const chalk = require('chalk');
const fs = require('fs-extra');
const { outputDir } = require('./utils/output-dir');
async function buildTypes() {
elapsed.start('types');
let coreEventsReact = '';
let coreEventsVue = '';
let coreEventsSvelte = '';
let modulesEventsReact = '';
let modulesEventsVue = '';
let modulesEventsSvelte = '';
const replaceInstances = (content) => {
return content
.replace(/this: Swiper, /g, '')
.replace(/this: Swiper/g, '')
.replace(/swiper: Swiper/g, 'swiper: SwiperClass');
};
const getCoreEventsContent = async () => {
let coreEventsContent = await fs.readFile(
path.resolve(__dirname, '../src/types/swiper-events.d.ts'),
'utf-8',
);
coreEventsContent = coreEventsContent
.split('// CORE_EVENTS_START')[1]
.split('// CORE_EVENTS_END')[0];
coreEventsReact = replaceInstances(
coreEventsContent.replace(/ ([a-zA-Z]*): \(/g, (string, name) => {
return ` on${name[0].toUpperCase()}${name.substr(1)}?: (`;
}),
);
coreEventsVue = replaceInstances(
coreEventsContent.replace(/ ([a-zA-Z_?]*): \(/g, (string, name) => {
return ` ${name.replace('?', '')}: (`;
}),
);
coreEventsSvelte = replaceInstances(
coreEventsContent
.replace(/ ([a-zA-Z_?]*): \(/g, (string, name) => {
return ` ${name.replace('?', '')}: CustomEvent<[`;
})
.replace(/\) => void;/g, ']>;')
.replace(/\) => any;/g, ']>;'),
);
};
const getModulesEventsContent = async () => {
const eventsFiles = await globby('src/types/modules/*.d.ts');
await Promise.all(
eventsFiles.map(async (eventsFile) => {
if (eventsFile.indexOf('public-api') > -1) {
return;
}
let eventsContent = await fs.readFile(eventsFile, 'utf-8');
eventsContent = eventsContent.split('Events {')[1].split('}')[0].trim();
if (eventsContent.length) {
modulesEventsReact += replaceInstances(
eventsContent.replace(/ ([a-zA-Z]*): \(/g, (string, name) => {
return ` on${name[0].toUpperCase()}${name.substr(1)}?: (`;
}),
);
modulesEventsVue += replaceInstances(
eventsContent.replace(/ ([a-zA-Z_?]*): \(/g, (string, name) => {
return ` ${name.replace('?', '')}: (`;
}),
);
modulesEventsSvelte += replaceInstances(
eventsContent
.replace(/ ([a-zA-Z_?]*): \(/g, (string, name) => {
return ` ${name.replace('?', '')}: CustomEvent<[`;
})
.replace(/\) => void;/g, ']>;')
.replace(/\) => any;/g, ']>;'),
);
}
}),
);
};
let files;
await Promise.all([
getCoreEventsContent(),
getModulesEventsContent(),
(async () => {
files = await globby('**/*.d.ts', { cwd: path.resolve(__dirname, '../src') });
})(),
]);
await Promise.all(
files.map(async (file) => {
const fileContent = await fs.readFile(path.resolve(__dirname, '../src', file), 'utf-8');
const destPath = path.resolve(__dirname, `../${outputDir}`, file);
await fs.ensureDir(path.dirname(destPath));
const processTypingFile = async (eventsCode, modulesCode) => {
const content = fileContent
.replace('// MODULES_EVENTS', eventsCode)
.replace('// CORE_EVENTS', modulesCode);
return fs.writeFile(destPath, content);
};
if (file.includes('swiper-react.d.ts')) {
return processTypingFile(coreEventsReact, modulesEventsReact);
}
if (file.includes('swiper-vue.d.ts')) {
return processTypingFile(coreEventsVue, modulesEventsVue);
}
if (file.includes('swiper-svelte.d.ts')) {
return processTypingFile(coreEventsSvelte, modulesEventsSvelte);
}
return fs.writeFile(destPath, fileContent);
}),
);
elapsed.end('types', chalk.green('Types build completed!'));
}
module.exports = buildTypes;