import { lstatSync, readdirSync } from 'fs'; const { join } = require('path'); import * as gitParser from 'git-url-parse'; import * as fs from 'fs'; import { logger } from '@cdm-logger/server'; import { IProjectSourceType as ISourceType, IProjectSourceProviders as ISourceProviders, IProject_Input as IProject, } from '@adminide-stack/core'; import { IGitService } from '@git-stack/core'; import { ICloneOptions, IGitSshSettings } from '@git-stack/core'; import { isEqual, differenceWith, get } from 'lodash'; import { IWorkspaceContextService } from '@workbench-stack/core'; import { URI } from '@vscode/monaco-editor/esm/vs/base/common/uri'; export const createProjects = async (projectConfig, rootPath, service: IGitService, workspaceContextService: IWorkspaceContextService, globalEnv: ICloneOptions & IGitSshSettings) => { return await projectConfig.projects.map(async (prj: IProject) => { switch (prj.source.type) { case ISourceType.Blank: const dirName = `${rootPath}/${prj.name}`; //create blank directory if (!fs.existsSync(dirName)) { fs.mkdirSync(dirName); } break; case ISourceType.Git: const gitData = gitParser(prj.source.location); const repoPath = `${rootPath}/${gitData.name}`; if (!fs.existsSync(repoPath)) { // if token available // await service.saveUser(prj.source.token, '', 'someId', prj.source.providers); // nee work here const providerName: ISourceProviders = prj.source.providers; const providerData = get(projectConfig.userParams, providerName); if (providerData) { const login = providerData ? providerData.username : globalEnv.login; const endpoint = get(process.env, `${providerName}-TOKEN`); const email = get(providerData, 'email') || globalEnv.email; const localSshKeyPath = get(providerData, 'sshKeyPath'); const gitSSHEnabled = !!localSshKeyPath || globalEnv.gitSSHEnabled; console.log('--GITSSHENABLED', gitSSHEnabled); const res = await service.clone(prj.source.location, rootPath, { login, endpoint, email, gitSSHEnabled, gitSSHPrivateKeyPath: localSshKeyPath || globalEnv.gitSSHPrivateKeyPath, }); if (res.exitCode === 0) { const path = URI.file(res.stderr.split("'")[1]); await service.openRepository(res.stderr.split("'")[1]); await workspaceContextService.addFolders([{ uri: path, }]); } if (res.exitCode) { logger.warn('Clone failed due to [%]', res); // try running with https url const httpsUrl = get(prj.source, 'parameters.httpsUrl'); if (httpsUrl) { logger.debug('trying again with [%s]', httpsUrl); const resHttps = await service.clone(httpsUrl, rootPath, { login, endpoint, gitSSHEnabled: false }); if (resHttps.exitCode) { console.error('exitCode', resHttps); } } else { console.error('exitCode', res); } // notify user to add the private key into the git repo. } } } break; case ISourceType.Zip: console.warn('Not yet implemented Zip'); break; default: break; } }); }; export const updateProjects = async (rootPath, projectConfig, service: IGitService, workspaceContextService: IWorkspaceContextService, env) => { const oldFolders = getAllFolders(rootPath); try { const newFolders = projectConfig.projects.map(el => { const gitData = gitParser(el.source.location); return `${rootPath}/${gitData.name}`; }); const diff = differenceWith(oldFolders, newFolders, isEqual); await createProjects(projectConfig, rootPath, service, workspaceContextService, env); // skipping removing of the files // diff.map(el => { // rimraf(el, function () { console.log('done ' + el); }); // }); } catch (e) { console.error('e', e); } }; export const getAllFolders = (rootPath) => { const isDirectory = source => lstatSync(source).isDirectory(); const getDirectories = readdirSync(rootPath).map(name => join(rootPath, name)).filter(isDirectory); return getDirectories; };