Skip to content

Commit

Permalink
Merge pull request #14 from schalkneethling/8-enhance-check-for-prese…
Browse files Browse the repository at this point in the history
…nce-of-packagejson

enhance: check for precense of package.json
  • Loading branch information
schalkneethling authored Dec 29, 2024
2 parents 9c1f2dc + abf7fce commit 58b8c1a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-project-calavera",
"version": "0.0.9",
"version": "0.1.0",
"description": "A simple starting skeleton for common web projects.",
"exports": "./src/index.js",
"bin": {
Expand Down
68 changes: 58 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#!/usr/bin/env node
import { cancel, isCancel, intro, multiselect, spinner } from "@clack/prompts";
import { access, constants } from "node:fs/promises";
import { resolve } from "node:path";

import {
cancel,
confirm,
isCancel,
intro,
multiselect,
spinner,
} from "@clack/prompts";
import { execa } from "execa";

import { FileWriteError } from "./utils/file-write-error.js";
Expand All @@ -12,13 +22,49 @@ import configureStylelint from "./installers/stylelint.js";
import configureTSConfig from "./installers/tsconfig.js";
import configureTSESLint from "./installers/ts-eslint.js";

const main = async () => {
let dependencies = [];
/**
* Checks for the presence of an existing package.json file.
* If not found, prompts the user to create a default package.json.
* Exits the process if the user cancels the creation.
* @returns {Promise<void>}
*/
async function checkPrerequisites() {
logger.info("Checking for presence of an existing package.json...");

const packageJSON = resolve("package.json");

let createPackageJSON;
try {
await access(packageJSON, constants.F_OK);
} catch {
createPackageJSON = await confirm({
message:
"No package.json found. You will need one to continue, create a default?",
});

if (!createPackageJSON) {
cancel("Setup cancelled 👋");
process.exit(0);
}

const spin = spinner();

spin.start("📦 Creating package.json...");
await execa("npm", ["init", "-y"], {
stderr: "inherit",
});

spin.stop("📦 Created package.json");
}
}

const main = async () => {
console.clear();

intro("Let's get you linting and formatting! 🧶");

await checkPrerequisites();

const options = [
{ value: "editorconfig", label: "EditorConfig", hint: "Recommended" },
{
Expand Down Expand Up @@ -54,6 +100,12 @@ const main = async () => {
required: true,
});

if (isCancel(tools)) {
cancel("Setup cancelled 👋");
process.exit(0);
}

let dependencies = [];
let withPrettier = tools.includes("prettier");
let withTypeScript =
tools.includes("tsconfig") || tools.includes("tsconfig-noemit");
Expand Down Expand Up @@ -89,20 +141,16 @@ const main = async () => {
dependencies = [...dependencies, ...stylelintDeps];
}

const spin = spinner();
if (dependencies.length > 0) {
const spin = spinner();
spin.start("📦 Installing dependencies...");
await execa("npm", ["install", "--save-dev", ...dependencies], {
stderr: "inherit",
});
spin.stop("Dependencies installed 👍");
}

spin.stop("All done! 🎉 Happy coding 🙌");

if (isCancel(tools)) {
cancel("Setup cancelled 👋");
process.exit(0);
}
logger.info("\nAll done! 🎉 Happy coding 🙌");
};

main().catch((error) => {
Expand Down
1 change: 1 addition & 0 deletions src/installers/stylelint.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const configureStylelint = async () => {
plugins: ["stylelint-order"],
rules: {
"order/properties-alphabetical-order": true,
"custom-property-empty-line-before": null,
},
};

Expand Down

0 comments on commit 58b8c1a

Please sign in to comment.