-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(exposed/fs): moves file to fs; improves functions implementation…
… and typings; adds rw
- Loading branch information
Showing
8 changed files
with
116 additions
and
52 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { default as json } from './json'; | ||
export { default as remove } from './remove'; | ||
export { default as rw } from './rw'; | ||
|
||
// TODO: copy, mkdir, move, rw |
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,28 @@ | ||
import { IOfType } from '~/types'; | ||
import expose from '~/utils/expose'; | ||
import rw from './rw'; | ||
import { IFsReadOptions } from './types'; | ||
|
||
export default expose(json); | ||
|
||
/** | ||
* Reads a JSON `file` and passes it as an argument to a callback `fn`. If the callback returns other than `undefined`, **`file` will be overwritten** with the JSON parsed response. `file` can be relative to the project's directory. | ||
* It is an *exposed* function: call `json.fn()`, which takes the same arguments, in order to execute on call. | ||
* @returns An asynchronous function -hence, calling `json` won't have any effect until the returned function is called. | ||
*/ | ||
function json( | ||
file: string, | ||
fn: ( | ||
json?: IOfType<any> | ||
) => IOfType<any> | void | Promise<IOfType<any> | void>, | ||
options: IFsReadOptions = {} | ||
): () => Promise<void> { | ||
return async () => { | ||
const _fn = async (raw?: string): Promise<string | undefined> => { | ||
const json = await fn(raw ? JSON.parse(raw) : undefined); | ||
return json ? JSON.stringify(json, null, 2) : undefined; | ||
}; | ||
|
||
return rw.fn(file, _fn, options); | ||
}; | ||
} |
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,55 @@ | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import { exists, absolute } from '~/utils/file'; | ||
import core from '~/core'; | ||
import { rejects } from 'errorish'; | ||
import expose from '~/utils/expose'; | ||
import confirm from '../prompts/confirm'; | ||
import { IFsReadOptions } from './types'; | ||
import ensure from '../tags/ensure'; | ||
|
||
export default expose(rw); | ||
|
||
/** | ||
* Reads a `file` and passes it as an argument to a callback `fn`. If the callback returns other than `undefined`, **`file` will be overwritten** with its contents. `file` can be relative to the project's directory. | ||
* It is an *exposed* function: call `rw.fn()`, which takes the same arguments, in order to execute on call. | ||
* @returns An asynchronous function -hence, calling `rw` won't have any effect until the returned function is called. | ||
*/ | ||
function rw( | ||
file: string, | ||
fn: (raw?: string) => string | void | Promise<string | void>, | ||
options: IFsReadOptions = {} | ||
): () => Promise<void> { | ||
return async () => { | ||
options = Object.assign({ confirm: false, fail: true }, options); | ||
|
||
const cwd = await core.cwd(); | ||
file = absolute({ path: file, cwd }); | ||
|
||
const read = await exists(file, { fail: options.fail }); | ||
|
||
const raw = read | ||
? await fs | ||
.readFile(file) | ||
.then(String) | ||
.catch(rejects) | ||
: undefined; | ||
|
||
const response = await fn(raw); | ||
if (response !== undefined) { | ||
if (options.confirm) { | ||
const action = await confirm | ||
.fn(`Write "${path.relative(file, cwd)}"?`, { no: false }) | ||
.then((x) => x !== false); | ||
|
||
if (!action) { | ||
if (options.fail) throw Error(`Cancelled by user`); | ||
else return; | ||
} | ||
} | ||
|
||
await ensure.fn(path.parse(file).dir); | ||
await fs.writeFile(file, String(response)).catch(rejects); | ||
} | ||
}; | ||
} |
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,19 @@ | ||
/** | ||
* Options taken by *fs* functions. | ||
*/ | ||
export interface IFsOptions { | ||
/** | ||
* If `true`, it will require user confirmation for removal. Defaults to `false`. | ||
*/ | ||
confirm?: boolean; | ||
} | ||
|
||
/** | ||
* Options taken by *fs* functions that perform file reads. | ||
*/ | ||
export interface IFsReadOptions extends IFsOptions { | ||
/** | ||
* If `false`, it won't fail if a path doesn't exist or the user doesn't confirm. Defaults to `true`. | ||
*/ | ||
fail?: boolean; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export * from './exec'; | ||
export * from './file'; | ||
export * from './fs'; | ||
export * from './prompts'; | ||
export * from './tags'; | ||
export { default as options } from './options'; |
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