Skip to content

Commit

Permalink
use extension api, add linter and formatter
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Mar 14, 2023
1 parent fc80969 commit 82860ce
Show file tree
Hide file tree
Showing 14 changed files with 1,023 additions and 90 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# EditorConfig is awesome: http://EditorConfig.org

# https://github.com/jokeyrhyme/standard-editorconfig

# top-most EditorConfig file
root = true

# defaults
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_size = 2
indent_style = space

[*.md]
trim_trailing_whitespace = false
55 changes: 55 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"root": true,
"env": {
"es2021": true,
"node": true,
"browser": false
},
"extends": [
"eslint:recommended",
/** @see https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#recommended-configs */
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"ignorePatterns": [
"node_modules/**",
"**/dist/**",
"builtin/**"
],
"rules": {
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/no-explicit-any": "error",

/**
* Having a semicolon helps the optimizer interpret your code correctly.
* This avoids rare errors in optimized code.
* @see https://twitter.com/alex_kozack/status/1364210394328408066
*/
"semi": [
"error",
"always"
],
/**
* This will make the history of changes in the hit a little cleaner
*/
"comma-dangle": [
"warn",
"always-multiline"
],
/**
* Just for beauty
*/
"quotes": [
"warn", "single"
]
}
}
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"bracketSameLine": true,
"singleQuote": true,
"arrowParens": "avoid",
"printWidth": 120,
"trailingComma": "all"
}

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,31 @@
"scripts": {
"build": "rollup --bundleConfigAsCjs --config rollup.config.js --compact --environment BUILD:production && node ./scripts/build.js",
"watch": "rollup --bundleConfigAsCjs --config rollup.config.js -w",
"lint:check": "eslint . --ext js,ts",
"lint:fix": "eslint . --fix --ext js,ts",
"format:check": "prettier --check src/**",
"format:fix": "prettier --write src/**",
"desk:build": "node ./scripts/run.mjs build",
"desk:prepare": "node ./scripts/run.mjs prepare",
"desk:run": "node ./scripts/run.mjs run",
"desk:clean": "node ./scripts/run.mjs clean"
"desk:run": "node ./scripts/run.mjs run"
},
"dependencies": {},
"devDependencies": {
"7zip-min": "^1.4.4",
"@podman-desktop/api": "next",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^11.0.0",
"@types/node": "^18.14.6",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"compare-versions": "^6.0.0-rc.1",
"eslint": "^8.36.0",
"got": "^12.5.3",
"hasha": "^5.2.2",
"mkdirp": "^2.1.3",
"prettier": "^2.8.4",
"rollup": "^3.18.0",
"ts-node": "^10.9.1",
"tslib": "^2.5.0",
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { nodeResolve } from '@rollup/plugin-node-resolve';
export default {
input: {
'extension': 'src/extension.ts',
'thread': 'node_modules/hasha/thread.js'
'thread': 'node_modules/hasha/thread.js',
} ,
output: {
dir: 'dist',
Expand All @@ -32,7 +32,7 @@ export default {
'node:fs',
'node:child_process',
'node:crypto',
'node:worker_threads'
'node:worker_threads',
],
plugins: [
typescript(),
Expand Down
24 changes: 5 additions & 19 deletions scripts/run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ async function exec(command, args, options) {

proc.on('error', () => {
reject();
})
});
});
}

async function checkAndCloneDesktopRepo() {
if(!fs.existsSync(desktopPath)) {
console.log('Cloning podman-desktop repository...');
await exec('git', ['clone', 'https://github.com/containers/podman-desktop.git'], {cwd: path.join(__dirname, '..', '..')})
await exec('git', ['clone', 'https://github.com/containers/podman-desktop.git'], {cwd: path.join(__dirname, '..', '..')});
} else {
console.log('desktop repo already exist...');
}
Expand All @@ -72,18 +72,6 @@ function removeCrcExt() {
}
}

