temp-sandbox

Create a predictable temporary directory that makes it easy to create, list, and delete files.

MIT 22 个版本
安装
npm install temp-sandbox
yarn add temp-sandbox
pnpm add temp-sandbox
bun add temp-sandbox
README

temp-sandbox

npm Linux Build Status Windows Build Status Code Coverage

About

Create a predictable temporary directory that makes it easy to create, list, and delete files inside of the sandbox.

Installation

npm install --save-dev temp-sandbox

Usage

// index.test.js
import { TempSandbox } from 'temp-sandbox';

// Create sandbox with a predictable directory name
const sandbox = new TempSandbox();

// Create sandbox with randomized directory name
// Typically used inside of a beforeEach
let sandboxRandom = new TempSandbox({ randomDir: true });

beforeEach(async () => {
	sandboxRandom = new TempSandbox({ randomDir: true });

	// Remove all files/directories inside sandbox
	await sandbox.clean();
	sandbox.cleanSync();
});

afterAll(async () => {
	// delete sandbox and sandbox instance
	await sandbox.destroySandbox();
	sandbox.destroySandboxSync();
});

test('create file', async () => {
	// Get absolute path of <SANDBOX>/file1.js
	sandbox.path.resolve('file1.js');

	// Get relative path of <SANDBOX>/nested/file1.js
	sandbox.path.relative('<SANDBOX>/nested/file1.js');

	// Get relative path of <SANDBOX>/nested/file1.js from <SANDBOX>/nested directory
	sandbox.path.relative('nested', '<SANDBOX>/nested/file1.js'); // file1.js

	// Create directory <SANDBOX>/nested/dir
	await sandbox.createDir('nested/dir');
	sandbox.createDirSync('nested/dir');

	// Create file <SANDBOX>/file1.js with contents: // file1.js
	await sandbox.createFile('file1.js', '// file1.js');
	sandbox.createFileSync('file1.js', '// file1.js');

	// Read <SANDBOX>/file1.js contents
	await sandbox.readFile('file1.js');
	sandbox.readFileSync('file1.js');

	// Delete file/folders <SANDBOX>/file1.js
	await sandbox.delete('file1.js');
	sandbox.deleteSync('file1.js');

	// List all files inside sandbox
	const fileList = await sandbox.getFileList();
	const fileListSync = sandbox.getFileListSync();

	// List all files and their checksum
	const fileHashes = await sandbox.getAllFilesHash();
	const fileHashesSync = sandbox.getAllFilesHashSync();

	// Get checksum of file1.js
	const file1Hash = await sandbox.getFileHash('file1.js');
	const file1HashSync = sandbox.getFileHashSync('file1.js');
});
版本列表
4.0.1 2019-07-11
4.0.0 2019-07-11
3.0.0 2019-05-18
2.0.0 2019-04-22
1.0.17 2019-03-04
1.0.16 2019-01-29
1.0.15 2019-01-18
1.0.14 2019-01-18
1.0.13 2019-01-17
1.0.12 2019-01-17
1.0.11 2019-01-16
1.0.10 2019-01-16
1.0.9 2019-01-15
1.0.8 2019-01-15
1.0.7 2019-01-08
1.0.5 2019-01-04
1.0.4 2019-01-04
1.0.3 2018-07-25
1.0.2 2018-07-24
1.0.1 2018-05-30
1.0.0 2018-05-29
0.0.1 2018-05-29