it-tools/src/tools/crontab-generator/crontab-generator.vue

199 lines
5.2 KiB
Vue
Raw Normal View History

2022-04-14 02:02:05 +02:00
<script setup lang="ts">
2022-04-22 23:31:40 +02:00
import cronstrue from 'cronstrue';
import 'cronstrue/locales/zh_CN';
import 'cronstrue/locales/fr';
2022-04-22 23:31:40 +02:00
import { isValidCron } from 'cron-validator';
import { useStyleStore } from '@/stores/style.store';
2022-04-14 02:02:05 +02:00
function isCronValid(v: string) {
2022-04-22 23:31:40 +02:00
return isValidCron(v, { allowBlankDay: true, alias: true, seconds: true });
2022-04-14 02:02:05 +02:00
}
2022-04-22 23:31:40 +02:00
const styleStore = useStyleStore();
const { t, locale } = useI18n();
2022-04-22 23:31:40 +02:00
const cron = ref('40 * * * *');
2022-04-14 02:02:05 +02:00
const cronstrueConfig = reactive({
verbose: true,
dayOfWeekStartIndexZero: true,
use24HourTimeFormat: true,
2022-04-22 23:31:40 +02:00
throwExceptionOnParseError: true,
locale: locale.value === 'zh' ? 'zh_CN' : locale,
2022-04-22 23:31:40 +02:00
});
2022-04-14 02:02:05 +02:00
const helpers = [
2022-04-22 23:31:40 +02:00
{
symbol: '*',
meaning: t('tools.crontab-generator.anyMeaning'),
2022-04-22 23:31:40 +02:00
example: '* * * *',
equivalent: t('tools.crontab-generator.anyExample'),
2022-04-22 23:31:40 +02:00
},
{
symbol: '-',
meaning: t('tools.crontab-generator.rangeMeaning'),
2022-04-22 23:31:40 +02:00
example: '1-10 * * *',
equivalent: t('tools.crontab-generator.rangeExample'),
2022-04-22 23:31:40 +02:00
},
{
symbol: ',',
meaning: t('tools.crontab-generator.listMeaning'),
2022-04-22 23:31:40 +02:00
example: '1,10 * * *',
equivalent: t('tools.crontab-generator.listExample'),
2022-04-22 23:31:40 +02:00
},
{
symbol: '/',
meaning: t('tools.crontab-generator.stepMeaning'),
2022-04-22 23:31:40 +02:00
example: '*/10 * * *',
equivalent: t('tools.crontab-generator.stepExample'),
2022-04-22 23:31:40 +02:00
},
{
symbol: '@yearly',
meaning: t('tools.crontab-generator.yearlyMeaning'),
2022-04-22 23:31:40 +02:00
example: '@yearly',
equivalent: '0 0 1 1 *',
},
{
symbol: '@annually',
meaning: t('tools.crontab-generator.annuallyMeaning', { yearly: '@yearly' }),
2022-04-22 23:31:40 +02:00
example: '@annually',
equivalent: '0 0 1 1 *',
},
{
symbol: '@monthly',
meaning: t('tools.crontab-generator.monthlyMeaning'),
2022-04-22 23:31:40 +02:00
example: '@monthly',
equivalent: '0 0 1 * *',
},
{
symbol: '@weekly',
meaning: t('tools.crontab-generator.weeklyMeaning'),
2022-04-22 23:31:40 +02:00
example: '@weekly',
equivalent: '0 0 * * 0',
},
{
symbol: '@daily',
meaning: t('tools.crontab-generator.dailyMeaning'),
2022-04-22 23:31:40 +02:00
example: '@daily',
equivalent: '0 0 * * *',
},
{
symbol: '@midnight',
meaning: t('tools.crontab-generator.midnightMeaning', { daily: '@daily' }),
2022-04-22 23:31:40 +02:00
example: '@midnight',
equivalent: '0 0 * * *',
},
{
symbol: '@hourly',
meaning: t('tools.crontab-generator.hourlyMeaning'),
2022-04-22 23:31:40 +02:00
example: '@hourly',
equivalent: '0 * * * *',
},
{
symbol: '@reboot',
meaning: t('tools.crontab-generator.rebootMeaning'),
2022-04-22 23:31:40 +02:00
example: '',
equivalent: '',
},
];
2022-04-14 02:02:05 +02:00
const cronString = computed(() => {
if (isCronValid(cron.value)) {
2022-04-22 23:31:40 +02:00
return cronstrue.toString(cron.value, cronstrueConfig);
2022-04-14 02:02:05 +02:00
}
2022-04-22 23:31:40 +02:00
return ' ';
});
2022-04-14 02:02:05 +02:00
const cronValidationRules = [
{
validator: (value: string) => isCronValid(value),
message: t('tools.crontab-generator.invalidMessage'),
},
];
2022-04-14 02:02:05 +02:00
</script>
<template>
<c-card>
<div mx-auto max-w-sm>
<c-input-text
v-model:value="cron"
size="large"
placeholder="* * * * *"
:validation-rules="cronValidationRules"
mb-3
/>
</div>
<div class="cron-string">
{{ cronString }}
</div>
<n-divider />
<div flex justify-center>
<n-form :show-feedback="false" label-width="170" label-placement="left">
<n-form-item :label="t('tools.crontab-generator.verbose')">
<n-switch v-model:value="cronstrueConfig.verbose" />
</n-form-item>
<n-form-item :label="t('tools.crontab-generator.use24HourTimeFormat')">
<n-switch v-model:value="cronstrueConfig.use24HourTimeFormat" />
</n-form-item>
<n-form-item :label="t('tools.crontab-generator.dayOfWeekStartIndexZero')">
<n-switch v-model:value="cronstrueConfig.dayOfWeekStartIndexZero" />
</n-form-item>
</n-form>
</div>
</c-card>
<c-card>
<pre>
{{ t('tools.crontab-generator.secondDesc') }}
| {{ t('tools.crontab-generator.minuteDesc') }}
| | {{ t('tools.crontab-generator.hourDesc') }}
| | | {{ t('tools.crontab-generator.dayOfMonthDesc') }}
| | | | {{ t('tools.crontab-generator.monthDesc') }}
| | | | | {{ t('tools.crontab-generator.dayOfWeekDesc') }}
| | | | | |
* * * * * * {{ t('tools.crontab-generator.command') }}</pre>
<div v-if="styleStore.isSmallScreen">
<c-card v-for="{ symbol, meaning, example, equivalent } in helpers" :key="symbol" mb-3 important:border-none>
<div>
{{ t('tools.crontab-generator.symbol') }} <strong>{{ symbol }}</strong>
</div>
<div>
{{ t('tools.crontab-generator.meaning') }} <strong>{{ meaning }}</strong>
</div>
<div>
{{ t('tools.crontab-generator.example') }}
<strong><code>{{ example }}</code></strong>
</div>
<div>
{{ t('tools.crontab-generator.equivalent') }} <strong>{{ equivalent }}</strong>
</div>
</c-card>
</div>
<c-table v-else :data="helpers" />
</c-card>
</template>
2022-04-14 02:02:05 +02:00
<style lang="less" scoped>
::v-deep(input) {
font-size: 30px;
font-family: monospace;
padding: 5px;
2022-04-14 02:02:05 +02:00
text-align: center;
}
.cron-string {
text-align: center;
font-size: 22px;
opacity: 0.8;
margin: 5px 0 15px;
}
2022-04-18 08:50:57 +02:00
pre {
overflow: auto;
padding: 10px 0;
}
</style>