Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,47 @@ const {
PHANPY_APP_ERROR_LOGGING: ERROR_LOGGING,
PHANPY_REFERRER_POLICY: REFERRER_POLICY,
PHANPY_DISALLOW_ROBOTS: DISALLOW_ROBOTS,
PHANPY_COMMIT_HASH: COMMIT_HASH,
PHANPY_COMMIT_TIME: COMMIT_TIME,
PHANPY_NOW: NOW,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems not needed if there's PHANPY_COMMIT_TIME?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used for the build timestamp in version.json. It's not used elsewhere as far as I can see.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I guess it's reasonable to say that if you provide a PHANPY_COMMIT_TIME, then we can set now to that too, and use that for the buildTime in version.json as well. Would you prefer that?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically commit time != build time. commitTime = now; was a workaround as there's no available specific time for a commit. buildTime in version.json is also not used anywhere, so it's safe for it to be now, for now.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me, and it's what I intended. PHANPY_COMMIT_TIME provides an override for the commit time, and PHANPY_NOW provides an override for the build time.

PHANPY_DEV,
} = loadEnv('production', process.cwd(), allowedEnvPrefixes);

const now = new Date();
const now = NOW ? new Date(NOW) : new Date();
let commitHash;
let commitTime;
let fakeCommitHash = false;
try {
const gitResult = execSync('git log -1 --format="%h %cI"').toString().trim();
const [hash, time] = gitResult.split(' ');
commitHash = hash;
commitTime = new Date(time);
} catch (error) {
// If error, means git is not installed or not a git repo (could be downloaded instead of git cloned)
// Fallback to random hash which should be different on every build run 🤞
commitHash = uid();
commitTime = now;
fakeCommitHash = true;
if (COMMIT_HASH && COMMIT_TIME) {
console.log('Using provided commit hash and timestamp');
commitHash = COMMIT_HASH;
commitTime = new Date(COMMIT_TIME);
} else {
try {
console.log('Fetching commit hash and timestamp from Git');
const gitResult = execSync('git log -1 --format="%h %cI"')
.toString()
.trim();
const [hash, time] = gitResult.split(' ');
commitHash = hash;
commitTime = new Date(time);
} catch (error) {
// If error, means git is not installed or not a git repo (could be downloaded instead of git cloned)
// Fallback to random hash which should be different on every build run 🤞
console.log(
'Falling back to randomly-generated commit hash, and current timestamp',
);
commitHash = uid();
commitTime = now;
fakeCommitHash = true;
}
}
console.log(
[
` commit hash: ${commitHash}`,
` commit time: ${commitTime.toISOString()}`,
` build time: ${now.toISOString()}`,
].join('\n'),
);

let rollbarCode = fs.readFileSync(resolve(__dirname, './rollbar.js'), 'utf-8');
rollbarCode = rollbarCode.replace('__PHANPY_COMMIT_HASH__', `'${commitHash}'`);
Expand Down
Loading