mirror of
https://github.com/Mibew/simple-icons.git
synced 2025-02-08 02:04:42 +03:00
Handle cleanly interruption signals on add-icon-data script (#10620)
This commit is contained in:
parent
8aab5ab4e5
commit
1401fb2a05
@ -84,7 +84,7 @@
|
|||||||
"url": "https://opencollective.com/simple-icons"
|
"url": "https://opencollective.com/simple-icons"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@inquirer/prompts": "3.2.0",
|
"@inquirer/prompts": "4.3.0",
|
||||||
"chalk": "5.3.0",
|
"chalk": "5.3.0",
|
||||||
"del-cli": "5.1.0",
|
"del-cli": "5.1.0",
|
||||||
"editorconfig-checker": "5.1.1",
|
"editorconfig-checker": "5.1.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import { input, confirm, checkbox } from '@inquirer/prompts';
|
import { input, confirm, checkbox, ExitPromptError } from '@inquirer/prompts';
|
||||||
import autocomplete from 'inquirer-autocomplete-standalone';
|
import autocomplete from 'inquirer-autocomplete-standalone';
|
||||||
import getRelativeLuminance from 'get-relative-luminance';
|
import getRelativeLuminance from 'get-relative-luminance';
|
||||||
import { search } from 'fast-fuzzy';
|
import { search } from 'fast-fuzzy';
|
||||||
@ -88,99 +88,116 @@ const getIconDataFromAnswers = (answers) => ({
|
|||||||
: {}),
|
: {}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const answers = {};
|
const run = async () => {
|
||||||
|
const answers = {};
|
||||||
|
|
||||||
answers.title = await input({
|
answers.title = await input({
|
||||||
message: 'Title:',
|
message: 'Title:',
|
||||||
validate: titleValidator,
|
validate: titleValidator,
|
||||||
});
|
});
|
||||||
|
|
||||||
answers.hex = await input({
|
answers.hex = await input({
|
||||||
message: 'Hex:',
|
message: 'Hex:',
|
||||||
validate: hexValidator,
|
validate: hexValidator,
|
||||||
transformer: hexTransformer,
|
transformer: hexTransformer,
|
||||||
});
|
});
|
||||||
|
|
||||||
answers.source = await input({
|
answers.source = await input({
|
||||||
message: 'Source URL:',
|
message: 'Source URL:',
|
||||||
validate: sourceValidator,
|
|
||||||
});
|
|
||||||
|
|
||||||
answers.hasGuidelines = await confirm({
|
|
||||||
message: 'The icon has brand guidelines?',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (answers.hasGuidelines) {
|
|
||||||
answers.guidelines = await input({
|
|
||||||
message: 'Guidelines URL:',
|
|
||||||
validate: sourceValidator,
|
validate: sourceValidator,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
answers.hasLicense = await confirm({
|
answers.hasGuidelines = await confirm({
|
||||||
message: 'The icon has brand license?',
|
message: 'The icon has brand guidelines?',
|
||||||
});
|
|
||||||
|
|
||||||
if (answers.hasLicense) {
|
|
||||||
const licenseTypes =
|
|
||||||
jsonSchema.definitions.brand.properties.license.oneOf[0].properties.type.enum.map(
|
|
||||||
(license) => {
|
|
||||||
return { value: license };
|
|
||||||
},
|
|
||||||
);
|
|
||||||
answers.licenseType = await autocomplete({
|
|
||||||
message: 'License type:',
|
|
||||||
source: async (input) => {
|
|
||||||
input = (input || '').trim();
|
|
||||||
return input
|
|
||||||
? search(input, licenseTypes, { keySelector: (x) => x.value })
|
|
||||||
: licenseTypes;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
answers.licenseUrl = await input({
|
if (answers.hasGuidelines) {
|
||||||
message: `License URL ${chalk.reset('(optional)')}:`,
|
answers.guidelines = await input({
|
||||||
validate: (text) => text.length === 0 || sourceValidator(text),
|
message: 'Guidelines URL:',
|
||||||
});
|
validate: sourceValidator,
|
||||||
}
|
|
||||||
|
|
||||||
answers.hasAliases = await confirm({
|
|
||||||
message: 'This icon has brand aliases?',
|
|
||||||
default: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (answers.hasAliases) {
|
|
||||||
answers.aliasesTypes = await checkbox({
|
|
||||||
message: 'What types of aliases do you want to add?',
|
|
||||||
choices: aliasesChoices,
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const x of aliasesChoices) {
|
|
||||||
if (!answers?.aliasesTypes?.includes(x.value)) continue;
|
|
||||||
answers[`${x.value}AliasesList`] = await input({
|
|
||||||
message: x.value + chalk.reset(' (separate with commas)'),
|
|
||||||
validate: (text) => text.trim().length > 0,
|
|
||||||
transformer: aliasesTransformer,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
answers.confirmToAdd = await confirm({
|
answers.hasLicense = await confirm({
|
||||||
message: [
|
message: 'The icon has brand license?',
|
||||||
'About to write the following to simple-icons.json:',
|
});
|
||||||
chalk.reset(JSON.stringify(getIconDataFromAnswers(answers), null, 4)),
|
|
||||||
chalk.reset('Is this OK?'),
|
|
||||||
].join('\n\n'),
|
|
||||||
});
|
|
||||||
|
|
||||||
const icon = getIconDataFromAnswers(answers);
|
if (answers.hasLicense) {
|
||||||
|
const licenseTypes =
|
||||||
|
jsonSchema.definitions.brand.properties.license.oneOf[0].properties.type.enum.map(
|
||||||
|
(license) => {
|
||||||
|
return { value: license };
|
||||||
|
},
|
||||||
|
);
|
||||||
|
answers.licenseType = await autocomplete({
|
||||||
|
message: 'License type:',
|
||||||
|
source: async (input) => {
|
||||||
|
input = (input || '').trim();
|
||||||
|
return input
|
||||||
|
? search(input, licenseTypes, { keySelector: (x) => x.value })
|
||||||
|
: licenseTypes;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (answers.confirmToAdd) {
|
answers.licenseUrl = await input({
|
||||||
iconsData.icons.push(icon);
|
message: `License URL ${chalk.reset('(optional)')}:`,
|
||||||
iconsData.icons.sort((a, b) => collator.compare(a.title, b.title));
|
validate: (text) => text.length === 0 || sourceValidator(text),
|
||||||
await writeIconsData(iconsData);
|
});
|
||||||
console.log(chalk.green('\nData written successfully.'));
|
}
|
||||||
} else {
|
|
||||||
console.log(chalk.red('\nAborted.'));
|
answers.hasAliases = await confirm({
|
||||||
process.exit(1);
|
message: 'This icon has brand aliases?',
|
||||||
}
|
default: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (answers.hasAliases) {
|
||||||
|
answers.aliasesTypes = await checkbox({
|
||||||
|
message: 'What types of aliases do you want to add?',
|
||||||
|
choices: aliasesChoices,
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const x of aliasesChoices) {
|
||||||
|
if (!answers?.aliasesTypes?.includes(x.value)) continue;
|
||||||
|
answers[`${x.value}AliasesList`] = await input({
|
||||||
|
message: x.value + chalk.reset(' (separate with commas)'),
|
||||||
|
validate: (text) => text.trim().length > 0,
|
||||||
|
transformer: aliasesTransformer,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
answers.confirmToAdd = await confirm({
|
||||||
|
message: [
|
||||||
|
'About to write the following to simple-icons.json:',
|
||||||
|
chalk.reset(JSON.stringify(getIconDataFromAnswers(answers), null, 4)),
|
||||||
|
chalk.reset('Is this OK?'),
|
||||||
|
].join('\n\n'),
|
||||||
|
});
|
||||||
|
|
||||||
|
const icon = getIconDataFromAnswers(answers);
|
||||||
|
|
||||||
|
if (answers.confirmToAdd) {
|
||||||
|
iconsData.icons.push(icon);
|
||||||
|
iconsData.icons.sort((a, b) => collator.compare(a.title, b.title));
|
||||||
|
await writeIconsData(iconsData);
|
||||||
|
console.log(chalk.green('\nData written successfully.'));
|
||||||
|
} else {
|
||||||
|
console.log(chalk.red('\nAborted.'));
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const main = async () => {
|
||||||
|
try {
|
||||||
|
await run();
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof ExitPromptError) {
|
||||||
|
console.log(chalk.red('\nAborted.'));
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await main();
|
||||||
|
Loading…
Reference in New Issue
Block a user