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

217 lines
5.1 KiB
Vue
Raw Normal View History

2022-04-14 02:02:05 +02:00
<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>
2022-04-14 02:02:05 +02:00
<div class="cron-string">
{{ cronString }}
</div>
<n-divider />
<n-space justify="center">
2022-04-22 23:31:40 +02:00
<n-form :show-feedback="false" label-width="170" label-placement="left">
2022-04-14 02:02:05 +02:00
<n-form-item label="Verbose">
<n-switch v-model:value="cronstrueConfig.verbose" />
</n-form-item>
<n-form-item label="Use 24 hour time format">
<n-switch v-model:value="cronstrueConfig.use24HourTimeFormat" />
</n-form-item>
<n-form-item label="Days start at 0">
<n-switch v-model:value="cronstrueConfig.dayOfWeekStartIndexZero" />
</n-form-item>
</n-form>
</n-space>
</c-card>
<c-card>
2022-04-14 02:02:05 +02:00
<pre>
[optional] seconds (0 - 59)
| minute (0 - 59)
| | hour (0 - 23)
| | | day of month (1 - 31)
| | | | month (1 - 12) OR jan,feb,mar,apr ...
| | | | | day of week (0 - 6, sunday=0) OR sun,mon ...
| | | | | |
2022-04-22 23:31:40 +02:00
* * * * * * command</pre
>
2022-04-14 02:02:05 +02:00
2022-04-22 23:31:40 +02:00
<n-space v-if="styleStore.isSmallScreen" vertical>
<c-card v-for="{ symbol, meaning, example, equivalent } in helpers" :key="symbol" important:border-none>
2022-04-22 23:31:40 +02:00
<div>
Symbol: <strong>{{ symbol }}</strong>
</div>
<div>
Meaning: <strong>{{ meaning }}</strong>
</div>
<div>
Example:
<strong
><code>{{ example }}</code></strong
>
</div>
<div>
Equivalent: <strong>{{ equivalent }}</strong>
</div>
</c-card>
</n-space>
2022-04-22 23:31:40 +02:00
<n-table v-else size="small">
2022-04-14 02:02:05 +02:00
<thead>
<tr>
2022-04-22 23:31:40 +02:00
<th class="text-left" scope="col">Symbol</th>
<th class="text-left" scope="col">Meaning</th>
<th class="text-left" scope="col">Example</th>
<th class="text-left" scope="col">Equivalent</th>
2022-04-14 02:02:05 +02:00
</tr>
</thead>
<tbody>
2022-04-22 23:31:40 +02:00
<tr v-for="{ symbol, meaning, example, equivalent } in helpers" :key="symbol">
<td>{{ symbol }}</td>
<td>{{ meaning }}</td>
2022-04-14 02:02:05 +02:00
<td>
<code>{{ example }}</code>
2022-04-14 02:02:05 +02:00
</td>
<td>{{ equivalent }}</td>
2022-04-14 02:02:05 +02:00
</tr>
</tbody>
</n-table>
</c-card>
2022-04-14 02:02:05 +02:00
</template>
<script setup lang="ts">
2022-04-22 23:31:40 +02:00
import cronstrue from 'cronstrue';
import { isValidCron } from 'cron-validator';
2022-04-14 02:02:05 +02:00
import { computed, reactive, ref } from 'vue';
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();
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,
});
2022-04-14 02:02:05 +02:00
const helpers = [
2022-04-22 23:31:40 +02:00
{
symbol: '*',
meaning: 'Any value',
example: '* * * *',
equivalent: 'Every minute',
},
{
symbol: '-',
meaning: 'Range of values',
example: '1-10 * * *',
equivalent: 'Minutes 1 through 10',
},
{
symbol: ',',
meaning: 'List of values',
example: '1,10 * * *',
equivalent: 'At minutes 1 and 10',
},
{
symbol: '/',
meaning: 'Step values',
example: '*/10 * * *',
equivalent: 'Every 10 minutes',
},
{
symbol: '@yearly',
meaning: 'Once every year at midnight of 1 January',
example: '@yearly',
equivalent: '0 0 1 1 *',
},
{
symbol: '@annually',
meaning: 'Same as @yearly',
example: '@annually',
equivalent: '0 0 1 1 *',
},
{
symbol: '@monthly',
meaning: 'Once a month at midnight on the first day',
example: '@monthly',
equivalent: '0 0 1 * *',
},
{
symbol: '@weekly',
meaning: 'Once a week at midnight on Sunday morning',
example: '@weekly',
equivalent: '0 0 * * 0',
},
{
symbol: '@daily',
meaning: 'Once a day at midnight',
example: '@daily',
equivalent: '0 0 * * *',
},
{
symbol: '@midnight',
meaning: 'Same as @daily',
example: '@midnight',
equivalent: '0 0 * * *',
},
{
symbol: '@hourly',
meaning: 'Once an hour at the beginning of the hour',
example: '@hourly',
equivalent: '0 * * * *',
},
{
symbol: '@reboot',
meaning: 'Run at startup',
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: 'This cron is invalid',
},
];
2022-04-14 02:02:05 +02:00
</script>
<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>