2023-09-24 18:34:07 +02:00
|
|
|
import { execa } from 'execa';
|
2022-07-05 16:24:49 +02:00
|
|
|
import { generateTemplateFilesBatch } from 'generate-template-files';
|
2021-06-09 09:53:55 +02:00
|
|
|
import { Listr } from 'listr2';
|
2023-09-24 18:34:07 +02:00
|
|
|
import { existsSync, promises as fs, readFileSync } from 'node:fs';
|
|
|
|
import os from 'node:os';
|
|
|
|
import path, { basename, dirname } from 'node:path';
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { flutterIncompatibleNames, incompatibleNames } from '../constants.js';
|
2023-05-03 14:19:37 +02:00
|
|
|
import { buildVueIcons } from './buildVue.js';
|
2021-06-09 09:53:55 +02:00
|
|
|
|
|
|
|
// Paths
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
const rootDir = path.join(__dirname, '..');
|
|
|
|
const iconoirIconsDir = path.join(rootDir, 'icons');
|
2023-01-29 20:49:22 +01:00
|
|
|
const ignoreCleanFilenames = ['IconoirContext.tsx', 'server'];
|
2021-06-09 09:53:55 +02:00
|
|
|
|
|
|
|
// Targets for building icons
|
|
|
|
const targets = {
|
2021-10-13 16:27:47 +02:00
|
|
|
'meta-data': { path: 'meta-data.json' },
|
|
|
|
css: { path: 'css/iconoir.css' },
|
2022-07-05 16:24:49 +02:00
|
|
|
'iconoir-flutter': { flutter: true, path: 'packages/iconoir-flutter' },
|
2021-10-13 16:27:47 +02:00
|
|
|
'iconoir-react': { react: true, path: 'packages/iconoir-react' },
|
|
|
|
'iconoir-react-native': {
|
|
|
|
react: true,
|
|
|
|
path: 'packages/iconoir-react-native',
|
|
|
|
},
|
2023-05-03 14:19:37 +02:00
|
|
|
'iconoir-vue': {
|
|
|
|
vue: true,
|
|
|
|
path: 'packages/iconoir-vue',
|
|
|
|
},
|
2021-06-09 09:53:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Get targets from command line arguments
|
|
|
|
// (build all targets if no arguments)
|
|
|
|
const args = process.argv.slice(2);
|
|
|
|
const cliTargets = [];
|
|
|
|
args.forEach((target) => {
|
|
|
|
if (target in targets) {
|
|
|
|
cliTargets.push(target);
|
|
|
|
} else {
|
|
|
|
console.error(`Target '${target}' doesn't exist!\n\nPossible targets are:`);
|
|
|
|
for (const [targetName] of Object.entries(targets)) {
|
|
|
|
console.log(`- ${targetName}`);
|
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Build tasks
|
|
|
|
const tasks = new Listr(
|
|
|
|
[
|
|
|
|
{
|
2021-10-13 16:27:47 +02:00
|
|
|
title: 'Fetching icons',
|
2021-06-09 09:53:55 +02:00
|
|
|
task: async (ctx) => {
|
2023-09-05 12:54:49 +02:00
|
|
|
const iconFiles = await fs.readdir(iconoirIconsDir);
|
2023-09-24 18:34:07 +02:00
|
|
|
ctx.iconoirIconsFiles = iconFiles.filter((file) =>
|
|
|
|
file.endsWith('.svg'),
|
|
|
|
);
|
2021-06-09 09:53:55 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-13 16:27:47 +02:00
|
|
|
title: 'Building targets',
|
|
|
|
skip: (ctx) => !ctx.iconoirIconsFiles,
|
|
|
|
task: (_, task) =>
|
|
|
|
task.newListr(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Building meta-data file',
|
|
|
|
enabled: () =>
|
|
|
|
cliTargets.length === 0 || cliTargets.includes('meta-data'),
|
|
|
|
task: async (ctx) => {
|
|
|
|
await fs.writeFile(
|
|
|
|
path.join(rootDir, targets['meta-data'].path),
|
2023-09-24 18:34:07 +02:00
|
|
|
JSON.stringify({ icons: ctx.iconoirIconsFiles }),
|
2021-10-13 16:27:47 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Building CSS file',
|
|
|
|
enabled: () =>
|
|
|
|
cliTargets.length === 0 || cliTargets.includes('css'),
|
|
|
|
task: async (ctx) => {
|
|
|
|
const content = [
|
2022-04-23 20:05:22 +02:00
|
|
|
(
|
|
|
|
await fs.readFile(
|
|
|
|
path.join(__dirname, 'header.css'),
|
2023-09-24 18:34:07 +02:00
|
|
|
'utf8',
|
2022-04-23 20:05:22 +02:00
|
|
|
)
|
|
|
|
).replace('[YEAR]', new Date().getFullYear()),
|
2021-10-13 16:27:47 +02:00
|
|
|
];
|
|
|
|
ctx.iconoirIconsFiles.forEach((file) => {
|
2022-04-23 20:05:22 +02:00
|
|
|
const fileContents = readFileSync(
|
2023-09-24 18:34:07 +02:00
|
|
|
path.join(__dirname, '../icons/', file),
|
2022-04-23 20:05:22 +02:00
|
|
|
)
|
|
|
|
.toString()
|
2023-01-16 16:54:05 +01:00
|
|
|
.replace(/\n/g, '')
|
2023-05-10 14:08:57 +02:00
|
|
|
.replace(/(width|height)="[0-9]+px"/g, '')
|
2023-01-16 16:54:05 +01:00
|
|
|
.replace(/[ ]+/g, ' ');
|
2021-10-13 16:27:47 +02:00
|
|
|
content.push(
|
|
|
|
`.iconoir-${
|
|
|
|
path.parse(file).name
|
2023-09-24 18:34:07 +02:00
|
|
|
}::before{mask-image:url('data:image/svg+xml;charset=utf-8,${fileContents}');-webkit-mask-image:url('data:image/svg+xml;charset=utf-8,${fileContents}');}`,
|
2021-10-13 16:27:47 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
await fs.writeFile(
|
|
|
|
path.join(rootDir, targets.css.path),
|
2023-09-24 18:34:07 +02:00
|
|
|
content,
|
2021-10-13 16:27:47 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Building React libraries',
|
|
|
|
enabled: () =>
|
|
|
|
cliTargets.length === 0 ||
|
|
|
|
cliTargets.filter((cliTarget) => targets[cliTarget]?.react)
|
|
|
|
.length > 0,
|
2021-06-09 09:53:55 +02:00
|
|
|
task: (_, task) =>
|
|
|
|
task.newListr(
|
|
|
|
[
|
|
|
|
{
|
2021-10-13 16:27:47 +02:00
|
|
|
title: 'Creating temporary directory',
|
2021-06-09 09:53:55 +02:00
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
2021-10-13 16:27:47 +02:00
|
|
|
ctx.tmpDir = await fs.mkdtemp(
|
2023-09-24 18:34:07 +02:00
|
|
|
path.join(os.tmpdir(), 'iconoir-'),
|
2021-10-13 16:27:47 +02:00
|
|
|
);
|
2021-06-09 09:53:55 +02:00
|
|
|
} catch (err) {
|
2021-10-13 16:27:47 +02:00
|
|
|
ctx.skip = true;
|
2021-06-09 09:53:55 +02:00
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-13 16:27:47 +02:00
|
|
|
title:
|
|
|
|
'Copying icon files to temporary directory, while renaming icons with incompatible names',
|
|
|
|
skip: (ctx) => ctx.skip,
|
2021-06-09 09:53:55 +02:00
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
2021-10-13 16:27:47 +02:00
|
|
|
const promises = ctx.iconoirIconsFiles.map((file) => {
|
|
|
|
const srcFilePath = path.join(
|
|
|
|
iconoirIconsDir,
|
2023-09-24 18:34:07 +02:00
|
|
|
file,
|
2021-10-13 16:27:47 +02:00
|
|
|
);
|
|
|
|
const iconName = file.split('.')[0];
|
|
|
|
const dstFileName =
|
|
|
|
iconName in incompatibleNames
|
|
|
|
? incompatibleNames[iconName]
|
|
|
|
: iconName;
|
|
|
|
const dstFilePath = path.join(
|
2021-06-09 09:53:55 +02:00
|
|
|
ctx.tmpDir,
|
2023-09-24 18:34:07 +02:00
|
|
|
`${dstFileName}.svg`,
|
2021-10-13 16:27:47 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return fs.copyFile(srcFilePath, dstFilePath);
|
|
|
|
});
|
|
|
|
return Promise.all(promises).catch((err) => {
|
|
|
|
ctx.skip = true;
|
|
|
|
throw new Error(err.message);
|
|
|
|
});
|
2021-06-09 09:53:55 +02:00
|
|
|
} catch (err) {
|
2021-10-13 16:27:47 +02:00
|
|
|
ctx.skip = true;
|
2021-06-09 09:53:55 +02:00
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2021-10-13 16:27:47 +02:00
|
|
|
{
|
|
|
|
skip: (ctx) => ctx.skip,
|
|
|
|
task: (_, task) => {
|
|
|
|
const targetsToBuild =
|
|
|
|
cliTargets.length > 0
|
|
|
|
? cliTargets.filter(
|
2023-09-24 18:34:07 +02:00
|
|
|
(cliTarget) => targets[cliTarget]?.react,
|
2021-10-13 16:27:47 +02:00
|
|
|
)
|
|
|
|
: Object.keys(targets).filter(
|
2023-09-24 18:34:07 +02:00
|
|
|
(target) => targets[target].react,
|
2021-10-13 16:27:47 +02:00
|
|
|
);
|
|
|
|
const tasks = targetsToBuild.map((target) => {
|
|
|
|
const builtIconsDir = path.join(
|
|
|
|
rootDir,
|
|
|
|
targets[target].path,
|
2023-09-24 18:34:07 +02:00
|
|
|
'src',
|
2021-10-13 16:27:47 +02:00
|
|
|
);
|
|
|
|
return {
|
|
|
|
title: `Building ${target}`,
|
|
|
|
task: (_, task) =>
|
|
|
|
task.newListr(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Cleaning target directory',
|
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
2023-09-24 18:34:07 +02:00
|
|
|
const files =
|
|
|
|
await fs.readdir(builtIconsDir);
|
2023-01-29 20:49:22 +01:00
|
|
|
const serverFiles = existsSync(
|
2023-09-24 18:34:07 +02:00
|
|
|
path.join(builtIconsDir, 'server'),
|
2023-01-29 20:49:22 +01:00
|
|
|
)
|
|
|
|
? (
|
|
|
|
await fs.readdir(
|
|
|
|
path.join(
|
|
|
|
builtIconsDir,
|
2023-09-24 18:34:07 +02:00
|
|
|
'server',
|
|
|
|
),
|
2023-01-29 20:49:22 +01:00
|
|
|
)
|
|
|
|
).map((file) => `server/${file}`)
|
|
|
|
: [];
|
|
|
|
const promises = [
|
|
|
|
...files,
|
|
|
|
...serverFiles,
|
|
|
|
]
|
2022-08-28 20:12:09 +02:00
|
|
|
.filter(
|
|
|
|
(file) =>
|
|
|
|
!ignoreCleanFilenames.includes(
|
2023-09-24 18:34:07 +02:00
|
|
|
path.basename(file),
|
|
|
|
),
|
2022-08-28 20:12:09 +02:00
|
|
|
)
|
|
|
|
.map((file) => {
|
|
|
|
return fs.unlink(
|
2023-09-24 18:34:07 +02:00
|
|
|
path.join(builtIconsDir, file),
|
2022-08-28 20:12:09 +02:00
|
|
|
);
|
|
|
|
});
|
2021-10-13 16:27:47 +02:00
|
|
|
return Promise.all(promises).catch(
|
|
|
|
(err) => {
|
|
|
|
ctx[target] = { skip: true };
|
|
|
|
throw new Error(err.message);
|
2023-09-24 18:34:07 +02:00
|
|
|
},
|
2021-10-13 16:27:47 +02:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
ctx[target] = { skip: true };
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Building icon files',
|
|
|
|
skip: (ctx) => ctx[target]?.skip,
|
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
|
|
|
await execa(
|
|
|
|
'svgr',
|
|
|
|
[
|
|
|
|
'--config-file',
|
|
|
|
path.join(
|
|
|
|
targets[target].path,
|
2023-09-24 18:34:07 +02:00
|
|
|
'.svgrrc.json',
|
2021-10-13 16:27:47 +02:00
|
|
|
),
|
|
|
|
'--out-dir',
|
|
|
|
builtIconsDir,
|
2022-08-28 20:12:09 +02:00
|
|
|
'--template',
|
|
|
|
'bin/templates/icon-template.cjs',
|
|
|
|
'--index-template',
|
|
|
|
'bin/templates/index-template.cjs',
|
2023-09-24 21:13:56 +02:00
|
|
|
'--',
|
2021-10-13 16:27:47 +02:00
|
|
|
ctx.tmpDir,
|
|
|
|
],
|
2023-09-24 18:34:07 +02:00
|
|
|
{ preferLocal: true },
|
2021-10-13 16:27:47 +02:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2023-01-29 20:49:22 +01:00
|
|
|
...(target === 'iconoir-react'
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
title:
|
|
|
|
'Building icon files (server components)',
|
|
|
|
skip: (ctx) => ctx[target]?.skip,
|
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
|
|
|
await execa(
|
|
|
|
'svgr',
|
|
|
|
[
|
|
|
|
'--config-file',
|
|
|
|
path.join(
|
|
|
|
targets[target].path,
|
2023-09-24 18:34:07 +02:00
|
|
|
'.svgrrc.json',
|
2023-01-29 20:49:22 +01:00
|
|
|
),
|
|
|
|
'--out-dir',
|
|
|
|
path.join(
|
|
|
|
builtIconsDir,
|
2023-09-24 18:34:07 +02:00
|
|
|
'server',
|
2023-01-29 20:49:22 +01:00
|
|
|
),
|
|
|
|
'--template',
|
|
|
|
'bin/templates/icon-template-server-component.cjs',
|
|
|
|
'--index-template',
|
|
|
|
'bin/templates/index-template.cjs',
|
2023-09-24 21:13:56 +02:00
|
|
|
'--',
|
2023-01-29 20:49:22 +01:00
|
|
|
ctx.tmpDir,
|
|
|
|
],
|
2023-09-24 18:34:07 +02:00
|
|
|
{ preferLocal: true },
|
2023-01-29 20:49:22 +01:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: []),
|
2021-10-13 16:27:47 +02:00
|
|
|
],
|
2023-09-24 18:34:07 +02:00
|
|
|
{ concurrent: false, exitOnError: false },
|
2021-10-13 16:27:47 +02:00
|
|
|
),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return task.newListr(tasks, {
|
|
|
|
concurrent: true,
|
2023-09-24 21:53:53 +02:00
|
|
|
rendererOptions: { collapseSubtasks: false },
|
2021-10-13 16:27:47 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
2022-07-05 16:24:49 +02:00
|
|
|
],
|
2023-09-24 18:34:07 +02:00
|
|
|
{ concurrent: false },
|
2022-07-05 16:24:49 +02:00
|
|
|
),
|
|
|
|
},
|
2023-05-03 14:19:37 +02:00
|
|
|
{
|
|
|
|
title: 'Building Vue library',
|
|
|
|
enabled: () =>
|
|
|
|
cliTargets.length === 0 ||
|
|
|
|
cliTargets.filter((cliTarget) => targets[cliTarget]?.vue)
|
|
|
|
.length > 0,
|
|
|
|
task: (_, task) =>
|
|
|
|
task.newListr(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Creating temporary directory',
|
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
|
|
|
ctx.tmpDir = await fs.mkdtemp(
|
2023-09-24 18:34:07 +02:00
|
|
|
path.join(os.tmpdir(), 'iconoir-'),
|
2023-05-03 14:19:37 +02:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
ctx.skip = true;
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title:
|
|
|
|
'Copying icon files to temporary directory, while renaming icons with incompatible names',
|
|
|
|
skip: (ctx) => ctx.skip,
|
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
|
|
|
const promises = ctx.iconoirIconsFiles.map((file) => {
|
|
|
|
const srcFilePath = path.join(
|
|
|
|
iconoirIconsDir,
|
2023-09-24 18:34:07 +02:00
|
|
|
file,
|
2023-05-03 14:19:37 +02:00
|
|
|
);
|
|
|
|
const iconName = file.split('.')[0];
|
|
|
|
const dstFileName =
|
|
|
|
iconName in incompatibleNames
|
|
|
|
? incompatibleNames[iconName]
|
|
|
|
: iconName;
|
|
|
|
const dstFilePath = path.join(
|
|
|
|
ctx.tmpDir,
|
2023-09-24 18:34:07 +02:00
|
|
|
`${dstFileName}.svg`,
|
2023-05-03 14:19:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return fs.copyFile(srcFilePath, dstFilePath);
|
|
|
|
});
|
|
|
|
return Promise.all(promises).catch((err) => {
|
|
|
|
ctx.skip = true;
|
|
|
|
throw new Error(err.message);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
ctx.skip = true;
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skip: (ctx) => ctx.skip,
|
|
|
|
task: (_, task) => {
|
|
|
|
const targetsToBuild =
|
|
|
|
cliTargets.length > 0
|
|
|
|
? cliTargets.filter(
|
2023-09-24 18:34:07 +02:00
|
|
|
(cliTarget) => targets[cliTarget]?.vue,
|
2023-05-03 14:19:37 +02:00
|
|
|
)
|
|
|
|
: Object.keys(targets).filter(
|
2023-09-24 18:34:07 +02:00
|
|
|
(target) => targets[target].vue,
|
2023-05-03 14:19:37 +02:00
|
|
|
);
|
|
|
|
const tasks = targetsToBuild.map((target) => {
|
|
|
|
const builtIconsDir = path.join(
|
|
|
|
rootDir,
|
|
|
|
targets[target].path,
|
2023-09-24 18:34:07 +02:00
|
|
|
'src',
|
2023-05-03 14:19:37 +02:00
|
|
|
);
|
|
|
|
return {
|
|
|
|
title: `Building ${target}`,
|
|
|
|
task: (_, task) =>
|
|
|
|
task.newListr(
|
|
|
|
[
|
|
|
|
{
|
2023-09-26 02:35:33 +02:00
|
|
|
title: 'Create target directory',
|
2023-05-03 14:19:37 +02:00
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
2023-09-26 02:35:33 +02:00
|
|
|
await fs.mkdir(builtIconsDir, {
|
|
|
|
recursive: true,
|
|
|
|
});
|
2023-05-03 14:19:37 +02:00
|
|
|
} catch (err) {
|
|
|
|
ctx[target] = { skip: true };
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Building icon files',
|
|
|
|
skip: (ctx) => ctx[target]?.skip,
|
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
|
|
|
await buildVueIcons(ctx.tmpDir, {
|
|
|
|
outDir: builtIconsDir,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2023-09-24 18:34:07 +02:00
|
|
|
{ concurrent: false, exitOnError: false },
|
2023-05-03 14:19:37 +02:00
|
|
|
),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return task.newListr(tasks, {
|
|
|
|
concurrent: true,
|
2023-09-24 21:53:53 +02:00
|
|
|
rendererOptions: { collapseSubtasks: false },
|
2023-05-03 14:19:37 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2023-09-24 18:34:07 +02:00
|
|
|
{ concurrent: false },
|
2023-05-03 14:19:37 +02:00
|
|
|
),
|
|
|
|
},
|
2022-07-05 16:24:49 +02:00
|
|
|
{
|
|
|
|
title: 'Building Flutter libraries',
|
|
|
|
enabled: () =>
|
|
|
|
cliTargets.length === 0 ||
|
|
|
|
cliTargets.filter((cliTarget) => targets[cliTarget]?.flutter)
|
|
|
|
.length > 0,
|
|
|
|
task: (_, task) =>
|
|
|
|
task.newListr(
|
|
|
|
[
|
2021-10-13 16:27:47 +02:00
|
|
|
{
|
2022-07-05 16:24:49 +02:00
|
|
|
title: 'Creating temporary directory',
|
2021-10-13 16:27:47 +02:00
|
|
|
task: async (ctx) => {
|
2022-07-05 16:24:49 +02:00
|
|
|
try {
|
2023-03-20 18:33:01 +01:00
|
|
|
ctx.flutterTmpDir = await fs.mkdtemp(
|
2023-09-24 18:34:07 +02:00
|
|
|
path.join(os.tmpdir(), 'iconoir-'),
|
2022-07-05 16:24:49 +02:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
ctx.skip = true;
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title:
|
|
|
|
'Copying icon files to temporary directory, while renaming icons with incompatible names',
|
|
|
|
skip: (ctx) => ctx.skip,
|
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
|
|
|
const promises = ctx.iconoirIconsFiles.map((file) => {
|
|
|
|
const srcFilePath = path.join(
|
|
|
|
iconoirIconsDir,
|
2023-09-24 18:34:07 +02:00
|
|
|
file,
|
2022-07-05 16:24:49 +02:00
|
|
|
);
|
|
|
|
const iconName = file.split('.')[0];
|
|
|
|
const dstFileName =
|
2023-03-20 18:33:01 +01:00
|
|
|
iconName in flutterIncompatibleNames
|
|
|
|
? flutterIncompatibleNames[iconName]
|
2022-07-05 16:24:49 +02:00
|
|
|
: iconName;
|
|
|
|
const dstFilePath = path.join(
|
2023-03-20 18:33:01 +01:00
|
|
|
ctx.flutterTmpDir,
|
2023-09-24 18:34:07 +02:00
|
|
|
`${dstFileName}.svg`,
|
2022-07-05 16:24:49 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
ctx.dstFilePaths = [
|
|
|
|
...(ctx.dstFilePaths ?? []),
|
|
|
|
dstFilePath,
|
|
|
|
];
|
|
|
|
|
|
|
|
return fs.copyFile(srcFilePath, dstFilePath);
|
|
|
|
});
|
|
|
|
return Promise.all(promises).catch((err) => {
|
|
|
|
ctx.skip = true;
|
|
|
|
throw new Error(err.message);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
ctx.skip = true;
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
skip: (ctx) => ctx.skip,
|
|
|
|
task: (_, task) => {
|
|
|
|
const targetsToBuild =
|
|
|
|
cliTargets.length > 0
|
|
|
|
? cliTargets.filter(
|
2023-09-24 18:34:07 +02:00
|
|
|
(cliTarget) => targets[cliTarget]?.flutter,
|
2022-07-05 16:24:49 +02:00
|
|
|
)
|
|
|
|
: Object.keys(targets).filter(
|
2023-09-24 18:34:07 +02:00
|
|
|
(target) => targets[target].flutter,
|
2022-07-05 16:24:49 +02:00
|
|
|
);
|
|
|
|
const tasks = targetsToBuild.map((target) => {
|
|
|
|
const builtIconsDir = path.join(
|
|
|
|
rootDir,
|
|
|
|
targets[target].path,
|
2023-09-24 18:34:07 +02:00
|
|
|
'lib',
|
2022-07-05 16:24:49 +02:00
|
|
|
);
|
|
|
|
return {
|
|
|
|
title: `Building ${target}`,
|
|
|
|
task: (_, task) =>
|
|
|
|
task.newListr(
|
|
|
|
[
|
|
|
|
{
|
2023-09-26 02:35:33 +02:00
|
|
|
title: 'Create target directory',
|
2022-07-05 16:24:49 +02:00
|
|
|
task: async (ctx) => {
|
|
|
|
try {
|
2023-09-26 02:35:33 +02:00
|
|
|
await fs.mkdir(builtIconsDir, {
|
|
|
|
recursive: true,
|
2022-07-05 16:24:49 +02:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
ctx[target] = { skip: true };
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Create entry file',
|
|
|
|
task: async () => {
|
|
|
|
await fs.writeFile(
|
|
|
|
path.join(
|
|
|
|
builtIconsDir,
|
2023-09-24 18:34:07 +02:00
|
|
|
'iconoir_flutter.dart',
|
2022-07-05 16:24:49 +02:00
|
|
|
),
|
2023-09-24 18:34:07 +02:00
|
|
|
'library iconoir_flutter;\n\n',
|
2022-07-05 16:24:49 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Building icon files',
|
|
|
|
skip: (ctx) => ctx[target]?.skip,
|
|
|
|
task: async (ctx) => {
|
2022-08-28 20:12:09 +02:00
|
|
|
const finalFileNames = [];
|
2022-07-05 16:24:49 +02:00
|
|
|
try {
|
2022-08-28 20:12:09 +02:00
|
|
|
await Promise.all(
|
|
|
|
ctx.dstFilePaths.map(async (file) => {
|
2022-07-05 16:24:49 +02:00
|
|
|
const svgfilename =
|
|
|
|
path.parse(file).name;
|
|
|
|
// Prefix with Svg if icon name starts with a number
|
|
|
|
const iconname = `${
|
|
|
|
/^\d/.test(svgfilename)
|
|
|
|
? 'Svg'
|
|
|
|
: ''
|
|
|
|
}${svgfilename}`;
|
|
|
|
|
|
|
|
const svgfilecontent = (
|
|
|
|
await fs.readFile(file)
|
|
|
|
).toString();
|
|
|
|
|
|
|
|
await generateTemplateFilesBatch([
|
|
|
|
{
|
|
|
|
option:
|
|
|
|
'Create Icon Flutter Widget',
|
|
|
|
entry: {
|
|
|
|
folderPath:
|
|
|
|
'./bin/templates/__svgfilename__.dart',
|
|
|
|
},
|
|
|
|
dynamicReplacers: [
|
|
|
|
{
|
|
|
|
slot: '__icon__',
|
|
|
|
slotValue: iconname,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
slot: '__svgfilecontent__',
|
|
|
|
slotValue: svgfilecontent,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
slot: '__svgfilename__',
|
|
|
|
slotValue: svgfilename,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
output: {
|
|
|
|
path: './packages/iconoir-flutter/lib/__svgfilename__(snakeCase).dart',
|
|
|
|
pathAndFileNameDefaultCase:
|
|
|
|
'(snakeCase)',
|
|
|
|
},
|
2023-09-24 21:53:53 +02:00
|
|
|
onComplete(results) {
|
2022-08-28 20:12:09 +02:00
|
|
|
finalFileNames.push(
|
2023-09-24 18:34:07 +02:00
|
|
|
results.output.path,
|
2022-07-05 16:24:49 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
2023-09-24 18:34:07 +02:00
|
|
|
}),
|
2022-08-28 20:12:09 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
finalFileNames.sort();
|
|
|
|
await fs.appendFile(
|
|
|
|
path.join(
|
|
|
|
builtIconsDir,
|
2023-09-24 18:34:07 +02:00
|
|
|
'iconoir_flutter.dart',
|
2022-08-28 20:12:09 +02:00
|
|
|
),
|
|
|
|
finalFileNames
|
|
|
|
.map(
|
|
|
|
(fileName) =>
|
|
|
|
`export './${basename(
|
2023-09-24 18:34:07 +02:00
|
|
|
fileName,
|
|
|
|
)}';`,
|
2022-08-28 20:12:09 +02:00
|
|
|
)
|
2023-09-24 18:34:07 +02:00
|
|
|
.join('\n'),
|
2022-07-05 16:24:49 +02:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2023-09-24 18:34:07 +02:00
|
|
|
{ concurrent: false, exitOnError: false },
|
2022-07-05 16:24:49 +02:00
|
|
|
),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return task.newListr(tasks, {
|
|
|
|
concurrent: true,
|
2023-09-24 21:53:53 +02:00
|
|
|
rendererOptions: {
|
|
|
|
collapseSubtasks: false,
|
|
|
|
},
|
2022-07-05 16:24:49 +02:00
|
|
|
});
|
2021-10-13 16:27:47 +02:00
|
|
|
},
|
|
|
|
},
|
2021-06-09 09:53:55 +02:00
|
|
|
],
|
2023-09-24 18:34:07 +02:00
|
|
|
{ concurrent: false },
|
2021-06-09 09:53:55 +02:00
|
|
|
),
|
2021-10-13 16:27:47 +02:00
|
|
|
},
|
|
|
|
],
|
2023-09-24 18:34:07 +02:00
|
|
|
{ concurrent: true },
|
2021-10-13 16:27:47 +02:00
|
|
|
),
|
2021-06-09 09:53:55 +02:00
|
|
|
},
|
2022-07-05 16:24:49 +02:00
|
|
|
{
|
2023-03-20 18:33:01 +01:00
|
|
|
title: 'Removing React temporary directory',
|
2022-07-05 16:24:49 +02:00
|
|
|
skip: (ctx) => !ctx.tmpDir,
|
|
|
|
task: async (ctx) => {
|
|
|
|
await fs.rm(ctx.tmpDir, { recursive: true });
|
|
|
|
},
|
|
|
|
},
|
2023-03-20 18:33:01 +01:00
|
|
|
{
|
|
|
|
title: 'Removing Flutter temporary directory',
|
|
|
|
skip: (ctx) => !ctx.flutterTmpDir,
|
|
|
|
task: async (ctx) => {
|
|
|
|
await fs.rm(ctx.flutterTmpDir, { recursive: true });
|
|
|
|
},
|
|
|
|
},
|
2021-06-09 09:53:55 +02:00
|
|
|
],
|
|
|
|
{
|
|
|
|
concurrent: false,
|
|
|
|
exitOnError: false,
|
2023-09-24 21:53:53 +02:00
|
|
|
rendererOptions: {
|
|
|
|
collapseSubtasks: false,
|
|
|
|
collapseErrors: false,
|
|
|
|
},
|
2023-09-24 18:34:07 +02:00
|
|
|
},
|
2021-06-09 09:53:55 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
await tasks.run();
|