Skip to content

Commit

Permalink
integrated Downlod CLI option
Browse files Browse the repository at this point in the history
  • Loading branch information
drom committed Jun 3, 2024
1 parent 1478670 commit 2e021f8
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
package-lock.json
index.js
*.tgz
pdfs
20 changes: 18 additions & 2 deletions bin/duh-bus.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#!/usr/bin/env node
'use strict';

const process = require('process');
const path = require('path');
const { program } = require('commander');
const dl = require('../lib/amba-pdf-dl.js');

const specPath = path.resolve(__dirname, '../specs');
const main = async () => {
program
.option('--amba-pdf-dl <folder>', 'download AMBA spec PDFs')
.parse(process.argv);

const opts = program.opts();
if (opts.ambaPdfDl) {
await dl(opts.ambaPdfDl);
return;
}
const specPath = path.resolve(__dirname, '../specs');
console.log(specPath);
};

main();

console.log(specPath);
/* eslint no-console:0 */
38 changes: 38 additions & 0 deletions lib/amba-pdf-dl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const { createWriteStream, existsSync } = require('fs');
const { mkdir } = require("fs/promises");
const path = require('path');
const { Readable } = require('stream');
const { finished } = require('stream/promises');

const specs = require('./amba-specs.js');

const dl = async (ambaSpecsDl) => {
const dlFolderPath = path.resolve('.', ambaSpecsDl);
if (!existsSync(dlFolderPath)) {
await mkdir(dlFolderPath);
}
for (const spec of specs) {
const url = `https://documentation-service.arm.com/static/${spec.id}?token=`;
const res = await fetch(url);
const headers = res.headers.entries();
let fileName;
for (const [key, val] of headers) {
if (key === 'content-disposition') {
const m = val.match(/^attachment; filename=(?<baseName>[\w_-]+).pdf$/);
fileName = m.groups.baseName + '.pdf';
}
}
const fullFileName = path.resolve('.', dlFolderPath, fileName);
if (!existsSync(fullFileName)) {
const fileStream = createWriteStream(fullFileName, { flags: 'wx'});
await finished(Readable.fromWeb(res.body).pipe(fileStream));
console.log(fileName + ' - downloded');
} else {
console.log(fileName + ' - exists');
}
}
};

module.exports = dl;
55 changes: 55 additions & 0 deletions lib/amba-specs.js

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"nyc": "^15.1.0"
},
"dependencies": {
"commander": "^12.1.0",
"json5": "^2.2.3"
}
}

0 comments on commit 2e021f8

Please sign in to comment.