This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from nachoaldamav/feat/init-command
feat ✨: (cli) Added `ultra init` command
- Loading branch information
Showing
5 changed files
with
102 additions
and
10 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
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
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,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); | ||
} |
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,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; |