-
Notifications
You must be signed in to change notification settings - Fork 4
/
write-files.ts
28 lines (24 loc) · 912 Bytes
/
write-files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import fs from 'fs-extra';
import path from 'path';
import Listr from 'listr';
import makeDir from 'make-dir';
import { IMergedFiles } from './merge-files';
import { wait } from './wait';
import { IOptions } from './const';
const writeFile = async (pathName: string, data: string | object) => {
await makeDir(path.join(pathName.replace(path.basename(pathName), '')));
await fs.writeFile(pathName, typeof data === 'string' ? data : JSON.stringify(data, null, 2));
};
export const writeFiles = async (files: string[], mergedFiles: IMergedFiles, { cwd }: IOptions) => {
const tasks = new Listr(
files.map((file: string) => ({
title: file,
task: () => Promise.all([writeFile(path.join(cwd, file), mergedFiles[file].data), wait()]),
})),
{
concurrent: 3,
renderer: process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'ci' ? 'silent' : 'default',
}
);
return await tasks.run();
};