Skip to content

Commit

Permalink
refactor: lint (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
vijayymmeena authored Jun 11, 2024
1 parent 904d1b6 commit 61fcbdd
Show file tree
Hide file tree
Showing 64 changed files with 2,774 additions and 445 deletions.
9 changes: 7 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:@typescript-eslint/strict-type-checked",
"plugin:@typescript-eslint/stylistic-type-checked",
"plugin:prettier/recommended",
],
Expand All @@ -20,7 +20,12 @@
],
"root": true,
"rules": {
// disabled
// typescript-eslint
"@typescript-eslint/consistent-return": "error",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/consistent-type-definitions": "error",
"@typescript-eslint/consistent-type-exports": "error",
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-redundant-type-constituents": "off",
"@typescript-eslint/no-unsafe-argument": "off",
Expand Down
410 changes: 210 additions & 200 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"@changesets/cli": "^2.27.5",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-angular": "^19.3.0",
"@typescript-eslint/eslint-plugin": "^7.12.0",
"@typescript-eslint/parser": "^7.12.0",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
Expand All @@ -66,9 +66,9 @@
"husky": "^9.0.11",
"is-ci": "^3.0.1",
"minipass": "^7.1.2",
"prettier": "^3.3.1",
"prettier": "^3.3.2",
"tsup": "^8.1.0",
"turbo": "^2.0.1",
"turbo": "^2.0.3",
"typedoc": "^0.25.13",
"typescript": "5.4.5"
},
Expand Down
19 changes: 19 additions & 0 deletions packages/create-discordx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# create-discordx

## 1.2.2

### Patch Changes

- lint

## 1.2.1

### Patch Changes

