Skip to content

Commit

Permalink
Lint & refactor codebase (#539)
Browse files Browse the repository at this point in the history
* Ignore shims script (hard forked)

* Format scripts

* Remove redundant code

* [Refactor] Remove ternary statement

* Add ESM top-level await

* Add TS types, clean up

* Ignore ESLint warnings in scripts

* Remove redundant code

* Lint tests

* Remove stylistic ESLint rule

!array.length is acceptable here.

* Ignore ESLint errors on yaml parsing

No easy solution here; this is already in the wild.
Needs proper reimplementation at some point.

* nit

* [Refactor] if-else => switch

* [Refactor] Remove redundant code

* Remove unnecessary use of static class

* Annotate remaining low-priority ESLint items

* Lint configs

* Lint playwright config

* nit
  • Loading branch information
webfiltered authored Dec 19, 2024
1 parent 146a588 commit a179d5c
Show file tree
Hide file tree
Showing 33 changed files with 345 additions and 305 deletions.
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ out
dist
assets
.vite
scripts
scripts/shims
.env_*
12 changes: 11 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import eslintPluginUnicorn from 'eslint-plugin-unicorn';
export default tseslint.config(
// Baseline include / exclude
{ files: ['**/*.{js,cjs,mjs,ts,mts}'] },
{ ignores: ['dist/**/*', 'jest.config.cjs'] },
{ ignores: ['dist/**/*', 'jest.config.cjs', 'scripts/shims/**/*'] },

// Baseline
eslint.configs.recommended,
Expand Down Expand Up @@ -51,6 +51,7 @@ export default tseslint.config(
'unicorn/no-null': 'off',
'unicorn/prevent-abbreviations': 'off',
'unicorn/switch-case-braces': 'off',
'unicorn/explicit-length-check': 'off',
},
},

Expand All @@ -60,5 +61,14 @@ export default tseslint.config(
rules: {
'unicorn/no-process-exit': 'off',
},
},

// Tests
{
files: ['tests/**/*'],
rules: {
'unicorn/prefer-module': 'off',
'@typescript-eslint/unbound-method': 'off',
},
}
);
15 changes: 7 additions & 8 deletions playwright.setup.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { type FullConfig } from '@playwright/test';
import { spawn } from 'child_process';
import { spawn } from 'node:child_process';

