makepack 1.0.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/README.md ADDED
File without changes
package/index.html ADDED
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <meta http-equiv="X-UA-Compatible" content="ie=edge" />
8
+ <title>Playground</title>
9
+ </head>
10
+
11
+ <body>
12
+ <div id="root"></div>
13
+ <script type="module" src="./index.tsx"></script>
14
+ </body>
15
+
16
+ </html>
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "makepack",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "A simple CLI tool to create a new npm package",
6
+ "categories": [
7
+ "Other"
8
+ ],
9
+ "author": {
10
+ "name": "Devnax",
11
+ "email": "devnaxrul@gmail.com"
12
+ },
13
+ "bin": "./src/index.js",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/devnax/makepack"
17
+ },
18
+ "scripts": {},
19
+ "devDependencies": {
20
+ "@eslint/js": "^9.15.0",
21
+ "@types/express": "^5.0.0",
22
+ "@types/node": "^22.10.1",
23
+ "@types/react": "^18.3.12",
24
+ "@types/react-dom": "^18.3.1",
25
+ "@vitejs/plugin-react": "^4.3.4",
26
+ "commander": "^12.1.0",
27
+ "esbuild": "^0.24.2",
28
+ "eslint": "^9.15.0",
29
+ "eslint-plugin-react-hooks": "^5.0.0",
30
+ "eslint-plugin-react-refresh": "^0.4.14",
31
+ "express": "^4.21.1",
32
+ "fs-extra": "^11.2.0",
33
+ "globals": "^15.12.0",
34
+ "inquirer": "^12.1.0",
35
+ "tsx": "^4.19.2",
36
+ "typescript": "^5.7.2",
37
+ "typescript-eslint": "^8.15.0",
38
+ "vite": "^6.0.2"
39
+ },
40
+ "dependencies": {
41
+ "glob": "^11.0.0",
42
+ "react": "^18.3.1",
43
+ "react-dom": "^18.3.1"
44
+ }
45
+ }
@@ -0,0 +1,60 @@
1
+ import inquirer from 'inquirer'
2
+ import esbuild from 'esbuild';
3
+ import fs from 'fs-extra';
4
+ import path from 'path';
5
+ import { glob } from 'glob'
6
+ import ts from 'typescript'
7
+
8
+ const pack = async (args) => {
9
+ try {
10
+ fs.removeSync(path.join(process.cwd(), args.outdir));
11
+ } catch (err) { }
12
+
13
+ const files = await glob('test/**/*.{tsx,ts,js,jsx}') || []
14
+ const entries = files.map(entry => path.join(process.cwd(), entry))
15
+
16
+ esbuild.build({
17
+ entryPoints: entries,
18
+ outdir: path.join(process.cwd(), args.outdir),
19
+ minify: true,
20
+ sourcemap: true,
21
+ format: "esm",
22
+ platform: 'node',
23
+ loader: { '.ts': 'ts' },
24
+ tsconfig: path.join(process.cwd(), 'tsconfig.json'),
25
+ }).then(() => {
26
+ console.log('Build completed successfully!');
27
+ }).catch((err) => {
28
+ console.error('Build failed:', err);
29
+ });
30
+
31
+ const options = {
32
+ declaration: true,
33
+ emitDeclarationOnly: true,
34
+ outDir: path.join(process.cwd(), args.outdir),
35
+ strict: true,
36
+ allowJs: true,
37
+ jsx: ts.JsxEmit.React,
38
+ esModuleInterop: true,
39
+ };
40
+
41
+ const program = ts.createProgram(files, options);
42
+ const emitResult = program.emit();
43
+ const diagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
44
+
45
+ if (diagnostics.length > 0) {
46
+ diagnostics.forEach(diagnostic => {
47
+ const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
48
+ if (diagnostic.file) {
49
+ const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
50
+ console.error(`Error at ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
51
+ } else {
52
+ console.error(`Error: ${message}`);
53
+ }
54
+ });
55
+ } else {
56
+ console.log('Type declarations generated successfully!');
57
+ }
58
+ }
59
+
60
+ export default pack
@@ -0,0 +1,195 @@
1
+ import path from "path"
2
+ import fs from "fs-extra"
3
+ import inquirer from "inquirer"
4
+ import { execSync, logLoader } from "../helpers.js"
5
+ const cwd = process.cwd()
6
+ const cwdFolder = cwd.split(path.sep).pop()
7
+
8
+ const create = async (args) => {
9
+ const { projectDirName } = await inquirer.prompt([
10
+ {
11
+ type: 'input',
12
+ name: 'projectDirName',
13
+ message: 'Enter the project name',
14
+ default: cwdFolder
15
+ }
16
+ ])
17
+
18
+ let projectDir = cwd
19
+
20
+ if (projectDirName !== cwdFolder) {
21
+ projectDir = path.join(cwd, projectDirName);
22
+ if (fs.existsSync(projectDir)) {
23
+ const { proceed } = await inquirer.prompt([
24
+ {
25
+ type: "confirm",
26
+ name: 'proceed',
27
+ message: "The directory already exists, do you want to overwrite it?",
28
+ default: cwdFolder
29
+ }
30
+ ])
31
+ if (!proceed) {
32
+ console.log('Project creation canceled.');
33
+ return
34
+ }
35
+ }
36
+
37
+ }
38
+
39
+ let { template } = await inquirer.prompt([
40
+ {
41
+ type: 'list',
42
+ name: 'template',
43
+ message: 'Select a template',
44
+ choices: ['typescript', 'javascript', 'react with typescript', 'react with javascript'],
45
+ default: 'typeScript'
46
+ }
47
+ ])
48
+
49
+ if (projectDirName !== cwdFolder) {
50
+ fs.removeSync(projectDir)
51
+ fs.mkdirSync(projectDir)
52
+ }
53
+ let dependencies = {}
54
+ let devDependencies = {
55
+ // "make-pack": "@latest"
56
+ }
57
+
58
+ if (template.includes("react")) {
59
+ dependencies = {
60
+ "react": "^17.0.2",
61
+ "react-dom": "^17.0.2"
62
+ }
63
+ }
64
+
65
+ if (template.includes("typescript")) {
66
+ devDependencies["typescript"] = "^4.4.2"
67
+ devDependencies["@types/react"] = "^18.3.12"
68
+ devDependencies["@types/react-dom"] = "^18.3.1"
69
+ }
70
+
71
+ let loader = logLoader("Creating project...")
72
+ fs.mkdirSync(path.join(projectDir, "src"))
73
+ fs.writeFileSync(path.join(projectDir, "package.json"), JSON.stringify({
74
+ name: projectDirName,
75
+ version: "1.0.0",
76
+ description: "",
77
+ main: "index.js",
78
+ scripts: {
79
+ "start": "makepack serve",
80
+ "build": "makepack build",
81
+ },
82
+ dependencies,
83
+ devDependencies,
84
+ keywords: [],
85
+ }, null, 2), "utf-8")
86
+
87
+ switch (template) {
88
+ case "typescript":
89
+ fs.writeFileSync(path.join(projectDir, "src/index.ts"), `console.log("Hello, World!")`, "utf-8")
90
+ break;
91
+ case "react with typescript":
92
+ fs.writeFileSync(path.join(projectDir, "src/index.tsx"), `import React from 'react';
93
+
94
+ const App: React.FC = () => {
95
+ return (
96
+ <div style={{ textAlign: 'center', marginTop: '50px' }}>
97
+ <h1>Welcome to React Typescript with make-pack CLI!</h1>
98
+ <p>Edit <code>index.tsx</code> and save to reload.</p>
99
+ <a
100
+ href="https://reactjs.org"
101
+ target="_blank"
102
+ rel="noopener noreferrer"
103
+ style={{ color: '#61dafb', textDecoration: 'none' }}
104
+ >
105
+ Learn React
106
+ </a>
107
+ </div>
108
+ );
109
+ };
110
+
111
+ export default App;
112
+ `)
113
+
114
+ break;
115
+ case "javascript":
116
+ fs.writeFileSync(path.join(projectDir, "src/index.js"), `console.log("Hello, World!")`, "utf-8")
117
+ break;
118
+ case "react with javascript":
119
+ fs.writeFileSync(path.join(projectDir, "src/index.jsx"), `import React from 'react';
120
+
121
+ const App: React.FC = () => {
122
+ return (
123
+ <div style={{ textAlign: 'center', marginTop: '50px' }}>
124
+ <h1>Welcome to React JS with make-pack CLI!</h1>
125
+ <p>Edit <code>index.jsx</code> and save to reload.</p>
126
+ <a
127
+ href="https://reactjs.org"
128
+ target="_blank"
129
+ rel="noopener noreferrer"
130
+ style={{ color: '#61dafb', textDecoration: 'none' }}
131
+ >
132
+ Learn React
133
+ </a>
134
+ </div>
135
+ );
136
+ };
137
+ export default App;
138
+ `)
139
+ break;
140
+ }
141
+
142
+ // generate tsconfig.json
143
+ if (template.includes("typescript")) {
144
+ let config = {
145
+ "compilerOptions": {
146
+ "target": "es5",
147
+ "lib": ["dom", "dom.iterable", "esnext"],
148
+ "allowJs": true,
149
+ "skipLibCheck": true,
150
+ "esModuleInterop": true,
151
+ "allowSyntheticDefaultImports": true,
152
+ "strict": true,
153
+ "forceConsistentCasingInFileNames": true,
154
+ "module": "esnext",
155
+ "moduleResolution": "node",
156
+ "resolveJsonModule": true,
157
+ "isolatedModules": true,
158
+ "noEmit": true,
159
+ },
160
+ "include": ["src"]
161
+ }
162
+ // config for react
163
+ if (template.includes("react")) {
164
+ config.compilerOptions["jsx"] = "react-jsx"
165
+ config.compilerOptions["jsxImportSource"] = "react"
166
+ }
167
+ fs.writeFileSync(path.join(projectDir, "tsconfig.json"), JSON.stringify(config, null, 2), "utf-8")
168
+ }
169
+
170
+ // generate .gitignore
171
+ fs.writeFileSync(path.join(projectDir, ".gitignore"), "node_modules\n", "utf-8")
172
+
173
+ loader.stop("")
174
+
175
+ loader = logLoader("Installing dependencies...")
176
+ execSync("npm install", {
177
+ cwd: projectDir,
178
+ })
179
+
180
+ loader.stop("Project setup complete!")
181
+
182
+ console.log(`
183
+
184
+ To start working with your project:
185
+ 1. Navigate to your project directory:
186
+ cd ${projectDirName}
187
+
188
+ 2. Run the development server:
189
+ makepack serve
190
+
191
+ Enjoy your new project! 😊`);
192
+
193
+ }
194
+
195
+ export default create
@@ -0,0 +1,64 @@
1
+ import inquirer from 'inquirer'
2
+ import { createServer as createViteServer } from 'vite';
3
+ import express from 'express';
4
+
5
+ const app = express();
6
+
7
+ const serve = async (args) => {
8
+
9
+ if (args.entry === undefined) {
10
+ const { entry } = await inquirer.prompt([{
11
+ type: 'input',
12
+ name: 'entry',
13
+ message: 'Enter the root file',
14
+ default: 'src/index.js'
15
+ }]);
16
+ args.entry = entry;
17
+ }
18
+
19
+ let template = `
20
+ <!doctype html>
21
+ <html lang="en">
22
+ <head>
23
+ <meta charset="UTF-8" />
24
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
25
+ <title>Xanos</title>
26
+ </head>
27
+ <body>
28
+ <div id="root"></div>
29
+ <script type="module" src="${args.entry}"></script>
30
+ </body>
31
+ </html>
32
+ `;
33
+
34
+ const vite = await createViteServer({
35
+ root: process.cwd(),
36
+ // plugins: [react()],
37
+ server: {
38
+ middlewareMode: true
39
+ },
40
+ appType: 'custom'
41
+ });
42
+
43
+ app.use(vite.middlewares);
44
+ app.get('/', async (req, res, next) => {
45
+ const url = req.originalUrl;
46
+ try {
47
+ template = await vite.transformIndexHtml(url, template);
48
+ res.status(200).set({
49
+ 'Content-Type': 'text/html'
50
+ }).end(template);
51
+ } catch (e) {
52
+ vite.ssrFixStacktrace(e);
53
+ next(e);
54
+ }
55
+ });
56
+
57
+ app.use('*', async (_req, res) => {
58
+ res.status(404).end("404 page not found");
59
+ });
60
+
61
+ app.listen(args.port, () => console.log(`http://localhost:${args.port}`));
62
+ }
63
+
64
+ export default serve
package/src/helpers.js ADDED
@@ -0,0 +1,60 @@
1
+ import child_process from 'child_process'
2
+ import path from 'path';
3
+ import fs from 'fs';
4
+ import { fileURLToPath } from 'url'
5
+
6
+ export const packageDir = process.cwd() + "/node_modules/xanos"
7
+ const __dirname = path.dirname(fileURLToPath(import.meta.url))
8
+
9
+
10
+ export const logLoader = (message = "") => {
11
+ const spinner = ['|', '/', '-', '\\'];
12
+ let i = 0;
13
+ const interval = setInterval(() => {
14
+ process.stdout.write(`\r${message} ${spinner[i]}`);
15
+ i = (i + 1) % spinner.length;
16
+ }, 100);
17
+
18
+ return {
19
+ stop: (msg) => {
20
+ clearInterval(interval);
21
+ !!msg && console.log(`\r${msg}`);
22
+ process.stdout.write(`\r`);
23
+ }
24
+ }
25
+ }
26
+
27
+ export const exec = (command) => {
28
+ child_process.exec(command, (error, stdout, stderr) => {
29
+ if (error) {
30
+ console.error("Error:", error);
31
+ return;
32
+ }
33
+ console.log(stdout.toString());
34
+ console.error("stderr:", stderr);
35
+ });
36
+ };
37
+
38
+
39
+ export const execSync = (command, option = {}) => {
40
+ try {
41
+ const result = child_process.execSync(command, {
42
+ encoding: "utf-8",
43
+ stdio: 'inherit',
44
+ ...option
45
+ });
46
+ result && console.log(result);
47
+ } catch (error) {
48
+ console.error(`Command failed: ${error.message}`);
49
+ }
50
+ };
51
+
52
+
53
+ export const packageJson = () => {
54
+ try {
55
+ const data = fs.readFileSync('./package.json', 'utf8');
56
+ return JSON.parse(data);
57
+ } catch (error) {
58
+ console.error('Error reading package.json', error);
59
+ }
60
+ }
package/src/index.js ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from "commander";
4
+ import serve from "./actions/serve.js";
5
+ import build from "./actions/build.js";
6
+ import create from "./actions/create.js";
7
+ const program = new Command();
8
+
9
+ program.name("Make Pack").description("Usages");
10
+
11
+ program
12
+ .command("create")
13
+ .description("create a new project")
14
+ .action(create);
15
+
16
+ program
17
+ .command("serve")
18
+ .option("-p, --port <type>", "Port number", "3000")
19
+ .option("-e, --entry <type>", "entry file")
20
+ .description("Start the server")
21
+ .action(serve);
22
+
23
+ program
24
+ .command("build")
25
+ .option("-e, --entry <type>", "entry file o directory (you can use glob pattern)", "src/**/*.{tsx,ts,js,jsx}")
26
+ .option("-out, --outdir <type>", "output directory", "build")
27
+ .description("build the project")
28
+ .action(build);
29
+
30
+ program.parse();
@@ -0,0 +1,5 @@
1
+ alert()
2
+
3
+ export default () => {
4
+ return "Hello World"
5
+ }
@@ -0,0 +1,10 @@
1
+ import ReactDOM from 'react-dom/client'
2
+ import React from 'react'
3
+ import calc from '.'
4
+ const OSMain = () => {
5
+ return (
6
+ <div>{calc(2, 2)}</div>
7
+ )
8
+ }
9
+
10
+ ReactDOM.createRoot(document.getElementById('root') as any).render(<OSMain />)
package/test/index.ts ADDED
@@ -0,0 +1,8 @@
1
+
2
+ export type Calc = (a: number, b: number) => number;
3
+
4
+ const calc = (a: number, b: number) => {
5
+ return a + b;
6
+ }
7
+
8
+ export default calc;
package/test/math.ts ADDED
File without changes
package/tsconfig.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "emitDeclarationOnly": true,
5
+ "jsx": "react",
6
+ "module": "esnext",
7
+ "lib": [
8
+ "dom",
9
+ "esnext"
10
+ ],
11
+ "target": "ESNext",
12
+ "importHelpers": true,
13
+ "sourceMap": true,
14
+ "strict": true,
15
+ "noFallthroughCasesInSwitch": true,
16
+ "noUnusedLocals": true,
17
+ "noUnusedParameters": true,
18
+ "moduleResolution": "node",
19
+ "esModuleInterop": true,
20
+ "skipLibCheck": true,
21
+ "forceConsistentCasingInFileNames": true,
22
+ },
23
+ "include": [
24
+ "test/**/*"
25
+ ],
26
+ "exclude": [
27
+ "node_modules"
28
+ ]
29
+ }