forked from iconify/iconify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
222 lines (198 loc) · 4.28 KB
/
build.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
const packagesDir = path.dirname(__dirname);
// List of commands to run
const commands = [];
// api-extractor command line
const extractor = (name) =>
`api-extractor run --local --verbose --config api-extractor.${name}.json`;
// Parse command line
const compile = {
core: false,
tsc: true,
bundles: true,
api: true,
finish: true,
};
process.argv.slice(2).forEach((cmd) => {
if (cmd.slice(0, 2) !== '--') {
return;
}
const parts = cmd.slice(2).split('-');
if (parts.length === 2) {
// Parse 2 part commands like --with-lib
const key = parts.pop();
if (compile[key] === void 0) {
return;
}
switch (parts.shift()) {
case 'with':
// enable module
compile[key] = true;
break;
case 'without':
// disable module
compile[key] = false;
break;
case 'only':
// disable other modules
Object.keys(compile).forEach((key2) => {
compile[key2] = key2 === key;
});
break;
}
}
});
// Check if required modules in same monorepo are available
const fileExists = (file) => {
try {
fs.statSync(file);
} catch (e) {
return false;
}
return true;
};
if (compile.dist && !fileExists(packagesDir + '/core/lib/modules.mjs')) {
compile.core = true;
}
// Compile core before compiling this package
if (compile.core) {
commands.push({
cmd: 'npm',
args: ['run', 'build'],
cwd: packagesDir + '/core',
});
}
// Compile other packages
Object.keys(compile).forEach((key) => {
if (!compile[key]) {
return;
}
switch (key) {
case 'core':
break;
case 'api':
apiFiles().forEach((name) => {
const cmd = extractor(name).split(' ');
commands.push({
cmd: cmd.shift(),
args: cmd,
});
});
break;
case 'finish':
commands.push(cleanup);
break;
default:
commands.push({
cmd: 'npm',
args: ['run', 'build:' + key],
});
}
});
/**
* Get all api-extractor.*.json files
*/
function apiFiles() {
return fs
.readdirSync(__dirname)
.map((item) => {
const parts = item.split('.');
if (parts.pop() !== 'json' || parts.shift() !== 'api-extractor') {
return '';
}
return parts.length === 1 ? parts[0] : '';
})
.filter((item) => item !== '');
}
/**
* Run next command
*/
function next() {
const item = commands.shift();
if (item === void 0) {
process.exit(0);
}
if (typeof item === 'function') {
item();
process.nextTick(next);
return;
}
if (item.cwd === void 0) {
item.cwd = __dirname;
}
const result = child_process.spawnSync(item.cmd, item.args, {
cwd: item.cwd,
stdio: 'inherit',
});
if (result.status === 0) {
process.nextTick(next);
} else {
process.exit(result.status);
}
}
next();
/**
* Cleanup
*/
function cleanup() {
// Merge TypeScript files
const sourceDir = __dirname + '/src/';
const distDir = __dirname + '/dist/';
function createTypes() {
// Get Svelte file, split it. Import and content should be separated by empty line
const svelteParts = fs
.readFileSync(sourceDir + 'svelte.d.ts', 'utf8')
.trim()
.replace(/\r/g, '')
.split('\n\n');
if (svelteParts.length < 2) {
throw new Error(
'Error parsing svelte.d.ts. Imports and content should be separated by 2 new lines'
);
}
const svelteImport = svelteParts.shift() + '\n\n';
const svelteContent = '\n\n' + svelteParts.join('\n\n');
// Merge files
[
// Full component
{
source: 'iconify.d.ts',
target: 'index.d.ts',
},
{
source: 'iconify.d.ts',
target: 'Icon.svelte.d.ts',
},
// Offline component
{
source: 'offline-iconify.d.ts',
target: 'offline.d.ts',
},
{
source: 'offline-iconify.d.ts',
target: 'OfflineIcon.svelte.d.ts',
},
].forEach((item) => {
const content =
svelteImport +
fs
.readFileSync(distDir + item.source, 'utf8')
.replace('export { }', '')
.trim() +
svelteContent;
fs.writeFileSync(distDir + item.target, content, 'utf8');
console.log(`Created dist/${item.target}`);
});
}
function copyComponents() {
['Icon.svelte', 'OfflineIcon.svelte'].forEach((name) => {
const content = fs.readFileSync(sourceDir + name, 'utf8');
fs.writeFileSync(distDir + name, content, 'utf8');
console.log(`Copied dist/${name}`);
});
}
createTypes();
copyComponents();
}