async function globalSetup(config: FullConfig) {
console.log('globalSetup');
async function globalSetup() {
console.log('Playwright globalSetup called');

return new Promise<void>(async (resolve, reject) => {
return new Promise<void>((resolve, reject) => {
const electron = spawn('node', ['./scripts/launchdev.js']);

electron.on('close', () => {
reject('process failed to start');
reject(new Error('process failed to start'));
});

electron.stdout.on('data', (data) => {
if (data.indexOf('App ready') >= 0) {
electron.stdout.on('data', (data: string | Buffer) => {
if (data.includes('App ready')) {
resolve();
}
});
Expand Down
69 changes: 35 additions & 34 deletions scripts/downloadFrontend.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
import axios from 'axios'
import extract from 'extract-zip'
import fs from 'node:fs/promises'
import path from 'node:path'
import axios from 'axios';
import extract from 'extract-zip';
import fs from 'node:fs/promises';
import path from 'node:path';

import packageJson from './getPackage.js'
import packageJson from './getPackage.js';

// Example "v1.3.34"
const version = process.argv[2] || packageJson.config.frontendVersion;
if (!version) {
console.error('No version specified');
process.exit(1);
console.error('No version specified');
process.exit(1);
}

const url = `https://github.com/Comfy-Org/ComfyUI_frontend/releases/download/v${version}/dist.zip`;
const downloadPath = 'temp_frontend.zip';
const extractPath = 'assets/ComfyUI/web_custom_versions/desktop_app';

async function downloadAndExtractFrontend() {
try {
// Create directories if they don't exist
await fs.mkdir(extractPath, { recursive: true });

// Download the file
console.log('Downloading frontend...');
const response = await axios({
method: 'GET',
url: url,
responseType: 'arraybuffer'
});

// Save to temporary file
await fs.writeFile(downloadPath, response.data);

// Extract the zip file
console.log('Extracting frontend...');
await extract(downloadPath, { dir: path.resolve(extractPath) });

// Clean up temporary file
await fs.unlink(downloadPath);

console.log('Frontend downloaded and extracted successfully!');
} catch (error) {
console.error('Error downloading frontend:', error.message);
process.exit(1);
}
try {
// Create directories if they don't exist
await fs.mkdir(extractPath, { recursive: true });

// Download the file
console.log('Downloading frontend...');
const response = await axios({
method: 'GET',
url: url,
responseType: 'arraybuffer',
});

// Save to temporary file
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await fs.writeFile(downloadPath, response.data);

// Extract the zip file
console.log('Extracting frontend...');
await extract(downloadPath, { dir: path.resolve(extractPath) });

// Clean up temporary file
await fs.unlink(downloadPath);

console.log('Frontend downloaded and extracted successfully!');
} catch (error) {
console.error('Error downloading frontend:', error.message);
process.exit(1);
}
}

await downloadAndExtractFrontend();
125 changes: 61 additions & 64 deletions scripts/downloadUV.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,83 @@
import path from "node:path"
import os from 'node:os'
import fs from 'fs-extra'
import axios from 'axios'
import * as tar from 'tar'
import extractZip from 'extract-zip'
import packageJson from './getPackage.js'
import path from 'node:path';
import os from 'node:os';
import fs from 'fs-extra';
import axios from 'axios';
import * as tar from 'tar';
import extractZip from 'extract-zip';
import packageJson from './getPackage.js';

const uvVer = packageJson.config.uvVersion;

/** @typedef {{ [key]: { zipFile: string, uvOutputFolderName: string, zip: boolean } }} UvDownloadOptions */
const options = {
win32: {
zipFile: 'uv-x86_64-pc-windows-msvc.zip',
uvOutputFolderName: 'win',
zip: true,
},
darwin: {
zipFile: 'uv-aarch64-apple-darwin.tar.gz',
uvOutputFolderName: 'macos',
zip: false,
},
linux: {
zipFile: 'uv-x86_64-unknown-linux-gnu.tar.gz',
uvOutputFolderName: 'linux',
zip: false,
}
}
win32: {
zipFile: 'uv-x86_64-pc-windows-msvc.zip',
uvOutputFolderName: 'win',
zip: true,
},
darwin: {
zipFile: 'uv-aarch64-apple-darwin.tar.gz',
uvOutputFolderName: 'macos',
zip: false,
},
linux: {
zipFile: 'uv-x86_64-unknown-linux-gnu.tar.gz',
uvOutputFolderName: 'linux',
zip: false,
},
};

async function downloadUV() {

const allFlag = process.argv[2];
const baseDownloadURL = `https://github.com/astral-sh/uv/releases/download/${uvVer}/`;
if (allFlag)
{
if (allFlag === 'all') {
await downloadAndExtract(baseDownloadURL, options.win32);
await downloadAndExtract(baseDownloadURL, options.darwin);
await downloadAndExtract(baseDownloadURL, options.linux);
return;
}
if (allFlag === 'none') {
return;
}
if (allFlag) {
if (allFlag === 'all') {
await downloadAndExtract(baseDownloadURL, options.win32);
await downloadAndExtract(baseDownloadURL, options.darwin);
await downloadAndExtract(baseDownloadURL, options.linux);
return;
}
if (allFlag === 'none') {
return;
}
}

const uvDownloaded = fs.existsSync(path.join('./assets', 'uv'));
if (!uvDownloaded) {
await downloadAndExtract(baseDownloadURL, options[os.platform()]);
return;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await downloadAndExtract(baseDownloadURL, options[os.platform()]);
return;
}
console.log('< UV Folder Exists, Skipping >');

};
}

/** @param {UvDownloadOptions[any]} options */
async function downloadAndExtract(baseURL, options) {
const {
zipFile,
uvOutputFolderName,
zip
} = options;
const zipFilePath = path.join('./assets', zipFile);
const outputUVFolder = path.join('./assets', 'uv', uvOutputFolderName);
await fs.mkdir(outputUVFolder, {
recursive: true
const { zipFile, uvOutputFolderName, zip } = options;
const zipFilePath = path.join('./assets', zipFile);
const outputUVFolder = path.join('./assets', 'uv', uvOutputFolderName);
await fs.mkdir(outputUVFolder, {
recursive: true,
});
const downloadedFile = await axios({
method: 'GET',
url: baseURL + zipFile,
responseType: 'arraybuffer',
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
fs.writeFileSync(zipFilePath, downloadedFile.data);
if (zip) {
await extractZip(zipFilePath, { dir: path.resolve(outputUVFolder) });
} else {
tar.extract({
sync: true,
file: zipFilePath,
C: outputUVFolder,
'strip-components': 1,
});
const downloadedFile = await axios({
method: 'GET',
url: baseURL + zipFile,
responseType: 'arraybuffer'
});
fs.writeFileSync(zipFilePath, downloadedFile.data);
zip ? await extractZip(zipFilePath, {
dir: path.resolve(outputUVFolder)
}) : tar.extract({
sync: true,
file: zipFilePath,
C: outputUVFolder,
"strip-components": 1
});
await fs.unlink(zipFilePath);
console.log(`FINISHED DOWNLOAD AND EXTRACT UV ${uvOutputFolderName}`);
}
await fs.unlink(zipFilePath);
console.log(`FINISHED DOWNLOAD AND EXTRACT UV ${uvOutputFolderName}`);
}

//** Download and Extract UV. Default uses OS.Platfrom. Add 'all' will download all. Add 'none' will skip */
Expand Down
5 changes: 3 additions & 2 deletions scripts/getPackage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Read the main package.json
import { createRequire } from "node:module";
import { createRequire } from 'node:module';

/** @type {import('../package.json')} */
const packageJson = createRequire(import.meta.url)("../package.json");
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const packageJson = createRequire(import.meta.url)('../package.json');

export default packageJson;
1 change: 1 addition & 0 deletions scripts/launchdev.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function setupMainPackageWatcher() {
: ['--inspect=9223'];

/** Spawn new electron process */
// eslint-disable-next-line @typescript-eslint/no-base-to-string
electronApp = spawn(String(electronPath), [...args, '.'], {
stdio: 'inherit',
});
Expand Down
8 changes: 4 additions & 4 deletions scripts/makeComfy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as child_process from 'node:child_process'
import pkg from './getPackage.js'
import * as child_process from 'node:child_process';
import pkg from './getPackage.js';

function makeAssets(gpuFlag) {
const baseCommand = [
Expand All @@ -12,7 +12,7 @@ function makeAssets(gpuFlag) {
'--manager-url',
'https://github.com/Comfy-Org/ComfyUI-Manager',
'&&',
'yarn run make:frontend'
'yarn run make:frontend',
].join(' ');

try {
Expand All @@ -29,7 +29,7 @@ const gpuFlags = {
nvidia: '--nvidia',
amd: '--amd',
cpu: '--cpu',
macos: '--m-series'
macos: '--m-series',
};

if (!arg || !gpuFlags[arg]) {
Expand Down
Loading

0 comments on commit a179d5c

Please sign in to comment.