-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathactionTypes.mjs
More file actions
31 lines (28 loc) · 939 Bytes
/
actionTypes.mjs
File metadata and controls
31 lines (28 loc) · 939 Bytes
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
29
30
31
import path from 'node:path';
import fs from 'fs-extra';
export const ADD_TO_BARREL_FILE = 'addToBarrelFile';
export function setActionTypes(plop) {
plop.setActionType(ADD_TO_BARREL_FILE, addToBarrelFile);
}
async function addToBarrelFile(answers, config, plop) {
const renderedPath = plop.renderString(config.path, {
...answers,
...config.data
});
const pathToFile = path.isAbsolute(renderedPath) ? renderedPath : path.resolve(plop.getPlopfilePath(), renderedPath);
let fileContents = '';
try {
fileContents = await fs.readFile(pathToFile, 'utf8');
} catch (_) {
fileContents = '';
}
const lines = fileContents.split('\n');
const finalLine = lines.lastIndexOf('');
const updatedLines = [
...lines.slice(0, finalLine),
plop.renderString(config.template, { ...answers, ...config.data }),
''
];
await fs.outputFile(pathToFile, updatedLines.join('\n'));
return `-> ${pathToFile}`;
}