Skip to content

Commit f1d5fb8

Browse files
committed
feature: loop: add version on js
1 parent 3e63220 commit f1d5fb8

15 files changed

+104
-2
lines changed

.madrun.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default {
1111
'rm:docker': () => 'docker rmi -f coderaiser/loop:`version`',
1212
'docker:push': () => 'docker push coderaiser/loop:`version`',
1313
'postdocker:push': () => 'docker push coderaiser/loop:latest',
14-
'build:amber': () => 'mkdir -p dist; amber src/loop.ab dist/loop.sh',
14+
'build:amber': () => 'mkdir -p dist; amber amber/loop.js dist/loop.sh',
1515
'run:amber': () => 'bash dist/loop.sh',
1616
'run': () => run(['build:amber', 'run:amber']),
1717
'build': () => run('docker:build'),
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

lib/api.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ checkDependencies() {
4646
}
4747

4848
printMissing() {
49-
echo "$1 is missing, please install it";
49+
echo "'$1' is missing, please install it";
5050
}
5151

5252
createFileWhenNotExist() {

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,9 @@
4545
"nodemon": "^2.0.7",
4646
"putout": "^35.35.0",
4747
"version-io": "^4.0.1"
48+
},
49+
"dependencies": {
50+
"try-to-catch": "^3.0.1",
51+
"zx": "^8.1.2"
4852
}
4953
}

src/bin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import process from 'node:process';
2+
import {loop} from './loop.js';
3+
4+
const [file, size] = process.argv.slice(2);
5+
await loop(file, size);

src/create-file-when-not-exist.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {fileExist} from './std.js';
2+
import tryToCatch from 'try-to-catch';
3+
import {$} from 'zx';
4+
5+
export const createFileWhenNotExist = async (file) => {
6+
if (await fileExist(file))
7+
return $`mkfs.ext4 "${file}"`;
8+
9+
const [fallocateError] = await tryToCatch($`fallocate -l "{size}" "{file}"`);
10+
11+
if (!fallocateError)
12+
return;
13+
14+
return await $`dd if=/dev/zero of="{file}" bs="{size}" seek=1 count=0`;
15+
};

src/get-missing-dependency.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {hasFailed} from './std.js';
2+
3+
const names = [
4+
'fallocate',
5+
'mkfs.ext4',
6+
'e2fsck',
7+
'resize2fs',
8+
];
9+
10+
export const getMissingDependency = async function() {
11+
for (const name of names) {
12+
if (await hasFailed(name))
13+
return name;
14+
}
15+
16+
return '';
17+
};

src/get-version.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {createRequire} from 'node:module';
2+
3+
const require = createRequire(import.meta.url);
4+
const {version} = require('../package.json');
5+
6+
export const getVersion = () => version;

src/loop.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {getVersion} from './get-version.js';
2+
import {getMissingDependency} from './get-missing-dependency.js';
3+
import {createFileWhenNotExist} from './create-file-when-not-exist.js';
4+
import process from 'node:process';
5+
import {resizeFile} from './resize-file.js';
6+
7+
export const loop = async (file, size, overrides = {}) => {
8+
const {exit = process.exit, log = console.log} = overrides;
9+
10+
if (/^(-v|--version)$/.test(file)) {
11+
const version = getVersion();
12+
13+
log(`v${version}`);
14+
return exit(0);
15+
}
16+
17+
if (!file || !size) {
18+
log('loop <file> <size>');
19+
return exit(0);
20+
}
21+
22+
const missing = await getMissingDependency();
23+
24+
if (missing) {
25+
log(`'${missing}' is missing, please install it`);
26+
return exit(1);
27+
}
28+
29+
await createFileWhenNotExist(file, size);
30+
await resizeFile(file, size);
31+
};

src/resize-file.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {$} from 'zx';
2+
3+
export const resizeFile = (file, size) => {
4+
$`
5+
e2fsck -fy "${file}"
6+
resize2fs "${file}" "${size}"
7+
`;
8+
};
9+

src/std.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {$} from 'zx';
2+
import tryToCatch from 'try-to-catch';
3+
4+
const wrapTag = (tag) => (arg) => tag({quiet: true})`${arg}`;
5+
const $$ = wrapTag($);
6+
7+
export const hasFailed = async (command) => {
8+
const [error] = await tryToCatch($$, `eval ${command}`);
9+
return Boolean(error);
10+
};
11+
12+
export const fileExist = async (name) => {
13+
const [error] = await tryToCatch($$, `[ -f "${name}" ]`);
14+
return Boolean(error);
15+
};

0 commit comments

Comments
 (0)