- c697f01b: fix: rimraf export (#962)

## 1.2.0

### Minor Changes

- fix: monorepo
2 changes: 1 addition & 1 deletion packages/create-discordx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-discordx",
"version": "1.2.1",
"version": "1.2.2",
"description": "create discordx apps with one command",
"keywords": [
"bot",
Expand Down
144 changes: 144 additions & 0 deletions packages/create-discordx/src/helper/package-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* -------------------------------------------------------------------------------------------------------
* Copyright (c) Vijay Meena <[email protected]> (https://github.com/samarmeena). All rights reserved.
* Licensed under the Apache License. See License.txt in the project root for license information.
* -------------------------------------------------------------------------------------------------------
*/
import chalk from "chalk";
import { exec, execSync } from "child_process";
import ora from "ora";
import prompts from "prompts";

export enum PackageManager {
npm,
yarn,
pnpm,
none,
}

export async function GetPackageManager(): Promise<PackageManager | null> {
const selected = await prompts(
{
choices: [
{
title: "npm",
value: PackageManager.npm.toString(),
},
{
title: "yarn",
value: PackageManager.yarn.toString(),
},
{
title: "pnpm",
value: PackageManager.pnpm.toString(),
},
{
title: "none - do not install packages",
value: PackageManager.none.toString(),
},
],
message: "Pick package manager",
name: "package-manager",
type: "select",
},
{
onCancel: () => {
process.exit();
},
},
);

const manager = Number(selected["package-manager"]) as PackageManager;

try {
switch (manager) {
case PackageManager.none:
break;

case PackageManager.npm:
execSync("npm --version", { stdio: "ignore" });
break;

case PackageManager.yarn:
execSync("yarn --version", { stdio: "ignore" });
break;

case PackageManager.pnpm:
execSync("pnpm --version", { stdio: "ignore" });
break;
}
} catch (err) {
console.log(
chalk.red("×"),
`Could not found ${chalk.greenBright(
PackageManager[manager],
)} package manager, Please install it from:`,
PackageManager.pnpm === manager
? "https://pnpm.io"
: PackageManager.yarn === manager
? "https://yarnpkg.com"
: "https://nodejs.org/en/download",
);

return GetPackageManager();
}

return manager;
}

export async function InstallPackage(
root: string,
manager: PackageManager,
): Promise<void> {
if (PackageManager.none === manager) {
console.log(
chalk.blueBright("?"),
chalk.bold("skipped package installation..."),
);
return;
}

const spinner = ora({
text: chalk.bold("Installing packages..."),
}).start();

try {
switch (manager) {
case PackageManager.npm:
await new Promise((resolve, reject) => {
exec("npm install", { cwd: root }, (err) => {
if (err) {
reject(err);
}
resolve(true);
});
});
break;

case PackageManager.yarn:
await new Promise((resolve, reject) => {
exec("yarn install", { cwd: root }, (err) => {
if (err) {
reject(err);
}
resolve(true);
});
});
break;

case PackageManager.pnpm:
await new Promise((resolve, reject) => {
exec("pnpm install", { cwd: root }, (err) => {
if (err) {
reject(err);
}
resolve(true);
});
});
}

spinner.succeed(chalk.bold("Installed packages"));
} catch (err) {
spinner.fail(chalk.bold("Failed to install packages :("));
}
}
91 changes: 91 additions & 0 deletions packages/create-discordx/src/helper/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* -------------------------------------------------------------------------------------------------------
* Copyright (c) Vijay Meena <[email protected]> (https://github.com/samarmeena). All rights reserved.
* Licensed under the Apache License. See License.txt in the project root for license information.
* -------------------------------------------------------------------------------------------------------
*/
import axios from "axios";
import { createWriteStream, promises as fs } from "fs";
import { tmpdir } from "os";
import { join } from "path";
import { Readable, Stream } from "stream";
import tar from "tar";
import { promisify } from "util";

/**
* Get templates list from https://github.com/discordx-ts/templates
*
* @returns
*/
export async function GetTemplates(): Promise<
{ title: string; value: string }[]
> {
const response = await axios
.get<{ name: string; path: string; type: string }[]>(
"https://api.github.com/repos/discordx-ts/templates/contents",
)
.then((res) =>
res.data
.filter((row) => row.type === "dir" && /^[0-9].+/.test(row.name))
.map((row) => ({ title: row.name, value: row.path })),
)
.catch(() => []);

return response;
}

/**
* Check if the template exists on GitHub
*
* @param name template name
* @returns
*/
export async function IsTemplateExist(name: string): Promise<boolean> {
const response = await axios
.get(
`https://api.github.com/repos/discordx-ts/templates/contents/${name}?ref=main`,
)
.then(() => true)
.catch(() => false);

return response;
}

async function downloadTar(url: string) {
const pipeline = promisify(Stream.pipeline);
const tempFilename = `discordx-template.temp-${Date.now().toString()}`;
const tempFilePath = join(tmpdir(), tempFilename);

const request = await axios({
responseType: "stream",
url: url,
});

await pipeline(Readable.from(request.data), createWriteStream(tempFilePath));
return tempFilePath;
}

/**
* Download and extract template
*
* @param root project path
* @param name project name
* @returns
*/
export async function DownloadAndExtractTemplate(
root: string,
name: string,
): Promise<void> {
const tempFile = await downloadTar(
"https://codeload.github.com/discordx-ts/templates/tar.gz/main",
);

await tar.x({
cwd: root,
file: tempFile,
filter: (p) => p.includes(`templates-main/${name}`),
strip: 2,
});

await fs.unlink(tempFile);
}
62 changes: 62 additions & 0 deletions packages/create-discordx/src/helper/updater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* -------------------------------------------------------------------------------------------------------
* Copyright (c) Vijay Meena <[email protected]> (https://github.com/samarmeena). All rights reserved.
* Licensed under the Apache License. See License.txt in the project root for license information.
* -------------------------------------------------------------------------------------------------------
*/
import { readFile } from "node:fs/promises";

import boxen from "boxen";
import chalk from "chalk";
import isInstalledGlobally from "is-installed-globally";
import checkForUpdate from "update-check";

/**
* Read package.json
*/
const packageJson = JSON.parse(
await readFile(new URL("../package.json", import.meta.url), "utf-8"),
);

/**
* Check for update
*/

let update = null;

try {
update = await checkForUpdate(packageJson);
} catch (err) {
console.log(
boxen("Failed to check for updates", {
align: "center",
borderColor: "red",
borderStyle: "round",
margin: 1,
padding: 1,
}),
);
}

if (update) {
const updateCmd = isInstalledGlobally
? "npm i -g create-discordx@latest"
: "npm i create-discordx@latest";

const updateVersion = packageJson.version;
const updateLatest = update.latest;
const updateCommand = updateCmd;
const template = `Update available ${chalk.dim(updateVersion)}${chalk.reset(" → ")}${chalk.green(updateLatest)} \nRun ${chalk.cyan(updateCommand)} to update`;

console.log(
boxen(template, {
align: "center",
borderColor: "yellow",
borderStyle: "round",
margin: 1,
padding: 1,
}),
);
}

export default packageJson.version;
Loading

0 comments on commit 61fcbdd

Please sign in to comment.