Skip to content

Instantly share code, notes, and snippets.

@pshubham95
Created August 9, 2019 21:54
Show Gist options
  • Save pshubham95/fa78e35d328215f3206d6829fe8d4604 to your computer and use it in GitHub Desktop.
Save pshubham95/fa78e35d328215f3206d6829fe8d4604 to your computer and use it in GitHub Desktop.
Cordova prebuild hook
const path = require('path');
const { exec } = require('child_process');
const fs = require('fs');
const rimraf = require('rimraf');
function renameOutputFolder(buildFolderPath, outputFolderPath) {
return new Promise((resolve, reject) => {
fs.rename(buildFolderPath, outputFolderPath, (err) => {
if (err) {
reject(err);
} else {
resolve('Successfully built!');
}
});
});
}
function execPostReactBuild(buildFolderPath, outputFolderPath) {
return new Promise((resolve, reject) => {
if (fs.existsSync(buildFolderPath)) {
if (fs.existsSync(outputFolderPath)) {
rimraf(outputFolderPath, (err) => {
if (err) {
reject(err);
return;
}
renameOutputFolder(buildFolderPath, outputFolderPath)
.then(val => resolve(val))
.catch(e => reject(e));
});
} else {
renameOutputFolder(buildFolderPath, outputFolderPath)
.then(val => resolve(val))
.catch(e => reject(e));
}
} else {
reject(new Error('build folder does not exist'));
}
});
}
module.exports = () => {
const projectPath = path.resolve(process.cwd(), './node_modules/.bin/react-scripts');
return new Promise((resolve, reject) => {
exec(`${projectPath} build`,
(error) => {
if (error) {
console.error(error);
reject(error);
return;
}
execPostReactBuild(path.resolve(__dirname, '../build/'), path.join(__dirname, '../www/'))
.then((s) => {
console.log(s);
resolve(s);
})
.catch((e) => {
console.error(e);
reject(e);
});
});
});
};
@mariogarcia-ar
Copy link

mariogarcia-ar commented Jul 5, 2023

If you have an error like "rimraf is not a function" you can use

const fs = require('fs-extra'); `

and replace
rimraf(outputFolderPath, (err) => {

with

fs.remove(outputFolderPath, (err) => {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment