adonis-api-resources
Version:
API resources for AdonisJS
49 lines (48 loc) • 1.93 kB
JavaScript
import fs from 'node:fs';
export async function configure(_command) {
const codemods = await _command.createCodemods();
try {
await codemods.updateRcFile((rcFile) => {
rcFile.addCommand('adonis-api-resources/commands');
});
}
catch (error) {
console.error('Unable to update adonisrc.ts file');
console.error(error);
}
function updateConfig(configName) {
const generalErrorText = 'Failed to create #resources namespace';
const alreadyErrorText = 'Namespace #resources already exists in ';
fs.readFile('./' + configName, 'utf-8', (err, jsonData) => {
if (err) {
console.log(generalErrorText, err);
}
else {
try {
let data = JSON.parse(jsonData.replace(/(?<=(true|false|null|["\d}\]])\s*),(?=\s*[}\]])/g, ''));
switch (configName) {
case 'package.json':
if (data.imports?.['#resources/*'] !== undefined) {
console.log(alreadyErrorText + configName);
}
else {
data['imports']['#resources/*'] = './app/resources/*.js';
}
break;
}
fs.writeFile('./' + configName, JSON.stringify(data, null, 2)
.replace(/(\[\n\ +\")/gm, '["')
.replace(/(\"\n\ +\])/gm, '"]'), (error) => {
if (error) {
console.log(generalErrorText, error);
}
});
}
catch (error) {
console.log(generalErrorText, error);
}
}
});
}
updateConfig('package.json');
}