Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #76 from nachoaldamav/feat/init-command
Browse files Browse the repository at this point in the history
feat ✨: (cli) Added `ultra init` command
  • Loading branch information
nachoaldamav authored Oct 5, 2022
2 parents d1504bd + e76cbb4 commit 3f46b7e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 10 deletions.
17 changes: 11 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
platforms:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand All @@ -21,18 +21,23 @@ jobs:
with:
version: 7.11.0
- name: Install dependencies
run: cd packages/cli && pnpm install
run: pnpm install
- name: Build CLI
run: cd packages/cli && pnpm run build
run: pnpm run build --filter=ultrapkg
- name: Setup Bun Runtime
uses: antongolub/action-setup-bun@v1.12.8
- name: Install CLI
run: cd packages/cli && npm install . -g && cd ../../
run: npm install ./packages/cli -g
- name: Run benchmark
run: cd examples/alotta && fnpm benchmark --ignore-bun
# Copy examples/alotta to parent root
run: cp -r ./examples/alotta ../alotta && cd ../alotta && ultra benchmark --json
- name: Show JSON
run: cat ../alotta/results.json
- name: Read results
id: read_file
uses: andstor/file-reader-action@v1
with:
path: "./examples/alotta/results.md"
path: "../alotta/results.md"
- name: Create comment
uses: peter-evans/create-or-update-comment@v2
with:
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { existsSync, rm, rmSync, symlinkSync } from "node:fs";
import { spawn } from "child_process";
import { getDeps } from "../utils/getDeps.js";
import readPackage from "../utils/readPackage.js";
import manifestFetcher from "../utils/manifestFetcher.js";

export default async function create(args: string[]) {
if (args.length === 0) {
Expand All @@ -34,7 +35,9 @@ export default async function create(args: string[]) {
args.shift();

const spinner = ora(`Searching ${command} in NPM Registry...`).start();
const manifest = await pacote.manifest(command);
const manifest = await manifestFetcher(command, {
registry: "https://registry.npmjs.org/",
});
spinner.succeed();
spinner.text = `Found ${command} in NPM Registry`;

Expand Down Expand Up @@ -71,9 +74,8 @@ export default async function create(args: string[]) {
__installing.succeed();

// Get bin path
const bin = readPackage(globalPath + "/package.json").then(
(res: any) => res.bin
);
const bin = readPackage(path.join(globalPath, "package.json"))
.bin as string;

const isObject = bin && typeof bin === "object";
const binName = isObject ? Object.keys(bin)[0] : bin;
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import autocompletion from "./autocompletion.js";
import { performance } from "perf_hooks";
import ora from "ora";
import test from "./test.js";
import init from "./init.js";

const comms = [
{
Expand Down Expand Up @@ -84,6 +85,13 @@ const comms = [
abr: "t",
params: true,
},
{
name: "init",
description: "Initialize a package.json file",
command: init,
abr: "init",
params: true,
},
];

export async function commands(args: string[]) {
Expand Down
63 changes: 63 additions & 0 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import prompts from "prompts";
import { writeFileSync, readFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import chalk from "chalk";
import { __dirname } from "../utils/__dirname.js";
import samplePkg from "../utils/sample/package.js";
import { execa } from "execa";

export default async function init(args: string[]) {
let sampleJson = samplePkg;
if (existsSync("package.json")) {
console.log(
chalk.red(
"A package.json file already exists in this directory. Please delete it before running this command."
)
);
process.exit(1);
}

// Get current directory name
const currentDir = process.cwd().split("/").pop();

// If has -y just use the default values
if (args.includes("-y") && currentDir) {
sampleJson.name = currentDir;
writeFileSync(
join(process.cwd(), "package.json"),
JSON.stringify(sampleJson, null, 2)
);
console.log(`Created ${chalk.green("package.json")} with default values!`);
return;
}

const responses = await prompts([
{
type: "text",
name: "name",
message: "What is the name of your package?",
initial: currentDir,
},
{
type: "text",
name: "version",
message: "What is the version of your package?",
initial: "1.0.0",
},
{
type: "text",
name: "description",
message: "What is the description of your package?",
initial: "",
},
]);

// If no template, just create a package.json file
sampleJson.name = responses.name;
writeFileSync(
join(process.cwd(), "package.json"),
JSON.stringify(sampleJson, null, 2)
);
console.log(`Created ${chalk.green("package.json")}!`);
process.exit(0);
}
14 changes: 14 additions & 0 deletions packages/cli/src/utils/sample/package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const samplePkg = {
name: "name",
version: "1.0.0",
description: "",
main: "index.js",
scripts: {
test: 'echo "Error: no test specified" && exit 1',
},
keywords: [],
author: "",
license: "ISC",
};

export default samplePkg;

0 comments on commit 3f46b7e

Please sign in to comment.