Skip to content
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

More TS #301

Merged
merged 33 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cf1ba92
chore: enable `noImplicitAny` on `elm-binary.js`
lishaduck Nov 4, 2024
78f3479
fix: type-level arithmetic!
lishaduck Nov 4, 2024
03479d3
chore: enable `noImplicitAny` on `template-dependencies.js`
lishaduck Nov 4, 2024
70aae3f
chore: add watch-strict script to ease `noImplicitAny` adoption
lishaduck Nov 4, 2024
a5faaca
chore: enable `noImplicitAny` on `result-cache-worker.js`
lishaduck Nov 4, 2024
860a198
refactor: define CacheRequest type
lishaduck Nov 4, 2024
d336723
fix: readAst returns an Ast, not an ElmFile
lishaduck Nov 4, 2024
f02acd1
fix: use unknown over object, which ts now treats as any
lishaduck Nov 4, 2024
834c54a
chore: enable `noImplicitAny` on `result-cache.js`
lishaduck Nov 4, 2024
8b76474
fix: more types
lishaduck Nov 4, 2024
28c23bf
fix: remove redundant annotation
lishaduck Nov 4, 2024
ad4f70a
chore: enable `noImplicitAny` on `review-dependencies.js`
lishaduck Nov 4, 2024
a06b3c7
chore: enable `noImplicitAny` on `project-dependencies.js`
lishaduck Nov 4, 2024
17644f0
chore: enable `noImplicitAny` on `runner.js`
lishaduck Nov 4, 2024
7f6e6a7
chore: enable `noImplicitAny` on `app-wrapper.js`
lishaduck Nov 4, 2024
b2c7e04
chore: enable `noImplicitAny` on `autofix.js`
lishaduck Nov 4, 2024
8acbcea
chore: enable `noImplicitAny` on `remote-template.js`
lishaduck Nov 4, 2024
a6fa97b
chore: enable `noImplicitAny` on `optimize-js.js`
lishaduck Nov 4, 2024
e7c5864
chore: update type imports to use ts extensions
lishaduck Nov 4, 2024
d0f19fc
chore: enable `noImplicitAny` on `build.js`
lishaduck Nov 4, 2024
da322ba
chore: enable `noImplicitAny` on `elm-app-worker.js`
lishaduck Nov 4, 2024
3552ccd
chore: enable `noImplicitAny` on `init.js`
lishaduck Nov 4, 2024
98a0279
chore: enable `noImplicitAny` on `new-package.js`
lishaduck Nov 4, 2024
a64d587
chore: enable `noImplicitAny` on `main.js`
lishaduck Nov 4, 2024
ffe85cc
chore: enable `noImplicitAny` on `module-cache.js`
lishaduck Nov 4, 2024
f68a16d
chore: enable `noImplicitAny` on `run-review.js`
lishaduck Nov 4, 2024
fd34ca9
chore: enable `noImplicitAny` on `watch.js`
lishaduck Nov 4, 2024
505261a
chore: enable `noImplicitAny` on `new-package/`
lishaduck Nov 4, 2024
4e91e56
chore: remove `tsconfig.no-implicit-any.json`
lishaduck Nov 4, 2024
fd4fd3a
chore: enable blanket strict mode
lishaduck Nov 4, 2024
c13b9ff
chore: make eslint depend on tsconfig
lishaduck Nov 4, 2024
d7b4155
chore: add offline testing script
lishaduck Nov 4, 2024
bdaa693
Update maintenance file snapshots
jfmengels Nov 4, 2024
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
5 changes: 1 addition & 4 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,7 @@ I can help set you up with an initial configuration if you run ${chalk.magenta('
*/
async function buildFromGitHubTemplate(options, template) {
Spinner.setText('Fetching template information');
const commit = await RemoteTemplate.getRelevantCommit(
options,
options.template
);
const commit = await RemoteTemplate.getRelevantCommit(options, template);
const reviewElmJson = await RemoteTemplate.getRemoteElmJson(
options,
template,
Expand Down
81 changes: 60 additions & 21 deletions lib/remote-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const TemplateDependencies = require('./template-dependencies');
// GET LATEST INFORMATION ABOUT REPOSITORY

/**
* @param {Options} options
* @param {Template} template
* @returns {Promise<string>}
*/
async function getRelevantCommit(options, template) {
Expand All @@ -36,33 +38,42 @@ async function getRelevantCommit(options, template) {

/**
* @returns {Promise<string>}
* @param {Options} options
* @param {Template} template
*/
async function findDefaultBranch(options, template) {
Debug.log('Fetching default branch');
const body = await makeGitHubApiRequest(
options,
`https://api.github.com/repos/${template.repoName}`,
() => repoNotFoundErrorMessage(template.repoName)
const body = /** @type {{default_branch: string}} */ (
await makeGitHubApiRequest(
options,
`https://api.github.com/repos/${template.repoName}`,
() => repoNotFoundErrorMessage(template.repoName)
)
);
return body.default_branch;
}

/**
* @returns {Promise<string>}
* @param {Options} options
* @param {Template} template
* @param {string} reference
*/
async function getLatestCommitForReference(options, template, reference) {
Debug.log(`Fetching commit ${reference}`);
const body = await makeGitHubApiRequest(
options,
`https://api.github.com/repos/${template.repoName}/commits/${reference}`,
(responseBody) => {
if (responseBody.message === 'Not Found') {
// This error means that the repo itself was not found
return repoNotFoundErrorMessage(template.repoName);
}
const body = /** @type {{sha: string}} */ (
await makeGitHubApiRequest(
options,
`https://api.github.com/repos/${template.repoName}/commits/${reference}`,
(responseBody) => {
if (responseBody.message === 'Not Found') {
// This error means that the repo itself was not found
return repoNotFoundErrorMessage(template.repoName);
}

return commitNotFoundErrorMessage(template.repoName, reference);
}
return commitNotFoundErrorMessage(template.repoName, reference);
}
)
);
return body.sha;
}
Expand Down Expand Up @@ -108,6 +119,10 @@ async function getRemoteElmJson(
return TemplateDependencies.update(options, elmJson);
}

/**
* @param {Template} template
* @param {string} commit
*/
Copy link
Owner

Choose a reason for hiding this comment

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

Should this not a return type annotation? (Same of other functions in this file)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They should, but I used the IDE inferer to get started, which skips them, unfortunately, and I didn't realize it until I was done.
TS infers them though, so I think it can be left for a follow-up.

async function downloadTemplateElmJson(template, commit) {
const {repoName, pathToFolder} = template;
const pathToFolderAsUrl = pathToFolder ? `/${pathToFolder}` : '';
Expand Down Expand Up @@ -165,6 +180,9 @@ elm-review in your project:
elm-review init --template <some-configuration>`
};

/**
* @param {string} repoName
*/
function repoNotFoundErrorMessage(repoName) {
return {
title: 'REPOSITORY NOT FOUND',
Expand All @@ -176,6 +194,10 @@ with private ones at the moment.`
};
}

/**
* @param {unknown} repoName
* @param {unknown} reference
*/
function commitNotFoundErrorMessage(repoName, reference) {
return {
title: 'BRANCH OR COMMIT NOT FOUND',
Expand All @@ -189,6 +211,13 @@ Check the spelling and make sure it has been pushed.`

// DOWNLOAD TEMPLATE FILES

/**
* @param {Options} options
* @param {Template} template
* @param {string} commit
* @param {string} basePath
* @param {ApplicationElmJson} reviewElmJson
*/
async function downloadSourceDirectories(
options,
template,
Expand All @@ -209,6 +238,13 @@ async function downloadSourceDirectories(
);
}

/**
* @param {Options} options
* @param {Template} template
* @param {string} commit
* @param {string} basePath
* @param {string} directory
*/
async function downloadDirectory(
options,
template,
Expand All @@ -220,12 +256,15 @@ async function downloadDirectory(
const destinationDirectory = directory.split('..').join('parent');

await FS.mkdirp(path.join(basePath, destinationDirectory));
const fileListing = await makeGitHubApiRequest(
options,
`https://api.github.com/repos/${repoName}/contents${pathToFolder}/${directory}?ref=${commit}`
.split('//')
.join('/')
);
const fileListing =
/** @type {({ type: string; name: string; download_url: string; })[]} */ (
await makeGitHubApiRequest(
options,
`https://api.github.com/repos/${repoName}/contents${pathToFolder}/${directory}?ref=${commit}`
.split('//')
.join('/')
)
);

await Promise.all(
fileListing.map(async (fileOrDir) => {
Expand Down Expand Up @@ -283,7 +322,7 @@ async function downloadFile(url, dest) {
* @param {Options} options
* @param {string} url
* @param {((arg: JsonResponse) => {title: string, message: string})} [handleNotFound]
* @returns {Promise<object>}
* @returns {Promise<unknown>}
*/
async function makeGitHubApiRequest(options, url, handleNotFound) {
/** @type {OptionsOfJSONResponseBody}} */
Expand Down
1 change: 1 addition & 0 deletions tsconfig.no-implicit-any.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"lib/project-dependencies.js",
"lib/project-json-files.js",
"lib/promisify-port.js",
"lib/remote-template.js",
"lib/report.js",
"lib/result-cache.js",
"lib/result-cache-json.js",
Expand Down