Skip to content

Commit

Permalink
Merge pull request #26 from kkiyama117/develop
Browse files Browse the repository at this point in the history
Download schemas concurrently
  • Loading branch information
kkiyama117 authored Feb 23, 2022
2 parents 4b01006 + 529331b commit 21c9262
Show file tree
Hide file tree
Showing 4 changed files with 1,183 additions and 1,019 deletions.
132 changes: 121 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"url": "https://github.com/kkiyama117/coc-toml/issues"
},
"engines": {
"coc": "^0.0.77"
"coc": "^0.0.80"
},
"scripts": {
"build": "yarn --ignore-engines rollup --silent -c rollup.config.js",
Expand All @@ -44,13 +44,13 @@
"@rollup/plugin-node-resolve": "^13.1.3",
"@rollup/plugin-wasm": "^5.1.2",
"@types/node": "^17.0.19",
"@types/node-fetch": "^3.0.3",
"@types/node-fetch": "^2.6.1",
"coc.nvim": "0.0.80",
"ini": ">=1.3.6",
"ini": ">=2.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
"rollup": "^2.67.3",
"rollup": "^2.68.0",
"rollup-plugin-typescript2": "^0.31.2",
"ts-node": "^10.5.0",
"typescript": "^4.5.5",
Expand Down
70 changes: 44 additions & 26 deletions src/commands/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export function downloadSchemas(
statusItem.text = 'Fetching schema index';

try {
const index: any = await fetch(config.indexUrl).then((res) => res.json());
const index: TaploSchemas = await fetch(config.indexUrl).then((res) =>
res.json()
);

if (!index?.schemas) {
window.showMessage('invalid index JSON');
Expand All @@ -61,42 +63,58 @@ export function downloadSchemas(
);

const schemaCount: number = index.schemas?.length ?? 0;
let schemaDone: number = 0;

const schemasPath = path.join(ctx.storagePath, 'schemas');

await fs.promises.mkdir(schemasPath, { recursive: true });

// FIXME: maybe do this concurrently?
for (let i = 0; i < schemaCount; i++) {
const schemaMeta = index.schemas[i];
try {
const schema = await fetch(schemaMeta.url).then((res) => res.json());

await fs.promises.writeFile(
path.join(schemasPath, `${schemaMeta.urlHash}.json`),
JSON.stringify({
url: schemaMeta.url,
schema: schema,
})
const promises: Promise<boolean>[] = (index.schemas as TaploSchema[]).map(
async (schemaMeta) => {
try {
const schema: TaploSchema = await fetch(schemaMeta.url).then(
(res) => res.json()
);

await fs.promises.writeFile(
path.join(schemasPath, `${schemaMeta.urlHash}.json`),
JSON.stringify({
url: schemaMeta.url,
schema: schema,
})
);
} catch (e) {
// TODO: handle this better.
console.warn(e);
return false;
}
statusItem.text = `Downloaded schema (${schemaMeta.title}).`;
window.showMessage(
`Updated ${schemaMeta.title} schema from the repository.`
);

schemaDone += 1;
} catch (e) {
// TODO: handle this better.
console.warn(e);
return true;
}

statusItem.text = `Downloaded schema (${i}/${schemaCount}).`;

window.showMessage(
`Updated ${schemaDone}/${schemaCount} schemas from the repository.`
);
}
);
const sucessed = await Promise.all(promises);
window.showMessage(
`Updated ${
sucessed.filter(Boolean).length
}/${schemaCount} schemas from the repository.`
);
statusItem.hide();
} catch (e) {
console.error(e);
window.showMessage('Failed to download schemas.');
}
};
}

type TaploSchemas = { schemas: TaploSchema[] };
type TaploSchema = {
title: string;
description: string;
updated: string;
url: string;
urlHash: string;
authors: string[];
patterns: string[];
};
Loading

0 comments on commit 21c9262

Please sign in to comment.