-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: migrate to ESM #437
Conversation
BREAKING CHANGES: ESM and node 18 minimum
This issue has been linked to a new work item: W-14294408 |
This seems to contain my PR, but without reference or attribution. Could you cherry-pick the commit instead? |
Fixes #279 This can speed up execution, reduce unnecessary network requests, and clean up log files.
src/hooks/init/check-update.ts
Outdated
const readJSON = async <T>(file: string): Promise<{contents: T; stat: Stats}> => ({ | ||
contents: JSON.parse(await readFile(file, 'utf8')), | ||
stat: await stat(file), | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perf: promise.all the 2 tasks, then return them in the object.
[small perf nit, but this runs before every command]
src/hooks/init/check-update.ts
Outdated
authorization = '', | ||
} = (config.pjson.oclif as any)['warn-if-update-available'] || {} | ||
timeoutInDays = 60, | ||
} = config.pjson.oclif['warn-if-update-available'] || {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} = config.pjson.oclif['warn-if-update-available'] || {} | |
} = config.pjson.oclif['warn-if-update-available'] ?? {} |
src/hooks/init/check-update.ts
Outdated
timeoutInDays = 60, | ||
} = config.pjson.oclif['warn-if-update-available'] || {} | ||
|
||
const warningFrequency = (config.scopedEnvVar('NEW_VERSION_CHECK_FREQ') as number | undefined) ?? frequency |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TS lets you make ASsertions about what's in somebody's env (lying to the compiler), but that doesn't do any checks or conversions in the real world.
example: if their frequency was ham
and the unit was sandwiches
then convertToMs
is going to have unhandled errors.
Also, envs would be a string anyway, so you'll need to parseInt warningFrequency
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's have some tests for handling of these envs.
src/hooks/init/check-update.ts
Outdated
|
||
// If the file was modified before the timeout, don't show the warning | ||
if ( | ||
warningFrequency && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
confirming that if warningFrequency
is set to 0 you'd short-circuit the rest of the conditional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think that makes sense. Frequency of 0
causes the default behavior of seeing the warning on every command execution.
I guess the alternative is that a frequency of 0
turns the warning off and you never see it again? We already have an env var for that so I don't see a reason to duplicate it
src/hooks/init/check-update.ts
Outdated
const {gt} = await import('semver') | ||
if (distTags[tag] && gt(distTags[tag].split('-')[0], config.version.split('-')[0])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: since you've already got distTags, you could check that and avoid the semver import if it weren't needed
src/hooks/init/check-update.ts
Outdated
|
||
// Update the modified time (mtime) of the version file so that we can track the last time we | ||
// showed the warning. This makes it possible to respect the frequency and frequencyUnit options. | ||
await utimes(file, new Date(), new Date()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the utimes
op to wait on the lodash import and warning to happen first? or could it be run in parallel with the import?
].join('/') | ||
const headers = authorization ? {authorization} : {} | ||
await mkdir(dirname(file), {recursive: true}) | ||
await writeFile(file, JSON.stringify({current: version, headers})) // touch file with current version to prevent multiple updates | ||
const {body} = await HTTP.get<any>(url, {headers, timeout: 5000}) | ||
await writeFile(file, JSON.stringify({...body['dist-tags'], current: version, authorization})) | ||
const {body} = await HTTP.get<{'dist-tags': string[]}>(url, {headers, timeout: 5000}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the http.get need to wait on the mkdir/writeFile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure - and I don't want to take the risk of changing it, especially since this script is run in a unref'd spawned process. So any performance improvements in there will never be noticed by anyone
} | ||
|
||
export function semverGreaterThan(a: string, b: string): boolean { | ||
return a.localeCompare(b, undefined, {numeric: true, sensitivity: 'base'}) > 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool. I saw the semver removal and wondered what was going to happen here.
QA Notes: created switched to 2.3.8 via udpate ok, let's just go with and update to 2.5.8 to see if it knows about updates beyond that. ran org list a few times, not update suggestions. deletes the version file, run command again. testing envs:
🔴 potential design flaw:
|
QA notes ( update to 2.8.3 delete file delete both files SF_NEW_VERSION_CHECK_FREQ=30 SF_NEW_VERSION_CHECK_FREQ_UNIT=seconds sf org list set latest to 2.0.0 set latest to 2.8.3 (the current version) set latest to 2.8.4 (the very next version) SF_NEW_VERSION_CHECK_TAG=nigtly SF_FORCE_VERSION_CACHE_UPDATE=true sf org list SF_NEW_VERSION_CHECK_TAG=nigtly sf org list (bad tag) SF_NEW_VERSION_CHECK_TAG=nightly sf org list |
BREAKING CHANGES: ESM and node 18 minimum
frequency
andfrequencyUnit
settings to make warning frequency configurable<CLI>_SKIP_NEW_VERSION_CHECK
environment variable to disable version checkCloses #400
Release Notes
Added these env vars:
<CLI>_SKIP_NEW_VERSION_CHECK
: Skip the version update check<CLI>_NEW_VERSION_CHECK_FREQ
: the frequency at which the version warning should be shown<CLI>_NEW_VERSION_CHECK_FREQ_UNIT
: the unit of time used to set the frequency. Defaults to minutes (can bedays
,hours
,minutes
,seconds
,milliseconds
)