Commit 952db29b authored by Aral Balkan's avatar Aral Balkan
Browse files

Update build script

parent a581d1f3
...@@ -21,7 +21,7 @@ import process from 'process' ...@@ -21,7 +21,7 @@ import process from 'process'
import childProcess from 'child_process' import childProcess from 'child_process'
import { promisify } from 'util' import { promisify } from 'util'
const exec = promisify(childProcess.exec) const _exec = promisify(childProcess.exec)
const currentPath = path.resolve('.') const currentPath = path.resolve('.')
const currentPathSegments = currentPath.split(path.sep) const currentPathSegments = currentPath.split(path.sep)
...@@ -34,8 +34,12 @@ const distDirectoryGitPath = path.join(distDirectoryPath, '.git') ...@@ -34,8 +34,12 @@ const distDirectoryGitPath = path.join(distDirectoryPath, '.git')
const distGitBackupDirectoryName = `${distDirectoryName}.git` const distGitBackupDirectoryName = `${distDirectoryName}.git`
const distGitBackupDirectoryPath = path.resolve('..', distGitBackupDirectoryName) const distGitBackupDirectoryPath = path.resolve('..', distGitBackupDirectoryName)
async function execWithPromisesAndProgress(command) { const distOldGitBackupDirectoryName = `${distGitBackupDirectoryName}.old`
const commandPromise = exec(command) const distOldGitBackupDirectoryPath = path.resolve('..', distOldGitBackupDirectoryName)
// Exec with promises and piped stdout.
async function exec(command) {
const commandPromise = _exec(command)
const commandProcess = commandPromise.child const commandProcess = commandPromise.child
commandProcess.stdout.pipe(process.stdout) commandProcess.stdout.pipe(process.stdout)
await commandPromise await commandPromise
...@@ -46,9 +50,28 @@ console.log('Building…') ...@@ -46,9 +50,28 @@ console.log('Building…')
if (fs.existsSync(distDirectoryPath)) { if (fs.existsSync(distDirectoryPath)) {
// Backup the git folder if the distribution directory is under source control. // Backup the git folder if the distribution directory is under source control.
if (fs.existsSync(distDirectoryGitPath)) { if (fs.existsSync(distDirectoryGitPath)) {
// First, move the old backup, if one exists.
if (fs.existsSync(distGitBackupDirectoryPath)) {
// Remove the temporarily moved old backup, if one exists.
// (In case one was left over from an error in a previous build attempt.)
if (fs.existsSync(distOldGitBackupDirectoryPath)) {
console.log(' - Removing old .git folder backup.')
await exec(`rm -rf ${distOldGitBackupDirectoryPath}`)
}
console.log(' - Temporarily moving old .git folder backup.')
await exec(`mv ${distGitBackupDirectoryPath} ${distOldGitBackupDirectoryPath}`)
}
// Back up the git folder so it’s not nuked by the Snowpack build. // Back up the git folder so it’s not nuked by the Snowpack build.
console.log(' - Backing up .git folder from distribution directory.') console.log(' - Backing up .git folder from distribution directory.')
await execWithPromisesAndProgress(`cp -R ${distDirectoryGitPath} ${distGitBackupDirectoryPath}`, { env: process.env }) await exec(`cp -R ${distDirectoryGitPath} ${distGitBackupDirectoryPath}`, { env: process.env })
// Remove the temporarily moved old backup, if one exists.
if (fs.existsSync(distOldGitBackupDirectoryPath)) {
console.log(' - Removing old .git folder backup.')
await exec(`rm -rf ${distOldGitBackupDirectoryPath}`)
}
} else { } else {
console.log(' - Distribution directory not under version control; not backing up .git directory.') console.log(' - Distribution directory not under version control; not backing up .git directory.')
} }
...@@ -56,12 +79,12 @@ if (fs.existsSync(distDirectoryPath)) { ...@@ -56,12 +79,12 @@ if (fs.existsSync(distDirectoryPath)) {
console.log(' - Initial build.') console.log(' - Initial build.')
} }
await execWithPromisesAndProgress('npx snowpack build', { env: process.env }) await exec('npx snowpack build', { env: process.env })
if (fs.existsSync(distGitBackupDirectoryPath)) { if (fs.existsSync(distGitBackupDirectoryPath)) {
// Back up the git folder so it’s not nuked by the Snowpack build. // Back up the git folder so it’s not nuked by the Snowpack build.
console.log('Restoring .git folder into distribution directory.') console.log('Restoring .git folder into distribution directory.')
await execWithPromisesAndProgress(`cp -R ${distGitBackupDirectoryPath} ${distDirectoryGitPath}`, { env: process.env }) await exec(`cp -R ${distGitBackupDirectoryPath} ${distDirectoryGitPath}`, { env: process.env })
} else { } else {
console.log(' - Distribution directory not under version control; no .git directory to restore.') console.log(' - Distribution directory not under version control; no .git directory to restore.')
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment