Commit 85351a14 authored by Aral Balkan's avatar Aral Balkan
Browse files

Simplify build script by using a whitelist of files to keep

parent 01533b16
......@@ -5,12 +5,10 @@
// Generic Small Web Client build script.
//
// Build the client into a …/<client-name>-dist folder,
// optionally backing up and restoring a .git folder
// if the distribution folder happens to be under source control.
// (Note: you must manually create a git repository and configure
// it you build the client for the first time.)
// intelligently clearing it out first without destroying essential
// items like the .git folder.
//
// Copyright ⓒ 2021 Aral Balkan. Licensed under AGPLv3 or later.
// Copyright ⓒ 2021-present Aral Balkan. Licensed under AGPL v3.
// Shared with ♥ by the Small Technology Foundation.
//
//////////////////////////////////////////////////////////////////////
......@@ -29,15 +27,6 @@ const currentDirectoryName = currentPathSegments[currentPathSegments.length - 1]
const distDirectoryName = `${currentDirectoryName}-dist`
const distDirectoryPath = path.resolve('..', distDirectoryName)
const distDirectoryGitPath = path.join(distDirectoryPath, '.git')
const distGitBackupDirectoryName = `${distDirectoryName}.git`
const distGitBackupDirectoryPath = path.resolve('..', distGitBackupDirectoryName)
const distOldGitBackupDirectoryName = `${distGitBackupDirectoryName}.old`
const distOldGitBackupDirectoryPath = path.resolve('..', distOldGitBackupDirectoryName)
const gitIgnoreFilePath = path.join(distDirectoryPath, '.gitignore')
// Exec with promises and piped stdout.
async function exec(command) {
......@@ -49,62 +38,28 @@ async function exec(command) {
console.log('Building…')
let gitIgnoreFile = null
// When cleaning the distribution directory, do not erase these files/directories.
const itemsToKeep = [
'.git',
'.gitignore',
'README.md',
'LICENSE'
]
// Clean up the existing build first.
if (fs.existsSync(distDirectoryPath)) {
// Backup the .gitignore file, if one exists
if (fs.existsSync(gitIgnoreFilePath)) {
console.log(' - Backing up .gitignore file from distribution directory.')
gitIgnoreFile = fs.readFileSync(gitIgnoreFilePath, 'utf-8')
} else {
console.log(' - No .gitignore file found in distribution directory.')
}
// Backup the git folder if the distribution directory is under source control.
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(' - Cleaning distribution directory.')
const items = fs.readdirSync(distDirectoryPath)
items.forEach(item => {
if (!itemsToKeep.includes(item)) {
console.log(` - Removing ${item}`)
fs.rmSync(path.join(distDirectoryPath, item), {recursive: true})
}
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.
console.log(' - Backing up .git folder from distribution directory.')
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 {
console.log(' - Distribution directory not under version control; not backing up .git directory.')
}
})
} else {
console.log(' - Initial build.')
}
await exec('npx vite build', { env: process.env })
if (fs.existsSync(distGitBackupDirectoryPath)) {
// Back up the git folder so it’s not nuked by the Snowpack build.
console.log('Restoring .git folder into distribution directory.')
await exec(`cp -R ${distGitBackupDirectoryPath} ${distDirectoryGitPath}`, { env: process.env })
// And restore the previously-backed up .gitignore file, if one exists.
if (gitIgnoreFile !== null) {
console.log('Restoring .gitignore file.')
fs.writeFileSync(gitIgnoreFilePath, gitIgnoreFile, 'utf-8')
}
} else {
console.log(' - Distribution directory not under version control; no .git directory to restore.')
}
console.log('Done.')
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