Skip to content

Commit

Permalink
fix: temporarily rename env files when using development build comman…
Browse files Browse the repository at this point in the history
…ds (#233)

* fix: temporarily rename env files when using development build commands

* fix: fix target arg
  • Loading branch information
rjschill87 committed Nov 9, 2023
1 parent 729b576 commit 493b1ba
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
51 changes: 48 additions & 3 deletions tooling/cli/lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const childProcess = require('child_process');
const tar = require('tar');
const os = require('os');

const { getFilePaths } = require('./helpers/filepaths');
const { getFilePaths, getFileContents } = require('./helpers/filepaths');
const { instanceEndpoint } = require('./helpers/urls');
const { generateTranslationFile } = require('./file-generators');
const {
Expand Down Expand Up @@ -104,8 +104,13 @@ class DeploymentError extends Error {

// hash queries for whitelisting
await writeGraphqlManifest();

await maybeRenameEnvFile();

await uploadHeliumProject(policyData);
// reset translations file to include all keys for local development

await maybeResetEnvFile();
// // reset translations file to include all keys for local development
await resetTranslationFile();
const batchJobId = await triggerBatch(instance, key);

Expand Down Expand Up @@ -140,12 +145,52 @@ process.on('message', message => {
console.log('>>> Deploy process receive message', message);
});

async function maybeRenameEnvFile() {
// temporarily swap env files if it's a dev build.
// the CF Worker still loads an env file into process.env if it's present
// so we could end up with a project built with .env.development running
// w/ vars passed from .env instead.
if (isDevelopmentBuild()) {
await swapEnvFiles('.env', '.env.development', '.env.backup');
}

return;
}

async function maybeResetEnvFile() {
if (isDevelopmentBuild()) {
await swapEnvFiles('.env', '.env.backup', '.env.development');
}

return;
}

async function swapEnvFiles(target, source, newFilePath) {
const targetPath = path.join(OP_DIR, target);
const sourcePath = path.join(OP_DIR, source);

const hasTarget = await getFileContents(targetPath);
const hasSource = await getFileContents(sourcePath);

if (hasTarget && hasSource) {
const newPath = path.join(OP_DIR, newFilePath);
await rename(targetPath, newPath);
await rename(sourcePath, targetPath);
}

return;
}

function isDevelopmentBuild() {
return String(DEVELOPMENT_BUILD) === 'true';
}

async function buildProject(hasAtoms) {
return new Promise((resolve, reject) => {
const exec = childProcess.exec;

let buildCommandSuffix = hasAtoms ? 'atoms' : 'vite';
if (buildCommandSuffix === 'vite' && String(DEVELOPMENT_BUILD) === 'true') {
if (buildCommandSuffix === 'vite' && isDevelopmentBuild()) {
buildCommandSuffix = 'development';
}

Expand Down
14 changes: 2 additions & 12 deletions tooling/cli/lib/helpers/atoms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { getFilePaths } = require('./filepaths');
const { getFilePaths, getFileContents } = require('./filepaths');
const path = require('path');
const { access, readFile, writeFile } = require('fs/promises');
const { writeFile } = require('fs/promises');
const crypto = require('crypto');

const dirHasAtoms = async dir => {
Expand Down Expand Up @@ -54,14 +54,4 @@ const compileStyles = async (dir, atomsStyleHash) => {
return updatedStyleHash;
};

const getFileContents = async path => {
try {
await access(path);
const contents = await readFile(path);
return contents;
} catch {
return null;
}
};

module.exports = { dirHasAtoms, getAtomsHash, compileStyles };
13 changes: 12 additions & 1 deletion tooling/cli/lib/helpers/filepaths.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { readdir, stat } = require('fs/promises');
const path = require('path');
const { access, readFile } = require('fs/promises');

const getFilePaths = async (dir, filePaths = [], skipNodeModules = true) => {
const newFilePaths = filePaths;
Expand Down Expand Up @@ -36,4 +37,14 @@ const filePathIsValid = filePath => {
);
};

module.exports = { getFilePaths, filePathIsValid };
const getFileContents = async path => {
try {
await access(path);
const contents = await readFile(path);
return contents;
} catch {
return null;
}
};

module.exports = { getFilePaths, filePathIsValid, getFileContents };

0 comments on commit 493b1ba

Please sign in to comment.