psf-cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "psf-cli",
3
+ "version": "0.1.0",
4
+ "main": "cli.js",
5
+ "bin": {
6
+ "psf": "src/cli.js"
7
+ },
8
+ "type": "module",
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": [],
13
+ "author": "",
14
+ "license": "ISC",
15
+ "description": "",
16
+ "dependencies": {
17
+ "chalk": "^5.6.2",
18
+ "commander": "^14.0.2"
19
+ }
20
+ }
package/src/cli.js ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env node
2
+
3
+ // Packages
4
+ import { createRequire } from "module";
5
+ import { Command } from "commander";
6
+ import path from "path";
7
+ import { fileURLToPath } from "url";
8
+ import chalk from "chalk";
9
+
10
+ // Templates
11
+ import node from "./templates/node/index.js";
12
+
13
+ const require = createRequire(import.meta.url);
14
+ const packageJson = require("../package.json");
15
+
16
+ const __filename = fileURLToPath(import.meta.url);
17
+ const __dirname = path.dirname(__filename);
18
+
19
+ const program = new Command();
20
+
21
+ // Principal config
22
+ program
23
+ .name("psf")
24
+ .description("CLI built in Node.js")
25
+ .version(packageJson.version);
26
+
27
+ // Global options
28
+ program.option("-t, --template <string>", "template type", "node");
29
+ program.option("-d, --dir <path>", "Output directory", process.cwd());
30
+
31
+ // Init command
32
+ program
33
+ .command("init <projectName>")
34
+ .description("Initing a new project")
35
+ .action(async (projectName) => {
36
+ const opts = program.opts();
37
+
38
+ try {
39
+ switch (opts.template) {
40
+ case "node":
41
+ await node(opts.dir, projectName);
42
+ console.log(chalk.green("Node project initializated!"));
43
+ break;
44
+ default:
45
+ throw new Error(
46
+ `Invalid template: "${opts.template}". Available templates: node`,
47
+ );
48
+ }
49
+ } catch (err) {
50
+ console.error(chalk.red(err));
51
+ }
52
+ });
53
+
54
+ program.parse();
@@ -0,0 +1,48 @@
1
+ import fs from "fs/promises";
2
+ import { version } from "os";
3
+ import path from "path";
4
+
5
+ // Creating a Node Project
6
+ export default async function createNodeProject(dir, projectName) {
7
+ await folderCreator(dir, projectName);
8
+ }
9
+
10
+ // Creating a folders
11
+ async function folderCreator(dir, projectName) {
12
+ const pathFolder = path.join(dir, projectName);
13
+
14
+ try {
15
+ await fs.mkdir(pathFolder, { recursive: true });
16
+ await fs.mkdir(path.join(pathFolder, "src"), { recursive: true });
17
+ await fs.writeFile(path.join(pathFolder, "src", "index.js"), "");
18
+ await fs.writeFile(
19
+ path.join(pathFolder, "README.md"),
20
+ "# New Node Project",
21
+ );
22
+ await fs.writeFile(
23
+ path.join(pathFolder, "package.json"),
24
+ packageJsonCreator(projectName),
25
+ );
26
+ await fs.writeFile(path.join(pathFolder, ".gitignore"), "node_modules\n");
27
+ } catch (err) {
28
+ console.error("Error creating folder:", err);
29
+ }
30
+ }
31
+
32
+ function packageJsonCreator(projectName) {
33
+ const packageJson = {
34
+ name: projectName,
35
+ version: "1.0.0",
36
+ description: "",
37
+ main: "src/index.js",
38
+ type: "module",
39
+ scripts: {
40
+ test: 'echo "Error: no test specified" && exit 1',
41
+ },
42
+ keywords: [],
43
+ author: "",
44
+ license: "ISC ",
45
+ };
46
+
47
+ return JSON.stringify(packageJson, "", 2);
48
+ }