Skip to content

Commit

Permalink
feat(tools): add npm-stats script, migrate files()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 5, 2020
1 parent accacf9 commit 6542318
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
22 changes: 2 additions & 20 deletions tools/src/build-search-index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
import { ArraySet, Trie } from "@thi.ng/associative";
// @ts-ignore
import { serialize } from "@ygoe/msgpack";
import { readdirSync, readFileSync, statSync, writeFileSync } from "fs";
import { execSync } from "child_process";
import { readJSON } from "./io";
import { readFileSync, writeFileSync } from "fs";
import { files, readJSON } from "./io";
import { build, defEncoder } from "./search";

const RE_DOC_START = /^\s*\/\*\*$/;
const RE_DOC_END = /^\s+\*\/$/;
const RE_DOC_CODE = /^\s+\* \`\`\`/;
const RE_SYM = /^export (type|interface|class|const|function|enum) (\w+)/;

/**
* Recursively reads given directory and returns file names matching
* given extension.
*
* @param {*} dir
* @param {*} ext
*/
function* files(dir: string, ext: string): IterableIterator<string> {
for (let f of readdirSync(dir)) {
const curr = dir + "/" + f;
if (f.endsWith(ext)) {
yield curr;
} else if (statSync(curr).isDirectory()) {
yield* files(curr, ext);
}
}
}

class EquivTrie<T> extends Trie<string, T> {
makeChild() {
return new EquivTrie<T>();
Expand Down
20 changes: 19 additions & 1 deletion tools/src/io.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { readFileSync } from "fs";
import { readdirSync, readFileSync, statSync } from "fs";

export const readJSON = (path: string) => JSON.parse(<any>readFileSync(path));

export const readText = (path: string) => readFileSync(path).toString();

/**
* Recursively reads given directory and returns file names matching
* given extension.
*
* @param {*} dir
* @param {*} ext
*/
export function* files(dir: string, ext: string): IterableIterator<string> {
for (let f of readdirSync(dir)) {
const curr = dir + "/" + f;
if (f.endsWith(ext)) {
yield curr;
} else if (statSync(curr).isDirectory()) {
yield* files(curr, ext);
}
}
}
31 changes: 31 additions & 0 deletions tools/src/npm-stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { readdirSync, writeFileSync } from "fs";
import { request } from "https";
import { delayed } from "@thi.ng/compose";

(async () => {
const stats: any = {};
for (let f of readdirSync("packages")) {
console.log(f);
const req = request(
`https://api.npmjs.org/downloads/point/last-month/@thi.ng/${f}`,
(res) => {
console.log("status:", res.statusCode);
res.setEncoding("utf8");
res.on("data", (d) => {
stats[f] = JSON.parse(d.toString()).downloads || 0;
});
}
);

req.on("error", (e) => {
console.error(e);
});
req.end();

await delayed(null, 1000);
}

await delayed(null, 5000);
writeFileSync("stats.json", JSON.stringify(stats));
console.log("done");
})();

0 comments on commit 6542318

Please sign in to comment.