Created
August 9, 2019 21:54
-
-
Save pshubham95/fa78e35d328215f3206d6829fe8d4604 to your computer and use it in GitHub Desktop.
Cordova prebuild hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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) => {