-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
904d1b6
commit 61fcbdd
Showing
64 changed files
with
2,774 additions
and
445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 :(")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.