Skip to content

Commit

Permalink
feat: Create a postAction script to run via node after template installs
Browse files Browse the repository at this point in the history
Dotnet template prompts user if they want to run.
  • Loading branch information
BenjaminMichaelis committed Sep 20, 2024
1 parent 42b168c commit 06ecc57
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,19 @@
}
]
}
}
},
"postActions": [
{
"actionId": "3A7C4B45-1F5D-4A30-959A-51B88E82B5D2",
"description": "Run npm lint:fix in the .Web directory after template instantiation",
"manualInstructions": [
{ "text": "Install npm packages, and run linter to resolve linting issues." }
],
"args": {
"executable": "node",
"args": "postAction.js"
},
"continueOnError": true
}
]
}
59 changes: 59 additions & 0 deletions templates/Coalesce.Vue.Template/content/postAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');

const rootDir = process.cwd();

function findWebDirectory(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
if (file.endsWith('.Web')) {
return fullPath;
} else {
const result = findWebDirectory(fullPath);
if (result) return result;
}
}
}
return null;
}

const webDir = findWebDirectory(rootDir);

if (webDir) {
console.log(`Found .Web directory: ${webDir}`);
exec('npm install', { cwd: webDir }, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing npm install: ${error.message}`);
console.error(stderr);
console.log(stdout);
return;
}
console.log(stdout);
console.error(stderr);

exec('npm run lint:fix', { cwd: webDir }, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing npm run lint:fix: ${error.message}`);
console.error(stderr);
console.log(stdout);
return;
}
console.log(stdout);
console.error(stderr);

// Delete the script after execution
fs.unlink(__filename, (err) => {
if (err) {
console.error(`Error deleting script: ${err.message}`);
} else {
console.log('Script deleted successfully.');
}
});
});
});
} else {
console.error('No .Web directory found.');
}

0 comments on commit 06ecc57

Please sign in to comment.