makepack 1.4.0 → 1.5.1

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.
@@ -1,64 +1,64 @@
1
- // import makepack from "./files/makepack.js";
2
- import packageJson from "./files/package-json.js";
3
- import gitignore from "./files/gitignore.js";
4
- import serve from "./files/serve.js";
5
- import tsconfig from "./files/tsconfig.js";
6
- import projectJs from "./files/project-js.js";
7
- import projectJsx from "./files/project-jsx.js";
8
- import projectTs from "./files/project-ts.js";
9
- import projectTsx from "./files/project-tsx.js";
10
-
11
- import inquirer from 'inquirer'
12
- import fs from "fs-extra"
13
- import path from "path"
14
- import readmeMd from "./files/readme.md.js";
15
-
16
- export default async (args) => {
17
- const files = [
18
- packageJson(args),
19
- gitignore(args),
20
- serve(args),
21
- readmeMd(args)
22
- ];
23
-
24
- switch (args.template) {
25
- case "typescript":
26
- files.push(projectTs(args))
27
- break
28
- case "react with typescript":
29
- files.push(projectTsx(args))
30
- break;
31
- case "javascript":
32
- files.push(projectJs(args))
33
- break
34
- case "react with javascript":
35
- files.push(projectJsx(args))
36
- break;
37
- }
38
-
39
- // push ts config
40
- if (args.template.includes("typescript")) {
41
- files.push(tsconfig(args))
42
- }
43
-
44
- for (let file of files) {
45
- // check if the file exists
46
- if (fs.existsSync(path.join(args.cwd, file.filename))) {
47
- const { overwrite } = await inquirer.prompt([
48
- {
49
- type: "confirm",
50
- name: 'overwrite',
51
- message: `The file ${file.filename} already exists, do you want to overwrite it?`,
52
- default: true
53
- }
54
- ])
55
- if (!overwrite) {
56
- continue
57
- } else {
58
- fs.removeSync(path.join(args.cwd, file.filename))
59
- }
60
- }
61
-
62
- fs.writeFileSync(path.join(args.cwd, file.filename), file.content)
63
- }
64
- }
1
+ // import makepack from "./files/makepack.js";
2
+ import packageJson from "./files/package-json.js";
3
+ import gitignore from "./files/gitignore.js";
4
+ import App from "./files/App.js";
5
+ import tsconfig from "./files/tsconfig.js";
6
+ import projectJs from "./files/project-js.js";
7
+ import projectJsx from "./files/project-jsx.js";
8
+ import projectTs from "./files/project-ts.js";
9
+ import projectTsx from "./files/project-tsx.js";
10
+
11
+ import inquirer from 'inquirer'
12
+ import fs from "fs-extra"
13
+ import path from "path"
14
+ import readmeMd from "./files/readme.md.js";
15
+
16
+ export default async (info) => {
17
+ const files = [
18
+ await packageJson(info),
19
+ await gitignore(info),
20
+ await App(info),
21
+ await readmeMd(info)
22
+ ];
23
+
24
+ switch (info.template) {
25
+ case "typescript":
26
+ files.push(await projectTs(info))
27
+ break
28
+ case "react with typescript":
29
+ files.push(await projectTsx(info))
30
+ break;
31
+ case "javascript":
32
+ files.push(await projectJs(info))
33
+ break
34
+ case "react with javascript":
35
+ files.push(await projectJsx(info))
36
+ break;
37
+ }
38
+
39
+ // push ts config
40
+ if (info.template.includes("typescript")) {
41
+ files.push(await tsconfig(info))
42
+ }
43
+
44
+ for (let file of files) {
45
+ // check if the file exists
46
+ if (fs.existsSync(path.join(info.cwd, file.filename))) {
47
+ const { overwrite } = await inquirer.prompt([
48
+ {
49
+ type: "confirm",
50
+ name: 'overwrite',
51
+ message: `The file ${file.filename} already exists, do you want to overwrite it?`,
52
+ default: true
53
+ }
54
+ ])
55
+ if (!overwrite) {
56
+ continue
57
+ } else {
58
+ fs.removeSync(path.join(info.cwd, file.filename))
59
+ }
60
+ }
61
+
62
+ fs.writeFileSync(path.join(info.cwd, file.filename), file.content)
63
+ }
64
+ }
@@ -0,0 +1,19 @@
1
+ import path from 'path'
2
+ import { execSync, logger } from '../../helpers.js'
3
+ import makepackConfig from '../../makepack-config.js'
4
+
5
+ const publish = async () => {
6
+ const { build } = await makepackConfig()
7
+ const buildDir = path.join(process.cwd(), build.outdir)
8
+ const exists = fs.existsSync(buildDir)
9
+ if (!exists) {
10
+ logger.error(`Build directory ${buildDir} does not exist. Please build the project first`)
11
+ process.exit(1)
12
+ }
13
+ logger.info(`Publishing the production build to the npm repository...`)
14
+ execSync(`npm publish`, {
15
+ cwd: path.join(process.cwd(), build.outdir)
16
+ })
17
+ }
18
+
19
+ export default publish
@@ -1,97 +1,92 @@
1
- import inquirer from 'inquirer'
2
- import fs from 'fs-extra'
3
- import path from 'path'
4
- import { createServer as createViteServer } from 'vite';
5
- import express from 'express';
6
- import { glob } from 'glob'
7
- import { logger, loadConfig } from '../../helpers.js'
8
- import chalk from 'chalk';
9
- import figlet from 'figlet';
10
-
11
- const app = express();
12
-
13
-
14
-
15
- const serve = async (args) => {
16
- if (args.root === undefined) {
17
- const serveFile = await glob('serve.{ts,js,tsx,jsx}', {
18
- cwd: process.cwd()
19
- })
20
- if (!serveFile.length) {
21
- let { root } = await inquirer.prompt([{
22
- type: 'input',
23
- name: 'root',
24
- message: 'Enter the root file',
25
- }]);
26
-
27
- if (!fs.existsSync(path.join(process.cwd(), root))) {
28
- throw new Error(`invalid root: ${root}`);
29
- }
30
- args.root = root;
31
- } else {
32
- args.root = serveFile[0];
33
- }
34
- }
35
-
36
-
37
- let template = `
38
- <!doctype html>
39
- <html lang="en">
40
- <head>
41
- <meta charset="UTF-8" />
42
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
43
- </head>
44
- <body>
45
- <div id="root"></div>
46
- <script type="module" src="${args.root}"></script>
47
- </body>
48
- </html>
49
- `;
50
-
51
- let config = await loadConfig(args)
52
- let serveConfig = config.serve || {}
53
- let viteConfig = serveConfig.vite || {}
54
- let express = serveConfig.express
55
-
56
- const vite = await createViteServer(viteConfig);
57
- app.use(vite.middlewares);
58
-
59
- if (express) {
60
- express(app)
61
- }
62
-
63
- app.get('*', async (req, res, next) => {
64
- const url = req.originalUrl;
65
- try {
66
- template = await vite.transformIndexHtml(url, template);
67
- res.status(200).set({
68
- 'Content-Type': 'text/html'
69
- }).end(template);
70
- } catch (e) {
71
- vite.ssrFixStacktrace(e);
72
- next(e);
73
- }
74
- });
75
-
76
- let server = app.listen(args.port, () => {
77
- figlet("Make Pack", function (err, data) {
78
- if (err) {
79
- console.log("Something went wrong...");
80
- console.dir(err);
81
- server.close(() => {
82
- console.log('Server has been stopped.');
83
- });
84
- process.exit()
85
- }
86
- console.log(data);
87
- logger.success(`Server is running on ${chalk.blue(chalk.underline(`http://localhost:${args.port}`))}`);
88
- });
89
- });
90
-
91
- app.use((err, req, res, next) => {
92
- logger.error(`Unhandled Error: ${err.message}`);
93
- res.status(500).send('Internal Server Error');
94
- });
95
- }
96
-
97
- export default serve
1
+ import fs from 'fs-extra'
2
+ import path from 'path'
3
+ import { createServer as createViteServer } from 'vite';
4
+ import express from 'express';
5
+ import react from '@vitejs/plugin-react'
6
+ import { logger } from '../../helpers.js'
7
+ import chalk from 'chalk';
8
+ import figlet from 'figlet';
9
+ import makepackConfig from '../../makepack-config.js';
10
+
11
+ const app = express();
12
+
13
+ const start = async (args) => {
14
+ const config = await makepackConfig()
15
+ const exists = fs.existsSync(path.join(process.cwd(), config.start.entry))
16
+ if (!exists) {
17
+ logger.error(`Entry file ${entry} does not exist. please check your config file`)
18
+ process.exit(1)
19
+ }
20
+
21
+ let template = `
22
+ <!doctype html>
23
+ <html lang="en">
24
+ <head>
25
+ <meta charset="UTF-8" />
26
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
27
+ </head>
28
+ <body>
29
+ <div id="root"></div>
30
+ <script type="module" src="${config.start.entry}"></script>
31
+ </body>
32
+ </html>
33
+ `;
34
+
35
+ const viteConfig = {
36
+ root: process.cwd(),
37
+ plugins: [react()],
38
+ server: {
39
+ middlewareMode: true,
40
+ },
41
+ customLogger: {
42
+ info: (msg) => {
43
+ logger.info(msg)
44
+ },
45
+ warn: (msg) => logger.warning(msg),
46
+ error: (msg) => logger.error(msg),
47
+ },
48
+ appType: 'custom'
49
+ }
50
+
51
+ const vite = await createViteServer(viteConfig);
52
+ app.use(vite.middlewares);
53
+
54
+ if (config.start.express) {
55
+ config.start.express(app)
56
+ }
57
+
58
+ app.get('*', async (req, res, next) => {
59
+ const url = req.originalUrl;
60
+ try {
61
+ template = await vite.transformIndexHtml(url, template);
62
+ res.status(200).set({
63
+ 'Content-Type': 'text/html'
64
+ }).end(template);
65
+ } catch (e) {
66
+ vite.ssrFixStacktrace(e);
67
+ next(e);
68
+ }
69
+ });
70
+
71
+ let server = app.listen(config.start.port, () => {
72
+ figlet("Makepack", function (err, data) {
73
+ if (err) {
74
+ console.log("Something went wrong...");
75
+ console.dir(err);
76
+ server.close(() => {
77
+ console.log('Server has been stopped.');
78
+ });
79
+ process.exit()
80
+ }
81
+ console.log(data);
82
+ logger.success(`Server is running on ${chalk.blue(chalk.underline(`http://localhost:${config.start.port}`))}`);
83
+ });
84
+ });
85
+
86
+ app.use((err, req, res) => {
87
+ logger.error(`Unhandled Error: ${err.message}`);
88
+ res.status(500).send('Internal Server Error');
89
+ });
90
+ }
91
+
92
+ export default start
package/src/helpers.js CHANGED
@@ -1,122 +1,37 @@
1
- import child_process from 'child_process'
2
- import chalk from 'chalk';
3
- import figures from 'figures';
4
- import { pathToFileURL } from 'url';
5
- import path from 'path';
6
- import fs from 'fs-extra';
7
- import react from '@vitejs/plugin-react'
8
- import ts from 'typescript'
9
-
10
-
11
- export const logLoader = (message = "") => {
12
- const spinner = ['|', '/', '-', '\\'];
13
- let i = 0;
14
- const interval = setInterval(() => {
15
- process.stdout.write(`\r${message} ${spinner[i]}`);
16
- i = (i + 1) % spinner.length;
17
- }, 100);
18
-
19
- return {
20
- stop: (msg) => {
21
- clearInterval(interval);
22
- !!msg && console.log(`\r${msg}`);
23
- process.stdout.write(`\r`);
24
- }
25
- }
26
- }
27
-
28
- export const execSync = (command, option = {}) => {
29
- try {
30
- const result = child_process.execSync(command, {
31
- encoding: "utf-8",
32
- stdio: 'inherit',
33
- ...option
34
- });
35
- result && console.log(result);
36
- } catch (error) {
37
- console.error(`Command failed: ${error.message}`);
38
- process.exit(1);
39
- }
40
- };
41
-
42
-
43
-
44
- export const logger = {
45
- info: (message, prefix = 'INFO', icon = true) => {
46
- console.log(`${icon ? chalk.blue(figures.info) + " " : ""}${chalk.cyan.bold(prefix)} ${message}`);
47
- },
48
- success: (message, prefix = 'SUCCESS:', icon = true) => {
49
- console.log(`${icon ? chalk.green(figures.tick) + " " : ""}${chalk.green.bold(prefix)} ${message}`);
50
- },
51
- warning: (message, prefix = 'WARNING:', icon = true) => {
52
- console.log(`${icon ? chalk.yellow(figures.warning) + " " : ""}${chalk.yellow.bold(prefix)} ${message}`);
53
- },
54
- error: (message, prefix = 'ERROR:', icon = true) => {
55
- console.log(`${icon ? chalk.red(figures.cross) + " " : ""}${chalk.red.bold(prefix)} ${message}`);
56
- },
57
- custom: (icon, color, label, message) => {
58
- console.log(`${chalk[color](icon)} ${chalk[color].bold(`${label}:`)} ${message}`);
59
- },
60
- };
61
-
62
-
63
- export const loadConfig = async (args) => {
64
- const makepack = path.resolve(process.cwd(), "makepack.js");
65
- let esbuild = {
66
- minify: true,
67
- sourcemap: true,
68
- jsx: 'automatic',
69
- loader: {
70
- '.ts': 'ts',
71
- '.tsx': 'tsx'
72
- },
73
- }
74
-
75
- const defaultConfig = {
76
- pack: {
77
- tsconfig: {
78
- declaration: true,
79
- emitDeclarationOnly: true,
80
- outDir: path.join(process.cwd(), args.outdir || "pack", 'types'),
81
- strict: true,
82
- allowJs: true,
83
- jsx: ts.JsxEmit.React,
84
- esModuleInterop: true,
85
- },
86
- esm: esbuild,
87
- cjs: esbuild,
88
- },
89
- serve: {
90
- express: () => { },
91
- vite: {
92
- root: process.cwd(),
93
- plugins: [react()],
94
- server: {
95
- middlewareMode: true,
96
- },
97
- customLogger: {
98
- info: (msg) => {
99
- logger.info(msg)
100
- },
101
- warn: (msg) => logger.warning(msg),
102
- error: (msg) => logger.error(msg),
103
- },
104
- appType: 'custom'
105
- }
106
- }
107
- }
108
-
109
- if (fs.existsSync(makepack)) {
110
- try {
111
- const c = await import(pathToFileURL(makepack).href)
112
- const configFn = c.default
113
- if (typeof configFn === 'function') {
114
- return configFn(defaultConfig)
115
- }
116
- } catch (error) {
117
- console.log(error);
118
-
119
- }
120
- }
121
- return defaultConfig
122
- }
1
+ import child_process from 'child_process'
2
+ import chalk from 'chalk';
3
+ import figures from 'figures';
4
+
5
+ export const execSync = (command, option = {}) => {
6
+ try {
7
+ const result = child_process.execSync(command, {
8
+ encoding: "utf-8",
9
+ stdio: 'inherit',
10
+ ...option
11
+ });
12
+ result && console.log(result);
13
+ } catch (error) {
14
+ console.error(`Command failed: ${error.message}`);
15
+ process.exit(1);
16
+ }
17
+ };
18
+
19
+
20
+
21
+ export const logger = {
22
+ info: (message, prefix = 'INFO', icon = true) => {
23
+ console.log(`${icon ? chalk.blue(figures.info) + " " : ""}${chalk.cyan.bold(prefix)} ${message}`);
24
+ },
25
+ success: (message, prefix = 'SUCCESS:', icon = true) => {
26
+ console.log(`${icon ? chalk.green(figures.tick) + " " : ""}${chalk.green.bold(prefix)} ${message}`);
27
+ },
28
+ warning: (message, prefix = 'WARNING:', icon = true) => {
29
+ console.log(`${icon ? chalk.yellow(figures.warning) + " " : ""}${chalk.yellow.bold(prefix)} ${message}`);
30
+ },
31
+ error: (message, prefix = 'ERROR:', icon = true) => {
32
+ console.log(`${icon ? chalk.red(figures.cross) + " " : ""}${chalk.red.bold(prefix)} ${message}`);
33
+ },
34
+ custom: (icon, color, label, message) => {
35
+ console.log(`${chalk[color](icon)} ${chalk[color].bold(`${label}:`)} ${message}`);
36
+ },
37
+ };
package/src/index.js CHANGED
@@ -1,30 +1,33 @@
1
- #!/usr/bin/env node
2
-
3
- import { Command } from "commander";
4
- import serve from "./actions/serve/index.js";
5
- import pack from "./actions/pack/index.js";
6
- import create from "./actions/create/index.js";
7
-
8
- const program = new Command();
9
-
10
- program.name("Make Pack").description("Usages");
11
-
12
- program
13
- .command("create")
14
- .description("create a new project")
15
- .action(create);
16
-
17
- program
18
- .command("serve")
19
- .option("-p, --port <number>", "Port number", "5000")
20
- .option("-r, --root <file>", "root file")
21
- .description("Start the server")
22
- .action(serve);
23
-
24
- program
25
- .command("pack")
26
- .option("-e, --entry <file>", "Entry file or directory (you can use a glob pattern)", "src/**/*.{tsx,ts,js,jsx}")
27
- .option("-p, --publish", "Publish the project to the npm repository", false)
28
- .description("Build the project and optionally publish it to the npm repository")
29
- .action(pack);
30
- program.parse();
1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from "commander";
4
+ import start from "./actions/start/index.js";
5
+ import build from "./actions/build/index.js";
6
+ import create from "./actions/create/index.js";
7
+ import publish from "./actions/publish/index.js";
8
+
9
+ const program = new Command();
10
+
11
+ program.name("Makepack").description("Usages");
12
+
13
+ program
14
+ .command("create")
15
+ .description("create a new project")
16
+ .action(create);
17
+
18
+ program
19
+ .command("start")
20
+ .description("Start the server")
21
+ .action(start);
22
+
23
+ program
24
+ .command("build")
25
+ .description("Build the project")
26
+ .action(build);
27
+
28
+ program
29
+ .command("publish")
30
+ .description("Publish it to the npm repository")
31
+ .action(publish);
32
+
33
+ program.parse();
@@ -0,0 +1,66 @@
1
+ import path from 'path'
2
+ import fs from 'fs-extra'
3
+ import { pathToFileURL } from 'url';
4
+
5
+ const makepackConfig = async () => {
6
+ const makepack = path.resolve(process.cwd(), "makepack.js");
7
+
8
+ const defaultConfig = {
9
+ build: {
10
+ outdir: "build",
11
+ types: true,
12
+ formatPackageJson: (p) => p,
13
+ configs: [
14
+ {
15
+ entryPoints: "src/**/*.{tsx,ts,js,jsx}",
16
+ outdir: "",
17
+ format: "esm",
18
+ sourcemap: true,
19
+ minify: true,
20
+ jsx: 'automatic',
21
+ loader: {
22
+ '.ts': 'ts',
23
+ '.tsx': 'tsx'
24
+ },
25
+ },
26
+ {
27
+ entryPoints: "src/**/*.{tsx,ts,js,jsx}",
28
+ outdir: "cjs",
29
+ format: "cjs",
30
+ sourcemap: true,
31
+ minify: true,
32
+ jsx: 'automatic',
33
+ loader: {
34
+ '.ts': 'ts',
35
+ '.tsx': 'tsx'
36
+ },
37
+ }
38
+ ]
39
+ },
40
+ start: {
41
+ port: 5000,
42
+ entry: "App.tsx",
43
+ express: (_app) => { }
44
+ }
45
+ }
46
+
47
+ if (fs.existsSync(makepack)) {
48
+ try {
49
+ const c = await import(pathToFileURL(makepack).href)
50
+ const configFn = c.default
51
+ if (typeof configFn === 'function') {
52
+ const nc = configFn(defaultConfig)
53
+ if (!nc) {
54
+ console.log("Config function must return a config object")
55
+ process.exit(1)
56
+ }
57
+ return nc
58
+ }
59
+ } catch (error) {
60
+ console.log(error);
61
+ }
62
+ }
63
+ return defaultConfig
64
+ }
65
+
66
+ export default makepackConfig
@@ -1,19 +0,0 @@
1
- export default (args) => {
2
- const makepack = {
3
- "template": `${args.template}`,
4
- "serve": {
5
- "port": 3000,
6
- "entry": `${args.rootdir}/${args.entry}`
7
- },
8
- "build": {
9
- "entry": `${args.rootdir}/**/*.{tsx,ts,js,jsx}`,
10
- "outdir": `${args.outdir}`,
11
- "esbuild": {
12
- }
13
- },
14
- }
15
- return {
16
- content: `const makepack = ${JSON.stringify(makepack, null, 2)}\nexport default makepack\n`,
17
- filename: "makepack.js"
18
- }
19
- }