Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use citty for cli #113

Merged
merged 12 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
declaration: true,
rollup: {
emitCJS: true,
},
entries: [
'src/index.ts',
'src/cli.ts',
],
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
"test": "pnpm lint && vitest run --coverage"
},
"dependencies": {
"colorette": "^2.0.20",
"citty": "^0.1.5",
"consola": "^3.2.3",
"defu": "^6.1.3",
"https-proxy-agent": "^7.0.2",
"mri": "^1.2.0",
"node-fetch-native": "^1.4.1",
"pathe": "^1.1.1",
"tar": "^6.2.0"
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 78 additions & 56 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,86 @@
#!/usr/bin/env node
import { relative } from "node:path";
import mri from "mri";
import { cyan } from "colorette";
import { defineCommand, runMain } from "citty";
import { consola } from "consola";
import pkg from "../package.json" assert { type: "json" };

Check warning on line 4 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L2-L4

Added lines #L2 - L4 were not covered by tests
import { downloadTemplate } from "./giget";
import { startShell } from "./_utils";

async function main() {
const arguments_ = mri(process.argv.slice(2), {
boolean: [
"help",
"force",
"force-clean",
"offline",
"prefer-offline",
"shell",
"verbose",
],
string: ["registry", "cwd", "auth"],
});
const mainCommand = defineCommand({
meta: {
name: pkg.name,
version: pkg.version,
description: pkg.description,
},
args: {
// TODO: Make it `-t` in the next major version
template: {
type: "positional",
description:
"Template name or a a URI describing provider, repository, subdir, and branch/ref",
},
dir: {
type: "positional",
description: "A relative or absolute path where to extract the template",
required: false,
},
auth: {
type: "string",
description:
"Custom Authorization token to use for downloading template. (Can be overriden with `GIGET_AUTH` environment variable)",
},
cwd: {
type: "string",
description:
"Set current working directory to resolve dirs relative to it",
},
force: {
type: "boolean",
description: "Clone to existing directory even if exists",
},
forceClean: {
type: "boolean",
description:
"Remove any existing directory or file recusively before cloning",
},
offline: {
type: "boolean",
description: "o not attempt to download and use cached version",
},
preferOffline: {
type: "boolean",
description: "Use cache if exists otherwise try to download",
},
shell: {
type: "boolean",
description: "Open a new shell with current working ",
},
verbose: {
type: "boolean",
description: "Show verbose debugging info",
},
},
run: async ({ args }) => {
if (args.verbose) {
process.env.DEBUG = process.env.DEBUG || "true";
}

Check warning on line 65 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L8-L65

Added lines #L8 - L65 were not covered by tests

const input = arguments_._[0];
// eslint-disable-next-line unicorn/prevent-abbreviations
const dir = arguments_._[1];
const r = await downloadTemplate(args.template, {
dir: args.dir,
force: args.force,
forceClean: args.forceClean,
offline: args.offline,
preferOffline: args.preferOffline,
auth: args.auth,
});

Check warning on line 74 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L67-L74

Added lines #L67 - L74 were not covered by tests

if (!input || arguments_.help || arguments_.h) {
console.error(
"Usage: npx giget@latest <input> [<dir>] [--force] [--force-clean] [--offline] [--prefer-offline] [--shell] [--registry] [--no-registry] [--verbose] [--cwd] [--auth]",
);
process.exit(1);
}
const _from = r.name || r.url;
const _to = relative(process.cwd(), r.dir);
consola.log(`✨ Successfully cloned \`${_from}\` to \`${_to}\`\n`);

Check warning on line 78 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L76-L78

Added lines #L76 - L78 were not covered by tests

if (arguments_.verbose) {
process.env.DEBUG = process.env.DEBUG || "true";
}

const r = await downloadTemplate(input, {
dir,
force: arguments_.force,
forceClean: arguments_["force-clean"],
offline: arguments_.offline,
registry: arguments_.registry,
cwd: arguments_.cwd,
auth: arguments_.auth,
});

console.log(
`✨ Successfully cloned ${cyan(r.name || r.url)} to ${cyan(
relative(process.cwd(), r.dir),
)}\n`,
);

if (arguments_.shell) {
startShell(r.dir);
}

process.exit(0);
}

// eslint-disable-next-line unicorn/prefer-top-level-await
main().catch((error) => {
console.error(error);
process.exit(1);
if (args.shell) {
startShell(r.dir);
}
},

Check warning on line 83 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L80-L83

Added lines #L80 - L83 were not covered by tests
});

runMain(mainCommand);

Check warning on line 86 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L85-L86

Added lines #L85 - L86 were not covered by tests
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"module": "ESNext",
"moduleResolution": "Node",
"esModuleInterop": true,
"resolveJsonModule": true,
"strict": true
},
"include": ["src"]
"include": [
"src"
]
}