diff --git a/package-lock.json b/package-lock.json index 37fc2d5..ea2dc31 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,6 +31,7 @@ "eslint-config-sukka": "^4.1.10", "eslint-formatter-sukka": "^4.1.10", "fdir": "^6.2.0", + "memdisk": "^1.0.2", "mocha": "^10.4.0", "mocha-chai-jest-snapshot": "^1.1.4", "picocolors": "^1.0.0", @@ -2315,6 +2316,12 @@ "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==" }, + "node_modules/@types/gensync": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@types/gensync/-/gensync-1.0.4.tgz", + "integrity": "sha512-C3YYeRQWp2fmq9OryX+FoDy8nXS6scQ7dPptD8LnFDAUNcKWJjXQKDNJD3HVm+kOUsXhTOkpi69vI4EuAr95bA==", + "dev": true + }, "node_modules/@types/graceful-fs": { "version": "4.1.9", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.9.tgz", @@ -3082,6 +3089,15 @@ "integrity": "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==", "dev": true }, + "node_modules/commander": { + "version": "12.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz", + "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==", + "dev": true, + "engines": { + "node": ">=18" + } + }, "node_modules/commondir": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", @@ -4798,6 +4814,47 @@ "tmpl": "1.0.5" } }, + "node_modules/memdisk": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/memdisk/-/memdisk-1.0.2.tgz", + "integrity": "sha512-H+12zunRzrCd3twH5UYkGbXwy3IcxJbq7fhaczihxwd2kAdkmKvWI1umEZ1+OMDZV1SGLA8d4CJ7F6tJWDvZCg==", + "dev": true, + "dependencies": { + "@jsdevtools/ez-spawn": "^3.0.4", + "@types/gensync": "^1.0.4", + "commander": "^12.1.0", + "gensync": "1.0.0-beta.2", + "picocolors": "^1.0.1", + "which": "^4.0.0" + }, + "bin": { + "memdisk": "dist/cli.js" + } + }, + "node_modules/memdisk/node_modules/isexe": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-3.1.1.tgz", + "integrity": "sha512-LpB/54B+/2J5hqQ7imZHfdU31OlgQqx7ZicVlkm9kzg9/w8GKLEcFfJl/t7DCEDueOyBAD6zCCwTO6Fzs0NoEQ==", + "dev": true, + "engines": { + "node": ">=16" + } + }, + "node_modules/memdisk/node_modules/which": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/which/-/which-4.0.0.tgz", + "integrity": "sha512-GlaYyEb07DPxYCKhKzplCWBJtvxZcZMrL+4UkrTSJHHPyZU4mYYTv3qaOe77H7EODLSSopAUFAc6W8U4yqvscg==", + "dev": true, + "dependencies": { + "isexe": "^3.1.1" + }, + "bin": { + "node-which": "bin/which.js" + }, + "engines": { + "node": "^16.13.0 || >=18.0.0" + } + }, "node_modules/merge-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", @@ -5218,9 +5275,9 @@ } }, "node_modules/picocolors": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", - "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==", + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.1.tgz", + "integrity": "sha512-anP1Z8qwhkbmu7MFP5iTt+wQKXgwzf7zTyGlcdzabySa9vd0Xt392U0rVmz9poOaBj0uHJKyyo9/upk0HrEQew==", "dev": true }, "node_modules/picomatch": { diff --git a/package.json b/package.json index f5e7b4e..5f563a5 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "eslint-config-sukka": "^4.1.10", "eslint-formatter-sukka": "^4.1.10", "fdir": "^6.2.0", + "memdisk": "^1.0.2", "mocha": "^10.4.0", "mocha-chai-jest-snapshot": "^1.1.4", "picocolors": "^1.0.0", diff --git a/test/index.ts b/test/index.ts index 84c7676..8a14ba9 100644 --- a/test/index.ts +++ b/test/index.ts @@ -20,7 +20,7 @@ import type { JsMinifyOptions } from '@swc/core'; import chai from 'chai'; import { jestSnapshotPlugin } from 'mocha-chai-jest-snapshot'; -import { cleanup, init } from './ramdisk'; +import { create, destroy } from 'memdisk'; import { async as ezspawn } from '@jsdevtools/ez-spawn'; @@ -377,7 +377,7 @@ const tests = (rollupImpl: typeof rollup2 | typeof rollup3 | typeof rollup4, iso }; describe('rollup-plugin-swc3', () => { - const ramDiskPath = init('rolluppluginswc3test'); + const ramDiskPath = create.sync('rolluppluginswc3test', 64 * 1024 * 1024); describe('swc (rollup 2)', () => { const isolateDir = path.join(ramDiskPath, 'rollup2'); @@ -394,5 +394,5 @@ describe('rollup-plugin-swc3', () => { tests(rollup4, isolateDir); }); - after(() => cleanup(ramDiskPath)); + after(() => destroy.sync(ramDiskPath)); }); diff --git a/test/ramdisk.ts b/test/ramdisk.ts deleted file mode 100644 index 0863e66..0000000 --- a/test/ramdisk.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { existsSync, unlinkSync, mkdirSync } from 'fs'; -import path from 'path'; -import pc from 'picocolors'; -import { sync as ezspawn } from '@jsdevtools/ez-spawn'; -import { platform } from 'process'; -import { tmpdir } from 'os'; - -const create = (blocks: number, root: string) => { - if (platform === 'darwin' || platform === 'linux') { - const commands = { - darwin: `hdiutil attach -nomount ram://${blocks}`, - linux: `sudo mkdir -p ${root}` - }; - console.info(pc.blue('swc3 ramdisk:'), 'Initializing RAMdisk. You may be prompted for credentials'); - const { stdout: diskPath } = ezspawn(commands[platform]); - return diskPath.trim(); - } - - throw new Error('Unsupported platform!'); -}; - -const mount = (bytes: number, diskPath: string, name: string, root: string) => { - if (platform === 'darwin') { - console.info(pc.blue('swc3 ramdisk:'), `Mouting RAMdisk at ${diskPath}. You may be prompted for credentials`); - return ezspawn(`diskutil erasevolume HFS+ ${name} ${diskPath}`); - } - if (platform === 'linux') { - console.info(pc.blue('swc3 ramdisk:'), `Mouting RAMdisk at ${root}. You may be prompted for credentials`); - return ezspawn(`sudo mount -t tmpfs -o size=${bytes} tmpfs ${root}`); - } - - throw new Error('Unsupported platform!'); -}; - -export const cleanup = (root: string) => { - if (platform === 'darwin' || platform === 'linux') { - const commands = { - darwin: `hdiutil detach ${root}`, - linux: `sudo umount ${root}` - }; - - console.info(pc.yellow('swc3 ramdisk:'), `Unmouting RAMdisk at ${root}. You may be prompted for credentials`); - - return ezspawn(commands[platform]); - } - - return unlinkSync(root); -}; - -export const init = (name: string, bytes = 1.28e8 /** 128 MiB */, blockSize = 512) => { - if (platform === 'darwin' || platform === 'linux') { - const root = platform === 'darwin' ? `/Volumes/${name}` : `/mnt/${name}`; - const blocks = bytes / blockSize; - - if (!existsSync(root)) { - const diskPath = create(blocks, root); - mount(bytes, diskPath, name, root); - } - - console.info(pc.green('swc3 ramdisk:'), `RAMdisk is avaliable at ${root}.`); - - return root; - } - - console.info(pc.red('swc3 ramdisk:'), 'The current platform does not support RAMdisks. Using a temporary directory instead.'); - - const root = path.join(tmpdir(), '.fake-ramdisk', name); - mkdirSync(root, { recursive: true }); - - return root; -};