-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e63220
commit f1d5fb8
Showing
15 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ''; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
`; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |