2024-10-16 08:43:51 +00:00
|
|
|
import assert from 'assert';
|
2024-10-18 12:37:01 +00:00
|
|
|
import debug from 'debug';
|
2024-01-16 09:36:35 +00:00
|
|
|
import fs from 'fs-extra';
|
2024-10-18 12:37:01 +00:00
|
|
|
import { Octokit } from 'octokit';
|
2024-01-16 09:36:35 +00:00
|
|
|
import path from 'path';
|
|
|
|
import toml from 'toml';
|
2024-02-21 10:04:33 +00:00
|
|
|
import { DataSource, DeepPartial, EntityTarget, ObjectLiteral } from 'typeorm';
|
2024-10-16 08:43:51 +00:00
|
|
|
|
2024-04-21 23:02:42 +00:00
|
|
|
import { Config } from './config';
|
|
|
|
import { DEFAULT_CONFIG_FILE_PATH } from './constants';
|
2024-10-18 12:37:01 +00:00
|
|
|
import { PackageJSON } from './types';
|
2024-01-16 09:36:35 +00:00
|
|
|
|
|
|
|
const log = debug('snowball:utils');
|
|
|
|
|
2024-04-21 23:02:42 +00:00
|
|
|
export async function getConfig() {
|
|
|
|
// TODO: get config path using cli
|
|
|
|
return await _getConfig<Config>(DEFAULT_CONFIG_FILE_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
const _getConfig = async <ConfigType>(
|
|
|
|
configFile: string,
|
2024-01-16 09:36:35 +00:00
|
|
|
): Promise<ConfigType> => {
|
|
|
|
const configFilePath = path.resolve(configFile);
|
|
|
|
const fileExists = await fs.pathExists(configFilePath);
|
|
|
|
if (!fileExists) {
|
|
|
|
throw new Error(`Config file not found: ${configFilePath}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = toml.parse(await fs.readFile(configFilePath, 'utf8'));
|
|
|
|
log('config', JSON.stringify(config, null, 2));
|
|
|
|
|
|
|
|
return config;
|
|
|
|
};
|
2024-02-21 10:04:33 +00:00
|
|
|
|
|
|
|
export const checkFileExists = async (filePath: string): Promise<boolean> => {
|
|
|
|
try {
|
|
|
|
await fs.access(filePath, fs.constants.F_OK);
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
log(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getEntities = async (filePath: string): Promise<any> => {
|
|
|
|
const entitiesData = await fs.readFile(filePath, 'utf-8');
|
|
|
|
const entities = JSON.parse(entitiesData);
|
|
|
|
return entities;
|
|
|
|
};
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
export const loadAndSaveData = async <Entity extends ObjectLiteral>(
|
|
|
|
entityType: EntityTarget<Entity>,
|
|
|
|
dataSource: DataSource,
|
|
|
|
entities: any,
|
2024-04-21 23:02:42 +00:00
|
|
|
relations?: any | undefined,
|
2024-02-22 05:45:17 +00:00
|
|
|
): Promise<Entity[]> => {
|
2024-02-21 10:04:33 +00:00
|
|
|
const entityRepository = dataSource.getRepository(entityType);
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
const savedEntity: Entity[] = [];
|
2024-02-21 10:04:33 +00:00
|
|
|
|
|
|
|
for (const entityData of entities) {
|
|
|
|
let entity = entityRepository.create(entityData as DeepPartial<Entity>);
|
|
|
|
|
|
|
|
if (relations) {
|
|
|
|
for (const field in relations) {
|
|
|
|
const valueIndex = String(field + 'Index');
|
|
|
|
|
|
|
|
entity = {
|
|
|
|
...entity,
|
2024-04-21 23:02:42 +00:00
|
|
|
[field]: relations[field][entityData[valueIndex]],
|
2024-02-21 10:04:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const dbEntity = await entityRepository.save(entity);
|
|
|
|
savedEntity.push(dbEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
return savedEntity;
|
|
|
|
};
|
2024-02-29 13:03:34 +00:00
|
|
|
|
2024-04-21 23:02:42 +00:00
|
|
|
export const sleep = async (ms: number): Promise<void> =>
|
|
|
|
new Promise((resolve) => setTimeout(resolve, ms));
|
2024-10-18 12:37:01 +00:00
|
|
|
|
|
|
|
export const getRepoDetails = async (
|
|
|
|
octokit: Octokit,
|
|
|
|
repository: string,
|
|
|
|
commitHash: string | undefined,
|
|
|
|
): Promise<{
|
|
|
|
repo: string;
|
|
|
|
packageJSON: PackageJSON;
|
|
|
|
repoUrl: string;
|
|
|
|
}> => {
|
|
|
|
const [owner, repo] = repository.split('/');
|
|
|
|
const { data: packageJSONData } = await octokit.rest.repos.getContent({
|
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
path: 'package.json',
|
|
|
|
ref: commitHash,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!packageJSONData) {
|
|
|
|
throw new Error('Package.json file not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!Array.isArray(packageJSONData) && packageJSONData.type === 'file');
|
|
|
|
const packageJSON: PackageJSON = JSON.parse(atob(packageJSONData.content));
|
|
|
|
|
|
|
|
assert(packageJSON.name, "name field doesn't exist in package.json");
|
|
|
|
|
|
|
|
const repoUrl = (
|
|
|
|
await octokit.rest.repos.get({
|
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
})
|
|
|
|
).data.html_url;
|
|
|
|
|
|
|
|
return {
|
|
|
|
repo,
|
|
|
|
packageJSON,
|
|
|
|
repoUrl
|
|
|
|
};
|
|
|
|
}
|