I have a project with a client and server side. On the client-side, users get authenticated in github, and then select a template thumbnail, and then we sent a token and the template details to the node service side, where a new repository is generated in github. So, far, I have created this pipeline and it works fine. A new repo is generated, but it’s empty.. I need react to be installed in the newly generated repository, but I can’t make it work.
This is the relevant function in my node server that generates the repo:
async function createRepository(authAccessToken, repositoryName, repositoryDescription, repositoryFramework, templateName) {
const accessToken = authAccessToken;
const repoName = repositoryName;
const repoDescription = repositoryDescription;
const repoFramework = repositoryFramework;
console.log(templateName, repoFramework)
const headers = {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
};
const data = {
'name': repoName,
'description': repoDescription,
'private': false
};
const response = fetch('https://api.github.com/user/repos', {
method: 'POST',
headers,
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
if(data.html_url) {
console.log(`Repository created: ${data.html_url}`)
return data.html_url;
} else {
console.log('Failed to generate repository')
return null;
}
})
.catch(error => console.error(`Error creating repository: ${error}`));
if(response) {
return response;
} else {
return null;
}
}
I have tried several things:
I tried adding this:
const repoDir = `./${repoName}`;
fs.mkdirSync(repoDir);
exec(`cd ${repoDir} && npx create-react-app ./`, (error, stdout, stderr) => {
if (error) {
return null;
}
});
It didn’t work.
I tried:
console.log(‘Creating React app…’);
await runCommand(‘npm’, [‘exec’, ‘create-react-app’, ‘.’], { cwd: repoDir });
console.log(‘React app created successfully.’);
Also didn’t work..
I tried a few other similar adjustments, but it doesn’t work. For some I would get an error like: Error: spawn npm ENOENT
.. for others I don’t get any errors, the repo is just empty..
I tried increasing the time of the exec()
so it doesn’t time out while react is being generated, but it seems that the server restarts after a few seconds after exec()
is invoked.. not really sure what is the problem, and if the timing out is the problem.