async function linkApi() {
console.log('Linking @podman-desktop/api...');
await exec('yarn',['link'], {cwd: path.join(desktopPath, 'packages', 'extension-api')});
await exec('yarn',['link', '@podman-desktop/api'], {cwd: path.join(__dirname, '..')});
}

async function unlinkApi() {
console.log('Unlinking @podman-desktop/api...');
await exec('yarn',['unlink'], {cwd: path.join(desktopPath, 'packages', 'extension-api')});
await exec('yarn',['unlink', '@podman-desktop/api'], {cwd: path.join(__dirname, '..')});
}

async function patchPDBuild() {
console.log('Removing crc build script from package.json...');
const filePath = path.resolve(desktopPath, 'package.json');
Expand All @@ -97,7 +85,7 @@ async function prepareDev() {
await checkAndCloneDesktopRepo();
removeCrcExt();
await patchPDBuild();
linkApi();

await exec('yarn',undefined, {cwd: desktopPath });
await buildPD();
await exec('yarn',[], {cwd: path.join(__dirname, '..')});
Expand Down Expand Up @@ -144,10 +132,8 @@ switch(firstArg) {
case 'run':
await run();
break;
case 'clean':
await unlinkApi();
break;

case 'prepare' :
default:
default:
await prepareDev();
}
22 changes: 11 additions & 11 deletions src/crc.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": {
"crcVersion": "2.14.0",
"gitSha": "1a1ef27",
"openshiftVersion": "4.12.1",
"podmanVersion": "4.3.1"
},
"links": {
"linux": "https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/2.14.0/crc-linux-amd64.tar.xz",
"darwin": "https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/2.14.0/crc-macos-installer.pkg",
"windows": "https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/2.14.0/crc-windows-installer.zip"
}
"version": {
"crcVersion": "2.14.0",
"gitSha": "1a1ef27",
"openshiftVersion": "4.12.1",
"podmanVersion": "4.3.1"
},
"links": {
"linux": "https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/2.14.0/crc-linux-amd64.tar.xz",
"darwin": "https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/2.14.0/crc-macos-installer.pkg",
"windows": "https://developers.redhat.com/content-gateway/file/pub/openshift-v4/clients/crc/2.14.0/crc-windows-installer.zip"
}
}
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as fs from 'node:fs';
import type { Status } from './daemon-commander';
import { DaemonCommander } from './daemon-commander';
import { LogProvider } from './log-provider';
import { getAssetsFolder, isWindows } from './util';
import { isWindows } from './util';
import { daemonStart, daemonStop, getCrcVersion } from './crc-cli';
import { getCrcDetectionChecks } from './detection-checks';
import { CrcInstall } from './install/crc-install';
Expand Down
39 changes: 20 additions & 19 deletions src/install/base-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import stream from 'node:stream/promises';
import * as os from 'node:os';
import { isFileExists } from '../util';


