From f1d5fb892554b19431916acf34ca537e0ae4a0fa Mon Sep 17 00:00:00 2001 From: coderiaser Date: Tue, 18 Jun 2024 17:32:11 +0300 Subject: [PATCH] feature: loop: add version on js --- .madrun.js | 2 +- {src => amber}/create_file_when_not_exist.ab | 0 {src => amber}/get-missing-dependency.ab | 0 {src => amber}/get-version.ab | 0 {src => amber}/loop.ab | 0 {src => amber}/resize-file.ab | 0 lib/api.sh | 2 +- package.json | 4 +++ src/bin.js | 5 ++++ src/create-file-when-not-exist.js | 15 ++++++++++ src/get-missing-dependency.js | 17 +++++++++++ src/get-version.js | 6 ++++ src/loop.js | 31 ++++++++++++++++++++ src/resize-file.js | 9 ++++++ src/std.js | 15 ++++++++++ 15 files changed, 104 insertions(+), 2 deletions(-) rename {src => amber}/create_file_when_not_exist.ab (100%) rename {src => amber}/get-missing-dependency.ab (100%) rename {src => amber}/get-version.ab (100%) rename {src => amber}/loop.ab (100%) rename {src => amber}/resize-file.ab (100%) create mode 100644 src/bin.js create mode 100644 src/create-file-when-not-exist.js create mode 100644 src/get-missing-dependency.js create mode 100644 src/get-version.js create mode 100644 src/loop.js create mode 100644 src/resize-file.js create mode 100644 src/std.js diff --git a/.madrun.js b/.madrun.js index c073560..e8228e8 100644 --- a/.madrun.js +++ b/.madrun.js @@ -11,7 +11,7 @@ export default { 'rm:docker': () => 'docker rmi -f coderaiser/loop:`version`', 'docker:push': () => 'docker push coderaiser/loop:`version`', 'postdocker:push': () => 'docker push coderaiser/loop:latest', - 'build:amber': () => 'mkdir -p dist; amber src/loop.ab dist/loop.sh', + 'build:amber': () => 'mkdir -p dist; amber amber/loop.js dist/loop.sh', 'run:amber': () => 'bash dist/loop.sh', 'run': () => run(['build:amber', 'run:amber']), 'build': () => run('docker:build'), diff --git a/src/create_file_when_not_exist.ab b/amber/create_file_when_not_exist.ab similarity index 100% rename from src/create_file_when_not_exist.ab rename to amber/create_file_when_not_exist.ab diff --git a/src/get-missing-dependency.ab b/amber/get-missing-dependency.ab similarity index 100% rename from src/get-missing-dependency.ab rename to amber/get-missing-dependency.ab diff --git a/src/get-version.ab b/amber/get-version.ab similarity index 100% rename from src/get-version.ab rename to amber/get-version.ab diff --git a/src/loop.ab b/amber/loop.ab similarity index 100% rename from src/loop.ab rename to amber/loop.ab diff --git a/src/resize-file.ab b/amber/resize-file.ab similarity index 100% rename from src/resize-file.ab rename to amber/resize-file.ab diff --git a/lib/api.sh b/lib/api.sh index 95e6962..4e0449c 100755 --- a/lib/api.sh +++ b/lib/api.sh @@ -46,7 +46,7 @@ checkDependencies() { } printMissing() { - echo "$1 is missing, please install it"; + echo "'$1' is missing, please install it"; } createFileWhenNotExist() { diff --git a/package.json b/package.json index 7e88e05..a1b8444 100644 --- a/package.json +++ b/package.json @@ -45,5 +45,9 @@ "nodemon": "^2.0.7", "putout": "^35.35.0", "version-io": "^4.0.1" + }, + "dependencies": { + "try-to-catch": "^3.0.1", + "zx": "^8.1.2" } } diff --git a/src/bin.js b/src/bin.js new file mode 100644 index 0000000..38a746c --- /dev/null +++ b/src/bin.js @@ -0,0 +1,5 @@ +import process from 'node:process'; +import {loop} from './loop.js'; + +const [file, size] = process.argv.slice(2); +await loop(file, size); diff --git a/src/create-file-when-not-exist.js b/src/create-file-when-not-exist.js new file mode 100644 index 0000000..c348bc5 --- /dev/null +++ b/src/create-file-when-not-exist.js @@ -0,0 +1,15 @@ +import {fileExist} from './std.js'; +import tryToCatch from 'try-to-catch'; +import {$} from 'zx'; + +export const createFileWhenNotExist = async (file) => { + if (await fileExist(file)) + return $`mkfs.ext4 "${file}"`; + + const [fallocateError] = await tryToCatch($`fallocate -l "{size}" "{file}"`); + + if (!fallocateError) + return; + + return await $`dd if=/dev/zero of="{file}" bs="{size}" seek=1 count=0`; +}; diff --git a/src/get-missing-dependency.js b/src/get-missing-dependency.js new file mode 100644 index 0000000..700c743 --- /dev/null +++ b/src/get-missing-dependency.js @@ -0,0 +1,17 @@ +import {hasFailed} from './std.js'; + +const names = [ + 'fallocate', + 'mkfs.ext4', + 'e2fsck', + 'resize2fs', +]; + +export const getMissingDependency = async function() { + for (const name of names) { + if (await hasFailed(name)) + return name; + } + + return ''; +}; diff --git a/src/get-version.js b/src/get-version.js new file mode 100644 index 0000000..70087cc --- /dev/null +++ b/src/get-version.js @@ -0,0 +1,6 @@ +import {createRequire} from 'node:module'; + +const require = createRequire(import.meta.url); +const {version} = require('../package.json'); + +export const getVersion = () => version; diff --git a/src/loop.js b/src/loop.js new file mode 100644 index 0000000..b0486d4 --- /dev/null +++ b/src/loop.js @@ -0,0 +1,31 @@ +import {getVersion} from './get-version.js'; +import {getMissingDependency} from './get-missing-dependency.js'; +import {createFileWhenNotExist} from './create-file-when-not-exist.js'; +import process from 'node:process'; +import {resizeFile} from './resize-file.js'; + +export const loop = async (file, size, overrides = {}) => { + const {exit = process.exit, log = console.log} = overrides; + + if (/^(-v|--version)$/.test(file)) { + const version = getVersion(); + + log(`v${version}`); + return exit(0); + } + + if (!file || !size) { + log('loop '); + return exit(0); + } + + const missing = await getMissingDependency(); + + if (missing) { + log(`'${missing}' is missing, please install it`); + return exit(1); + } + + await createFileWhenNotExist(file, size); + await resizeFile(file, size); +}; diff --git a/src/resize-file.js b/src/resize-file.js new file mode 100644 index 0000000..873f94b --- /dev/null +++ b/src/resize-file.js @@ -0,0 +1,9 @@ +import {$} from 'zx'; + +export const resizeFile = (file, size) => { + $` + e2fsck -fy "${file}" + resize2fs "${file}" "${size}" + `; +}; + diff --git a/src/std.js b/src/std.js new file mode 100644 index 0000000..83d1450 --- /dev/null +++ b/src/std.js @@ -0,0 +1,15 @@ +import {$} from 'zx'; +import tryToCatch from 'try-to-catch'; + +const wrapTag = (tag) => (arg) => tag({quiet: true})`${arg}`; +const $$ = wrapTag($); + +export const hasFailed = async (command) => { + const [error] = await tryToCatch($$, `eval ${command}`); + return Boolean(error); +}; + +export const fileExist = async (name) => { + const [error] = await tryToCatch($$, `[ -f "${name}" ]`); + return Boolean(error); +};