@neodx/vfs
Version:
Simple virtual file system - working dir context, lazy changes, different modes, integrations and moreover
56 lines (53 loc) • 1.64 kB
JavaScript
import {
d as displayPath,
g as getVfsActions,
h as createTaskRunner
} from '../_internal/operations-BqwMWtvn.mjs';
import { tryFormatPrettier } from '@neodx/pkg-misc';
import { isTypeOfString, concurrently } from '@neodx/std';
import { c as createVfsPlugin } from '../_internal/create-vfs-plugin-BzqnUd8c.mjs';
function prettier({ auto = true } = {}) {
return createVfsPlugin('prettier', (vfs, { context, beforeApply }) => {
const log = context.log.child('prettier');
const { task } = createTaskRunner({
log
});
const format = task(
'format',
async (path, content) => {
const formattedContent = await tryFormatPrettier(
path,
isTypeOfString(content) ? content : content.toString('utf-8')
);
if (formattedContent !== null) {
await vfs.write(path, formattedContent);
}
return formattedContent !== null;
},
{
mapSuccessMessage: (formatted, path) =>
`${displayPath(context, path)} ${formatted ? 'formatted' : 'skipped'}`
}
);
vfs.format = async path => await format(path, await vfs.read(path, 'utf-8'));
vfs.formatAll = task(
'format all',
async () => {
await concurrently(
await getVfsActions(context, ['create', 'update']),
async ({ path, content }) => format(path, content),
5
);
},
{
mapSuccessMessage: () => 'formatted all changed files'
}
);
if (auto) {
beforeApply(() => vfs.formatAll());
}
return vfs;
});
}
export { prettier };
//# sourceMappingURL=prettier.mjs.map