export abstract class BaseCheck implements extensionApi.InstallCheck {
abstract title: string;
abstract execute(): Promise<extensionApi.CheckResult>;
Expand All @@ -47,15 +46,15 @@ export abstract class BaseCheck implements extensionApi.InstallCheck {
export interface CrcReleaseInfo {
version: {
crcVersion: string;
gitSha: string;
openshiftVersion: string;
podmanVersion: string;
gitSha: string;
openshiftVersion: string;
podmanVersion: string;
};

links: {
linux: string;
darwin: string;
windows: string;
windows: string;
};
}

Expand All @@ -68,7 +67,6 @@ export interface Installer {
}

export abstract class BaseInstaller implements Installer {

protected statusBarItem: extensionApi.StatusBarItem | undefined;

abstract install(releaseInfo: CrcReleaseInfo, logger?: extensionApi.Logger): Promise<boolean>;
Expand All @@ -86,29 +84,32 @@ export abstract class BaseInstaller implements Installer {
const shasSumArr = shaSumContentResponse.body.split('\n');

let installerSha = '';
for(const shaLine of shasSumArr){
if(shaLine.trim().endsWith(fileName)) {
for (const shaLine of shasSumArr) {
if (shaLine.trim().endsWith(fileName)) {
installerSha = shaLine.split(' ')[0];
break;
}
}

if(!installerSha) {
if (!installerSha) {
console.error(`Can't find SHA256 sum for ${fileName} in:\n${shaSumContentResponse.body}`);
throw new Error(`Can't find SHA256 sum for ${fileName}.`);
}

return installerSha;
}
}

async downloadCrcInstaller(installerUrl: string, destinationPath: string, fileSha: string): Promise<void> {
let lastProgressStr = '';
const downloadStream = got.stream(installerUrl);
const lastProgressStr = '';

this.statusBarItem.text = 'Downloading';
const downloadStream = got.stream(installerUrl);


downloadStream.on('downloadProgress', (progress) => {
const progressStr = /* progress.transferred + ' of ' + progress.total + ' ' + */Math.round(progress.percent * 100) + '%';
if(lastProgressStr !== progressStr) {
downloadStream.on('downloadProgress', progress => {
const progressStr =
/* progress.transferred + ' of ' + progress.total + ' ' + */ Math.round(progress.percent * 100) + '%';
if (lastProgressStr !== progressStr) {
//TODO: show progress on UI!
this.statusBarItem.text = 'Downloading: ' + progressStr;
}
});
Expand All @@ -117,7 +118,7 @@ export abstract class BaseInstaller implements Installer {

console.log(`Downloaded to ${destinationPath}`);

this.statusBarItem.text = 'Downloaded, verifying SHA...'
this.statusBarItem.text = 'Downloaded, verifying SHA...';
if (!(await checkFileSha(destinationPath, fileSha))) {
throw new Error(`Checksum for downloaded ${destinationPath} is not match, try to download again!`);
}
Expand All @@ -129,10 +130,10 @@ export abstract class BaseInstaller implements Installer {
const sha = await this.downloadSha(installerUrl, installerFileName);

const installerFolder = path.resolve(os.tmpdir(), 'crc-extension');
await fs.mkdir(installerFolder, {recursive: true});
await fs.mkdir(installerFolder, { recursive: true });
const installerPath = path.resolve(installerFolder, installerFileName);

if (!await isFileExists(installerPath)) {
if (!(await isFileExists(installerPath))) {
await this.downloadCrcInstaller(installerUrl, installerPath, sha);
this.removeStatusBar();
return installerPath;
Expand Down
6 changes: 3 additions & 3 deletions src/install/crc-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import { getCrcVersion } from '../crc-cli';
import { getCrcDetectionChecks } from '../detection-checks';
import { MacOsInstall } from './mac-install';

const crcLatestReleaseUrl = 'https://developers.redhat.com/content-gateway/rest/mirror/pub/openshift-v4/clients/crc/latest/release-info.json';
const crcLatestReleaseUrl =
'https://developers.redhat.com/content-gateway/rest/mirror/pub/openshift-v4/clients/crc/latest/release-info.json';

export class CrcInstall {
private installers = new Map<NodeJS.Platform, Installer>();
Expand Down Expand Up @@ -58,7 +59,6 @@ export class CrcInstall {
logger: extensionApi.Logger,
installFinishedFn: () => void,
): Promise<void> {

const latestRelease = await this.downloadLatestReleaseInfo();

const dialogResult = await extensionApi.window.showInformationMessage(
Expand All @@ -68,7 +68,7 @@ export class CrcInstall {
);
if (dialogResult === 'Yes') {
const installed = await this.installCrc(latestRelease, logger);
if(installed) {
if (installed) {
const newInstalledCrc = await getCrcVersion();
// // write crc version
// if (newInstalledCrc) {
Expand Down
Loading

0 comments on commit 82860ce

Please sign in to comment.