Skip to content

Commit

Permalink
feature: loop: add version on js
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jun 18, 2024
1 parent 3e63220 commit f1d5fb8
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .madrun.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ checkDependencies() {
}

printMissing() {
echo "$1 is missing, please install it";
echo "'$1' is missing, please install it";
}

createFileWhenNotExist() {
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
5 changes: 5 additions & 0 deletions src/bin.js
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 15 additions & 0 deletions src/create-file-when-not-exist.js
Original file line number Diff line number Diff line change
@@ -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`;
};
17 changes: 17 additions & 0 deletions src/get-missing-dependency.js
Original file line number Diff line number Diff line change
@@ -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 '';
};
6 changes: 6 additions & 0 deletions src/get-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {createRequire} from 'node:module';

const require = createRequire(import.meta.url);
const {version} = require('../package.json');

export const getVersion = () => version;
31 changes: 31 additions & 0 deletions src/loop.js
Original file line number Diff line number Diff line change
@@ -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 <file> <size>');
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);
};
9 changes: 9 additions & 0 deletions src/resize-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {$} from 'zx';

export const resizeFile = (file, size) => {
$`
e2fsck -fy "${file}"
resize2fs "${file}" "${size}"
`;
};

15 changes: 15 additions & 0 deletions src/std.js
Original file line number Diff line number Diff line change
@@ -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);
};

0 comments on commit f1d5fb8

Please sign in to comment.