@nodearch/cli
Version:
nodearch cli
90 lines (89 loc) • 3.77 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Service } from '@nodearch/core';
import axios from 'axios';
import path from 'path';
import fs from 'node:fs/promises';
import { createWriteStream } from 'node:fs';
let GitHubService = class GitHubService {
constructor() {
this.githubRepoUrl = 'https://api.github.com/repos/nodearch/nodearch/contents/';
}
async listTemplates() {
const dirsInfo = await this.getDirPathInfo('templates');
return dirsInfo.map((x) => x.name);
}
async downloadTemplate(downloadPath, templateName) {
const filesInfo = await this.getPathInfo('templates/' + templateName);
await Promise.all(filesInfo.map(async (fileInfo) => {
const filePath = fileInfo.path.split('/').slice(2).join('/');
await fs.mkdir(path.join(downloadPath, path.parse(filePath).dir), { recursive: true });
await this.downloadFile(path.join(downloadPath, filePath), fileInfo.download_url);
}));
await this.removeWorkspaceFromPackageJson(path.join(downloadPath, 'package.json'));
}
async getDirPathInfo(repoPath) {
const { data } = await axios({
method: 'get',
url: this.githubRepoUrl + repoPath,
responseType: 'json'
});
return data.filter((x) => x.type === 'dir');
}
async getPathInfo(repoPath) {
let pathsInfo = [];
const { data } = await axios({
method: 'get',
url: this.githubRepoUrl + repoPath,
responseType: 'json'
});
const files = data.filter((x) => x.type === 'file');
pathsInfo.push(...files);
await Promise.all(data
.filter((x) => x.type === 'dir')
.map(async (x) => {
const files = await this.getPathInfo(x.path);
pathsInfo.push(...files);
}));
return pathsInfo;
}
async downloadFile(filePath, downloadUrl) {
const response = await axios({
method: 'get',
url: downloadUrl,
responseType: 'stream'
});
await new Promise((resolve, reject) => {
const writeStream = createWriteStream(filePath);
response.data.pipe(writeStream);
writeStream.on('error', err => reject(err));
writeStream.on('finish', () => resolve(true));
});
}
async removeWorkspaceFromPackageJson(filePath) {
const packageJson = JSON.parse(await fs.readFile(filePath, 'utf8'));
for (const depType of ['dependencies', 'devDependencies']) {
const deps = packageJson[depType];
if (!deps) {
continue;
}
for (const depName in deps) {
if (deps.hasOwnProperty(depName)) {
const depVersion = deps[depName];
if (depVersion.startsWith('workspace:')) {
deps[depName] = depVersion.substring('workspace:'.length);
}
}
}
}
await fs.writeFile(filePath, JSON.stringify(packageJson, null, 2));
}
};
GitHubService = __decorate([
Service()
], GitHubService);
export { GitHubService };