Skip to content

Commit

Permalink
feat: 🎸 add mkdtempSync() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent 57f386b commit 1ac2df4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion demo/fsa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => {
console.log('mkdirSync() - can create a nested directory');
fs.mkdirSync('/public/site/assets/img', {recursive: true});
strictEqual(fs.statSync('/public/site/assets/img').isDirectory(), true);

console.log('mkdtempSync() - can create a temporary directory');
await fs.promises.mkdir('/tmp');
const tmpDirName = fs.mkdtempSync('/tmp/temporary-');
strictEqual(fs.statSync(tmpDirName).isDirectory(), true);
};

const main = async () => {
const button = document.createElement("button");
button.textContent = "Select folder";
button.textContent = "Select an empty folder";
document.body.appendChild(button);
button.onclick = async () => {
const dir = await (window as any).showDirectoryPicker({id: 'demo', mode: 'readwrite'});
Expand Down
11 changes: 9 additions & 2 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getOptions,
getStatOptions,
getAppendFileOpts,
getDefaultOpts,
} from '../node/options';
import {
bufToUint8,
Expand Down Expand Up @@ -875,10 +876,16 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
return this.getSyncAdapter().call('mkdir', [filename, options]);
};

public readonly mkdtempSync: FsSynchronousApi['mkdtempSync'] = (prefix: string, options?: opts.IOptions): misc.TDataOut => {
const {encoding} = getDefaultOpts(options);
if (!prefix || typeof prefix !== 'string') throw new TypeError('filename prefix is required');
nullCheck(prefix);
const result = this.getSyncAdapter().call('mkdtemp', [prefix, options]);
return strToEncoding(result, encoding);
};

public readonly ftruncateSync: FsSynchronousApi['ftruncateSync'] = notSupported;
public readonly linkSync: FsSynchronousApi['linkSync'] = notSupported;
public readonly mkdirpSync: FsSynchronousApi['mkdirpSync'] = notSupported;
public readonly mkdtempSync: FsSynchronousApi['mkdtempSync'] = notSupported;
public readonly openSync: FsSynchronousApi['openSync'] = notSupported;
public readonly readdirSync: FsSynchronousApi['readdirSync'] = notSupported;
public readonly readlinkSync: FsSynchronousApi['readlinkSync'] = notSupported;
Expand Down
1 change: 1 addition & 0 deletions src/fsa-to-node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface FsaNodeSyncAdapterApi {
rmdir(req: [filename: string, opts?: opts.IRmdirOptions]): void;
rm(req: [filename: string, opts?: opts.IRmOptions]): void;
mkdir(req: [filename: string, opts?: misc.TMode | opts.IMkdirOptions]): string | undefined;
mkdtemp(req: [filename: string, opts?: misc.TMode | opts.IOptions]): string;
}

export interface FsaNodeSyncAdapter {
Expand Down
3 changes: 3 additions & 0 deletions src/fsa-to-node/worker/FsaNodeSyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,8 @@ export class FsaNodeSyncWorker {
mkdir: async ([filename, options]): Promise<string | undefined> => {
return await this.fs.promises.mkdir(filename, options);
},
mkdtemp: async ([filename]): Promise<string> => {
return await this.fs.promises.mkdtemp(filename, {encoding: 'utf8'}) as string;
},
};
}
1 change: 0 additions & 1 deletion src/node/types/FsSynchronousApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export interface FsSynchronousApi {
mkdirSync(path: misc.PathLike, options: opts.IMkdirOptions & { recursive: true }): string | undefined;
mkdirSync(path: misc.PathLike, options?: misc.TMode | (opts.IMkdirOptions & { recursive?: false })): void;
mkdirSync(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): string | undefined;
mkdirpSync(path: misc.PathLike, mode?: misc.TMode): void;
mkdtempSync(prefix: string, options?: opts.IOptions): misc.TDataOut;
rmdirSync(path: misc.PathLike, options?: opts.IRmdirOptions): void;
rmSync(path: misc.PathLike, options?: opts.IRmOptions): void;
Expand Down

0 comments on commit 1ac2df4

Please sign in to comment.