1 | const fs = require('fs').promises;
|
2 | const { exists } = require('../util/helpers');
|
3 |
|
4 | const templateInfo = [
|
5 | {
|
6 | name: 'postcss',
|
7 | filepath: '.postcssrc.js',
|
8 | template: `\
|
9 | const { combineConfigSync } = require('@gera2ld/plaid/util/helpers');
|
10 | const precss = require('@gera2ld/plaid/postcss/precss');
|
11 |
|
12 | module.exports = combineConfigSync({}, [precss]);`,
|
13 | successMessage: 'PostCSS config is generated successfully at .postcssrc.js',
|
14 | },
|
15 | ];
|
16 |
|
17 |
|
18 | async function generate(cmd, name) {
|
19 | const info = templateInfo.find(info => info.name === name);
|
20 | if (info) {
|
21 | await handleConflict(info.filepath, info.template);
|
22 | console.info(info.successMessage);
|
23 | return;
|
24 | }
|
25 | throw new Error(`Unknown type to generate: ${name}`);
|
26 | }
|
27 |
|
28 | async function handleConflict(filepath, content) {
|
29 | if (await exists(filepath, { file: true })) {
|
30 | throw new Error(`File already exists: ${filepath}`);
|
31 | }
|
32 | return fs.writeFile(filepath, content, 'utf8');
|
33 | }
|
34 |
|
35 | generate.templateInfo = templateInfo;
|
36 | module.exports = generate;
|