You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

83 lines
2.1 KiB

import {Command} from 'commander';
import path from 'path';
import fetch from "node-fetch";
import fs from "fs";
import http from "https";
import unzipper from "unzipper";
import {createTechnicalFiles, defaultGitRepo} from "./generators/block/index.js";
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
init();
function init() {
const program = new Command();
program
.name('create-block')
.description('AXE-WEB Platform Blocks');
program.command('pull')
.argument('<string>', 'Provide a full name of required block, for example: @axe-web/hero-block')
.action(async (blockName, options) => {
console.log('Block to download:', blockName)
await getBlockSourceFiles(blockName);
});
program.parse();
}
async function getBlockSourceFiles(blockName) {
const blocksRegistry = 'http://localhost:3020';
const queryString = new URLSearchParams();
queryString.append('blockName', blockName);
const response = await fetch(`${blocksRegistry}?${queryString.toString()}`);
const responseData = await response.json();
if (!responseData) {
return;
}
const zipFile = await downloadFile(responseData.downloadUrl, responseData.name);
const file = await fs.createReadStream(zipFile)
.pipe(unzipper.Extract({path: `blocks/${responseData.name}/src`}))
.promise();
// Remove downloaded file.
try {
await fs.promises.access(zipFile, fs.constants.W_OK);
await fs.promises.unlink(zipFile);
} catch (e) {
console.log(e)
}
await createTechnicalFiles({
name: responseData.name,
blockFilename: responseData.name,
devToolSource: defaultGitRepo
}, __dirname);
}
async function downloadFile(url, blockName) {
const file = fs.createWriteStream(blockName + '.zip');
return new Promise((resolve, reject) => {
const request = http.get(url, function (response) {
response.pipe(file);
// after download completed close filestream
file.on("finish", () => {
file.close();
resolve(`./${blockName}.zip`);
});
});
})
}