2023-12-27 15:21:49 +00:00
|
|
|
import { readFileSync, writeFileSync, readdirSync } from 'node:fs';
|
|
|
|
import { spawnSync } from 'node:child_process';
|
2020-05-04 10:20:10 +00:00
|
|
|
|
|
|
|
// List of commands to run
|
|
|
|
const commands = [];
|
|
|
|
|
2021-05-14 18:39:12 +00:00
|
|
|
// api-extractor command line
|
|
|
|
const extractor = (name) =>
|
|
|
|
`api-extractor run --local --verbose --config api-extractor.${name}.json`;
|
|
|
|
|
2020-05-04 10:20:10 +00:00
|
|
|
// Parse command line
|
|
|
|
const compile = {
|
2021-04-30 07:01:30 +00:00
|
|
|
tsc: true,
|
|
|
|
bundles: true,
|
2021-04-30 07:39:11 +00:00
|
|
|
api: true,
|
2021-05-14 18:39:12 +00:00
|
|
|
finish: true,
|
2020-05-04 10:20:10 +00:00
|
|
|
};
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-29 06:45:30 +00:00
|
|
|
// Compile packages
|
2020-05-04 10:20:10 +00:00
|
|
|
Object.keys(compile).forEach((key) => {
|
2021-05-14 18:39:12 +00:00
|
|
|
if (!compile[key]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (key) {
|
|
|
|
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],
|
|
|
|
});
|
2020-05-04 10:20:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-14 18:39:12 +00:00
|
|
|
/**
|
|
|
|
* Get all api-extractor.*.json files
|
|
|
|
*/
|
|
|
|
function apiFiles() {
|
2023-12-27 15:21:49 +00:00
|
|
|
return readdirSync('.')
|
2021-05-14 18:39:12 +00:00
|
|
|
.map((item) => {
|
|
|
|
const parts = item.split('.');
|
|
|
|
if (parts.pop() !== 'json' || parts.shift() !== 'api-extractor') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return parts.length === 1 ? parts[0] : '';
|
|
|
|
})
|
|
|
|
.filter((item) => item !== '');
|
|
|
|
}
|
|
|
|
|
2020-05-04 10:20:10 +00:00
|
|
|
/**
|
|
|
|
* Run next command
|
|
|
|
*/
|
2021-05-14 18:39:12 +00:00
|
|
|
function next() {
|
2020-05-04 10:20:10 +00:00
|
|
|
const item = commands.shift();
|
|
|
|
if (item === void 0) {
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:39:12 +00:00
|
|
|
if (typeof item === 'function') {
|
|
|
|
item();
|
|
|
|
process.nextTick(next);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-04 10:20:10 +00:00
|
|
|
if (item.cwd === void 0) {
|
2023-12-27 15:21:49 +00:00
|
|
|
item.cwd = '.';
|
2020-05-04 10:20:10 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 15:21:49 +00:00
|
|
|
const result = spawnSync(item.cmd, item.args, {
|
2020-05-04 10:20:10 +00:00
|
|
|
cwd: item.cwd,
|
|
|
|
stdio: 'inherit',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.status === 0) {
|
|
|
|
process.nextTick(next);
|
|
|
|
} else {
|
|
|
|
process.exit(result.status);
|
|
|
|
}
|
2021-05-14 18:39:12 +00:00
|
|
|
}
|
2020-05-04 10:20:10 +00:00
|
|
|
next();
|
2021-05-14 18:39:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup
|
|
|
|
*/
|
|
|
|
function cleanup() {
|
|
|
|
// Merge TypeScript files
|
2023-12-27 15:21:49 +00:00
|
|
|
const sourceDir = './src/';
|
|
|
|
const distDir = './dist/';
|
2021-05-14 18:39:12 +00:00
|
|
|
|
|
|
|
function createTypes() {
|
|
|
|
// Get Svelte file, split it. Import and content should be separated by empty line
|
2023-12-27 15:21:49 +00:00
|
|
|
const svelteParts = readFileSync(sourceDir + 'svelte.d.ts', 'utf8')
|
2021-05-14 18:39:12 +00:00
|
|
|
.trim()
|
|
|
|
.replace(/\r/g, '')
|
|
|
|
.split('\n\n');
|
2022-02-21 04:40:10 +00:00
|
|
|
if (svelteParts.length < 2) {
|
2021-05-14 18:39:12 +00:00
|
|
|
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 +
|
2023-12-27 15:21:49 +00:00
|
|
|
readFileSync(distDir + item.source, 'utf8')
|
2021-05-14 18:39:12 +00:00
|
|
|
.replace('export { }', '')
|
|
|
|
.trim() +
|
|
|
|
svelteContent;
|
2023-12-27 15:21:49 +00:00
|
|
|
writeFileSync(distDir + item.target, content, 'utf8');
|
2021-05-14 18:39:12 +00:00
|
|
|
console.log(`Created dist/${item.target}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyComponents() {
|
|
|
|
['Icon.svelte', 'OfflineIcon.svelte'].forEach((name) => {
|
2023-12-27 15:21:49 +00:00
|
|
|
const content = readFileSync(sourceDir + name, 'utf8');
|
|
|
|
writeFileSync(distDir + name, content, 'utf8');
|
2021-05-14 18:39:12 +00:00
|
|
|
console.log(`Copied dist/${name}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createTypes();
|
|
|
|
copyComponents();
|
|
|
|
}
|