
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
objectize-fs
Advanced tools
ファイル・ディレクトリを抽象化してNode.js APIで扱うやつ。
JScriptのFileSystemObjectとは一切関係ない。
# v1までは仕様がコロコロ変わるため
$ npm i -E objectize-fs
const {Directory, File, ZIP, RAR, Utility} = require('objectize-fs');
// $ rm *.txt
const dir = await new Directory('./');
const files = await dir.getFiles();
for(let file of files){
if(file.ext==='txt'){
await file.delete();
}
}
自身を引数1パスにコピーする。
コピーした実体から作ったインスタンスを引数に解決するpromiseを返す。
引数2がtrueなら上書きを許可する。
// dir => dir, dir2
const dir = new Directory('dir');
const dir2 = await dir.copy('dir2');
// fileA.ext => fileA.ext, fileB.ext
const fileA = await new File('fileA.ext');
const fileB = await file.copy('fileB.ext');
自身の日付情報を含むオブジェクトを取得する。
取得したオブジェクトを引数に解決するpromiseを返す。
// date {a, m, c, birth}
const date = await dir.date();
自身の実体を削除する。
削除後に解決するpromiseを返す。
await instance.delete();
自身の親ディレクトリのDirectoryインスタンスを取得する。 取得したインスタンスを引数に解決するpromiseを返す。
const parentDir = await instance.getParentDirectory();
自身の実体が存在するかのbooleanを取得する。
取得したbooleanを引数に解決するpromiseを返す。
const bool = await instance.isLive();
自身の実体を引数パスのディレクトリに移動する。
引数2がtrueなら上書きを許可する。
移動後に解決するpromiseを返す。
// bar => foo/bar
await dir.move('foo');
// filename => foo/filename
await file.move('foo');
inodeナンバーが変化する。
自身の実体を開く。
子プロセスを引数に解決するpromiseを返す。
const child_process = await dir.open();
const child_process = await file.open();
自身と実体を引数文字列に改名する。
自身を引数に解決するpromiseを返す。
await dir.rename('new-dirname');
await file.rename('new-filename.ext');
// overwrite
await file.rename('new-filename.ext', true);
自身の実体をゴミ箱に移動する。
移動後に解決するpromiseを返す。
await dir.trash();
await file.trash();
自身の実体を圧縮する。
圧縮した.zipファイルのZIPインスタンスを引数に解決するpromiseを返す。
// dir => dir.zip
const zip = await dir.zip();
// dir => hoge.zip
const zip = await dir.zip('./hoge.zip');
// file.ext => file.zip
const zip = await file.zip();
// file.ext => hoge.zip
const zip = await file.zip('hoge.zip');
読み取り専用。
自身の親ディレクトリの絶対パス文字列。
instance.base; // Win: "C:\\Users\\username"
読み取り専用。
自身のファイル・ディレクトリ名の文字列。
dir.name; // "dirname"
file.name; // "filename.ext"
自身の絶対パスの文字列。
dir.path; // Win: "C:\\Users\\username\\dirname"
file.path; // Win: "C:\\Users\\username\\filename.ext"
いくつかの関数は引数に渡すオブジェクトで挙動を制御できる。
| key | type | description |
|---|---|---|
| directory | boolean | 結果にDirectoryインスタンスを含むか。 |
| file | boolean | 結果にFileインスタンスを含むか。 |
| filter | function | 走査中のインスタンス毎にそれを引数に実行し、trueを返したものを結果に含む。 |
| greedy | boolean | 一部メソッドのみ。下位ディレクトリに対して同じ処理を繰り返すか。 |
| global | boolean | 一部メソッドのみ。全てのコンテンツを走査して結果を配列で取得するか。 |
引数パスのディレクトリを基にインスタンスを作る。
作成したインスタンスを引数に解決するpromiseを返す。
const dir = await new Directory('path');
// config
const dir = await new Directory('path', {
stats: fs.statSync(path)
});
引数1パスのディレクトリが存在しなければ作成する。
作成したディレクトリのDirectoryインスタンスを引数に解決するpromiseを返す。
const dir = await Directory.make('hoge');
自身の実体をプロセスのカレントディレクトリにする。
変更後に解決するPromiseを返す。
await dir.cd();
自身の実体が含むファイル・ディレクトリを全て削除する。
削除後に解決するpromiseを返す。
await dir.clear();
自身が直接含む、引数文字列と一致する名前のファイル・ディレクトリのインスタンスかnullを取得する。
取得した結果を引数に解決するpromiseを返す。
const file = await dir.get('file.txt');
// options
const dir_foobar_empty = await dir.get('foobar', {
file: false,
directory: true,
async filter(dir){
const list = await dir.list();
return list.length===0;
}
});
自身が直接含むファイル・ディレクトリインスタンスを配列で取得する。
取得した配列を引数に解決するpromiseを返す。
const arr = await dir.getContents();
// options
const fileArr_deep_under1KB = await dir.getContents({
directory: false,
file: true,
greedy: true,
async filter(file){
const size = await file.size();
return size < 1024;
}}
);
自身が含むディレクトリインスタンスを配列で取得する。
取得した配列を引数に解決するpromiseを返す。
const dirs = await dir.getDirectories();
// options
const dirArr_deep_over1KB = await dir.getDirectories({
greedy: true,
async filter(dir){
const size = await dir.size();
return size > 1024;
}}
);
自身が含むファイルインスタンスを配列で取得する。
取得した配列を引数に解決するpromiseを返す。
const files = await dir.getFiles();
// options
const fileArr_deep_txt = await dir.getDirectories({
greedy: true,
filter(file){
return file.ext==='txt';
}}
);
自身が引数文字列名のファイル・ディレクトリを直接含んでいるか調べる。 結果のbooleanを引数に解決するpromiseを返す。
const bool = await dir.has('hoge');
// options
const hasSecretTextFile = await dir.has('fuga', {
file: true,
directory: false,
async filter(file){
const string = await file.read();
return string.includes('password');
}
});
自身が直接含んでいるファイル・ディレクトリ名の配列を取得する。
取得した配列を引数に解決するpromiseを返す。
const nameArr = await dir.list();
自身の含む、引数1の条件と一致する名前を持つファイル・ディレクトリインスタンスを取得する。
取得したインスタンスかnullを引数に解決するpromiseを返す。
const file = await dir.search('hoge.txt');
const dir = await dir.search(/fuga|piyo/);
const file_txt = await dir.search( (instance)=>{
return instance.isFile && instance.ext==='txt';
});
// options
const dirArr_deep_notEmpty = await dir.search(/./, {
file: false,
directory: true,
global: true,
greedy: true,
async filter(dir){
const list = dir.list();
return list!==0;
}
});
自身の含むファイル・ディレクトリの総サイズを数値で取得する。
取得した数値を引数に解決するpromiseを返す。
const size = await dir.size();
dir.isDirectory; // true
引数パスのファイルを基にfileインスタンスを作る。
作ったインスタンスを引数に解決するpromiseを返す。
const file = await new File('./filename.ext');
// config
const file = await new File('./filename.ext', {
stats: fs.statSync('./filename.ext')
});
引数1のパスに、引数2の内容でファイルを作成する。
作成したファイルのFileインスタンスを引数に解決するpromiseを返す。
const file = await File.make('hoge.txt', 'hogefugapiyo');
自身の実体から拡張子を推測して、可能なら改名する。
なにもしなければ自身を、改名していれば新しいインスタンスを引数に解決するpromiseを返す。
// filename => filename.ext
await file.autoExt();
// hoge => hoge.jpg
const image = await file.autoExt();
自身の内容を文字列で取得する。
取得した文字列を引数に解決するpromiseを返す。
const str = await file.read();
自身のファイルサイズを数値で取得する。
取得した数値を引数に解決するpromiseを返す。
const size = await file.size();
自身の内容を引数の文字列で書き換える。
書き換え後に解決するpromiseを返す。
await file.write('hogehoge');
自身の拡張子を除いたファイル名の文字列。
const file = await new File('filename.ext');
file.basename; // "filename"
自身の拡張子の文字列。
const file = await new File('filename.ext');
file.ext; // "ext"
file.isFile; // true
Fileを継承している。
引数パスの画像ファイルを元にImageインスタンスを作る。
作ったインスタンスを引数に解决するpromiseを返す。
const image = await new Image('hoge.jpg');
引数1のパスに、引数2のURLかbufferを元に画像ファイルの実体を作成する。
作成した画像ファイルのImageインスタンスを引数に解决するpromiseを返す。
const image = await Image.make(
'./hoge.jpg',
'http://example.com/hoge.jpg'
);
引数1パスに、引数2の設定で画像ファイルの実体を作成する。
作成した画像ファイルのImageインスタンスを引数に解决するpromiseを返す。
// png, greyscale, auto*250px
const imageB = await imageA.output('./image-b.png', {
greyscale: true,
quality: 100,
resize: ['jimp.AUTO', 250]
});
optionsについては以下を参照。
image.isImage; // true
Fileを継承している。
引数パスの.jsonファイルを基にJSONインスタンスを作る。
作ったインスタンスを引数に解決するpromiseを返す。
const json = await new JSON('hoge.json');
引数1パスに.jsonファイルを作成する。
引数2にオブジェクトが渡されていれば.jsonファイルに反映する。
作成したファイルのJSONインスタンスを引数に解決するpromiseを返す。
// => {}
const json = await JSON.make('hoge.json');
// => {"foo":"bar"}
const json = await JSON.make('hoge.json', {foo: 'bar'});
自身の内容をオブジェクトで取得する。
取得したオブジェクトを引数に解決するpromiseを返す。
const obj = await json.read();
引数オブジェクトを基に自身の実体へ書き込む。
書き込み後に解決するpromiseを返す。
await json.write({foo: 'bar'});
json.isJSON; // true
自身の整形に使う値。
json.space = 2;
/*
{
"key": "value"
}
*/
json.space = '\t';
/*
{
"key": "value"
}
*/
Fileを継承している。
引数パスの.zipファイルを基にZIPインスタンスを作る。
作ったインスタンスを引数に解決するpromiseを返す。
const zip = await new ZIP('hoge.zip');
引数のFile, Directoryインスタンスの実体を圧縮する。
圧縮したファイルのZIPインスタンスを引数に解決するpromiseを返す。
// file.ext => file.zip
const zip = await ZIP.make(file);
// file.ext & dir => hoge.zip
const zip = await ZIP.make([file, dir], './hoge.zip');
自身の実体を同じディレクトリか引数パスのディレクトリへ展開する。
展開先のDirectoryインスタンスを引数に解決するpromiseを返す。
// hoge.zip => ...
const dir = await zip.unzip();
// hoge.zip => output/...
const dir = await zip.unzip('./output');
自身の実体が持つコンテンツ一覧を配列で取得する。
取得した配列を引数に解決するpromiseを返す。
// [...'file.ext', 'dir/']
const arr = await zip.list();
zip.isZIP; // true
Fileを継承している。
引数パスの.rarファイルを基にRARインスタンスを作る。
作ったインスタンスを引数に解決するpromiseを返す。
const rar = await new RAR('hoge.rar');
自身の実体から引数1のコンテンツを、引数2のパスのディレクトリへ展開する。
展開先のDirectoryインスタンスを引数に解決するpromiseを返す。
// hoge.rar => output/dirinrar/file
const dir = await rar.extract('dirinrar/file', 'output');
// multi
const dir = await rar.extract([
'dirinrar/fileA',
'dirinrar/fileB'
], 'output');
自身の実体から全てのコンテンツを、引数1のパスのディレクトリへ展開する。
展開先のDirectoryインスタンスを引数に解決するpromiseを返す。
// hoge.rar => output/...
const dir = await rar.extractAll('output');
自身の実体が持つコンテンツ一覧を配列で取得する。
取得した配列を引数に解決するpromiseを返す。
// [...'file.ext', 'dir/']
const arr = await rar.list();
rar.isRAR; // true
コンストラクタじゃないよ。
引数1のURLからファイルをダウンロードして引数2のパスに保存する。
保存したFileインスタンスを引数に解決するpromiseを返す。
const file = await Utility.download('http://example.com/hoge.txt', './hoge.txt');
引数パスを基に対応するインスタンスを作る。
作ったインスタンスを引数に解決するpromiseを返す。
// dir or file
const instance = await Utility.getInstance('path');
それぞれデスクトップ・ユーザー・テンポラリディレクトリのDirectoryインスタンスを作る。
作ったインスタンスを引数に解決するpromiseを返す。
const dir_desktop = await Utility.getDesktop();
const dir_home = await Utility.getHomeDir();
const dir_Temp = await Utility.getTempDir();
キャッシュを管理するオブジェクト。
詳しくはhoneo/lru-cacheを参照。
// default 1000 => 300
Utility.cache.capacity = 300;
// default 1h => 3m
Utility.cache.expire = 1000*60*3;
FAQs
fsをオブジェクトっぽく抽象化して扱う
We found that objectize-fs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.