-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools): add npm-stats script, migrate
files()
- Loading branch information
1 parent
accacf9
commit 6542318
Showing
3 changed files
with
52 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
})(); |