makepack 1.5.24 → 1.6.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.
@@ -1,106 +1,104 @@
1
- import { execSync, logger } from "../../helpers.js"
2
- import makeFiles from "./makeFiles.js"
3
- import path from 'path'
4
- import inquirer from 'inquirer'
5
- import figlet from 'figlet'
6
- import fs from "fs-extra"
7
- const cwd = process.cwd()
8
- const cwdFolder = cwd.split(path.sep).pop()
9
-
10
- const create = async () => {
11
- const information = {
12
- projectDirName: cwdFolder,
13
- cwd: path.join(cwd, cwdFolder),
14
- template: "typescript",
15
- sourceDir: "src",
16
- sourceEntry: "index.ts",
17
- }
18
-
19
- let { projectDirName } = await inquirer.prompt([
20
- {
21
- type: 'input',
22
- name: 'projectDirName',
23
- message: 'Enter the project name',
24
- default: information.projectDir
25
- }
26
- ])
27
-
28
- if (projectDirName !== cwdFolder) {
29
- if (fs.existsSync(path.join(cwd, projectDirName))) {
30
- const { proceed } = await inquirer.prompt([
31
- {
32
- type: "confirm",
33
- name: 'proceed',
34
- message: "The directory already exists, do you want to overwrite it?",
35
- default: "No"
36
- }
37
- ])
38
- if (!proceed) {
39
- console.log('Project creation canceled.');
40
- return
41
- }
42
- }
43
- }
44
-
45
- information.projectDirName = projectDirName
46
- information.cwd = path.join(cwd, information.projectDirName)
47
- let isCurrentDir = projectDirName !== cwdFolder
48
-
49
- // template
50
- const { template } = await inquirer.prompt([
51
- {
52
- type: 'list',
53
- name: 'template',
54
- message: 'Select a template',
55
- choices: ['typescript', 'javascript', 'react with typescript', 'react with javascript'],
56
- default: information.template
57
- }
58
- ])
59
-
60
- information.template = template
61
-
62
- logger.info("", "Creating project...", false)
63
- const projectDir = path.join(cwd, information.projectDirName)
64
-
65
- if (!fs.existsSync(projectDir)) {
66
- fs.mkdirSync(projectDir)
67
- }
68
-
69
- if (!fs.existsSync(path.join(projectDir, information.sourceDir))) {
70
- fs.mkdirSync(path.join(projectDir, information.sourceDir))
71
- }
72
-
73
- switch (information.template) {
74
- case "react with typescript":
75
- information.sourceEntry = "index.tsx"
76
- break;
77
- case "react with javascript":
78
- information.sourceEntry = "index.jsx"
79
- break;
80
- case "javascript":
81
- information.sourceEntry = "index.js"
82
- break;
83
- }
84
-
85
- await makeFiles(information)
86
-
87
- logger.info("", "Installing Dependencies", false)
88
- execSync("npm install", {
89
- cwd: information.cwd,
90
- })
91
-
92
- logger.info("Project setup complete!", "", false)
93
- if (isCurrentDir) {
94
- console.log(`Run the development server: \n${logger.info("", "npm start", false)}\nEnjoy your new project! 😊`);
95
- } else {
96
- console.log(`To start working with your project:\nNavigate to your project directory:\n${logger.info("", "cd " + information.projectDirName, false)} and Run the development server:\n${logger.info("", "npm start", false)}\nEnjoy your new project! 😊`);
97
- }
98
-
99
- figlet("Makepack CLI", function (err, data) {
100
- if (!err) {
101
- console.log(data);
102
- }
103
- });
104
- }
105
-
1
+ import { execSync, logger } from "../../helpers.js"
2
+ import makeFiles from "./makeFiles.js"
3
+ import path from 'path'
4
+ import inquirer from 'inquirer'
5
+ import figlet from 'figlet'
6
+ import fs from "fs-extra"
7
+ import chalk from 'chalk';
8
+ const cwd = process.cwd()
9
+ const cwdFolder = cwd.split(path.sep).pop()
10
+
11
+ const valiateProjectName = (name) => {
12
+ if (!name) {
13
+ console.error("Project name cannot be empty.");
14
+ return false;
15
+ }
16
+ if (!/^[a-z0-9-]+$/.test(name)) {
17
+ console.error("Project name can only contain lowercase letters, numbers, and hyphens.");
18
+ return false;
19
+ }
20
+ if (name.length < 3) {
21
+ console.error("Project name must be at least 3 characters long.");
22
+ return false;
23
+ }
24
+ if (name.length > 50) {
25
+ console.error("Project name must be less than 50 characters long.");
26
+ return false;
27
+ }
28
+ return true;
29
+ }
30
+
31
+ const create = async () => {
32
+
33
+ let info = await inquirer.prompt([
34
+ {
35
+ type: 'input',
36
+ name: 'pdir',
37
+ message: 'Enter the project name',
38
+ default: cwdFolder
39
+ },
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
+ // check if the pdir is exists
50
+ let pdir = info.pdir.trim().replace(/\s+/g, '-').toLowerCase();
51
+ const isValidProjectName = valiateProjectName(pdir)
52
+ if (!isValidProjectName) return
53
+
54
+ if (pdir !== cwdFolder) {
55
+ if (fs.existsSync(path.join(cwd, pdir))) {
56
+ const { proceed } = await inquirer.prompt([
57
+ {
58
+ type: "confirm",
59
+ name: 'proceed',
60
+ message: "The directory already exists, do you want to overwrite it?",
61
+ default: "No"
62
+ }
63
+ ])
64
+ if (!proceed) {
65
+ console.log('Project creation canceled.');
66
+ return
67
+ }
68
+ }
69
+ }
70
+
71
+ const rootdir = path.join(cwd, pdir)
72
+ let isCurrentDir = pdir !== cwdFolder
73
+ logger.info("", "Creating project...", false)
74
+
75
+ if (!fs.existsSync(rootdir)) {
76
+ fs.mkdirSync(rootdir)
77
+ }
78
+
79
+ if (!fs.existsSync(path.join(rootdir, 'src'))) {
80
+ fs.mkdirSync(path.join(rootdir, 'src'))
81
+ }
82
+
83
+ await makeFiles(info)
84
+
85
+ logger.info("", "Installing Dependencies", false)
86
+ execSync("npm install", {
87
+ cwd: rootdir,
88
+ })
89
+
90
+ logger.success("Project setup complete!", "")
91
+ if (isCurrentDir) {
92
+ console.log(`Run the development server: ${chalk.blue("npm start")}\nEnjoy your new project! 😊`);
93
+ } else {
94
+ console.log(`Navigate to your project directory:\n${chalk.blue("cd " + info.pdir, false)} and Run the development server: ${chalk.blue("npm start")}\nEnjoy your new project! 😊`);
95
+ }
96
+
97
+ figlet("Makepack CLI", function (err, data) {
98
+ if (!err) {
99
+ console.log(data);
100
+ }
101
+ });
102
+ }
103
+
106
104
  export default create
@@ -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 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
- }
1
+ // import makepack from "./files/makepack.js";
2
+ import packageJson from "./files/package-json.js";
3
+ import gitignore from "./files/gitignore.js";
4
+ import tsconfig from "./files/tsconfig.js";
5
+ import projectJs from "./files/project-js.js";
6
+ import projectJsx from "./files/project-jsx.js";
7
+ import projectTs from "./files/project-ts.js";
8
+ import projectTsx from "./files/project-tsx.js";
9
+
10
+ import inquirer from 'inquirer'
11
+ import fs from "fs-extra"
12
+ import path from "path"
13
+ import readmeMd from "./files/readme.md.js";
14
+
15
+ export default async (info) => {
16
+ const files = [
17
+ await packageJson(info),
18
+ await gitignore(info),
19
+ await readmeMd(info)
20
+ ];
21
+
22
+ switch (info.template) {
23
+ case "typescript":
24
+ files.push(await projectTs(info))
25
+ break
26
+ case "react with typescript":
27
+ files.push(await projectTsx(info))
28
+ break;
29
+ case "javascript":
30
+ files.push(await projectJs(info))
31
+ break
32
+ case "react with javascript":
33
+ files.push(await projectJsx(info))
34
+ break;
35
+ }
36
+
37
+ // push ts config
38
+ if (info.template.includes("typescript")) {
39
+ files.push(await tsconfig(info))
40
+ }
41
+
42
+ const rootdir = path.join(process.cwd(), info.pdir)
43
+
44
+ for (let file of files) {
45
+ // check if the file exists
46
+ if (fs.existsSync(path.join(rootdir, 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(rootdir, file.filename))
59
+ }
60
+ }
61
+
62
+ fs.writeFileSync(path.join(rootdir, file.filename), file.content)
63
+ }
64
+ }
@@ -1,21 +1,20 @@
1
- import path from 'path'
2
- import { execSync, logger } from '../../helpers.js'
3
- import makepackConfig from '../../makepack-config.js'
4
- import fs from 'fs-extra'
5
-
6
- const publish = async () => {
7
- const { build } = await makepackConfig()
8
- const buildDir = path.join(process.cwd(), build.outdir)
9
- const exists = fs.existsSync(buildDir)
10
- if (!exists) {
11
- logger.error(`Build directory ${buildDir} does not exist. Please build the project first`)
12
- process.exit(1)
13
- }
14
-
15
- logger.info(`Publishing the production build to the npm repository...`)
16
- execSync(`npm publish`, {
17
- cwd: buildDir
18
- })
19
- }
20
-
1
+ import path from 'path'
2
+ import { execSync, logger } from '../../helpers.js'
3
+ import fs from 'fs-extra'
4
+
5
+ const publish = async () => {
6
+ const buildDir = path.join(process.cwd(), '.mpack')
7
+ const packageJsonPath = path.join(buildDir, 'package.json')
8
+ const exists = fs.existsSync(buildDir)
9
+ if (!exists || !fs.existsSync(packageJsonPath)) {
10
+ logger.error(`Project is not built yet. Please build the project first.`)
11
+ process.exit(1)
12
+ }
13
+
14
+ logger.info(`Publishing the production build to the npm repository...`)
15
+ execSync(`npm publish`, {
16
+ cwd: buildDir
17
+ })
18
+ }
19
+
21
20
  export default publish
@@ -1,26 +1,43 @@
1
- import express from 'express';
2
- import { logger } from '../../helpers.js'
3
- import chalk from 'chalk';
4
- import makepackConfig from '../../makepack-config.js';
5
- import viteSetup from './vite.js';
6
- import userExpress from './user-express.js';
7
-
8
- const app = express();
9
- const server = async () => {
10
- const config = await makepackConfig()
11
- userExpress(app)
12
- await viteSetup(app)
13
- app.use((_req, res) => {
14
- res.status(500).send('Internal Server Error');
15
- });
16
-
17
- app.listen(config.start.port, () => {
18
- logger.success(`Server is running on ${chalk.blue(chalk.underline(`http://localhost:${config.start.port}`))}`);
19
- });
20
-
21
- process.on('SIGINT', async () => {
22
- process.exit(0);
23
- });
24
- }
25
-
1
+ import express from 'express';
2
+ import { logger } from '../../helpers.js'
3
+ import chalk from 'chalk';
4
+ import path from 'path'
5
+ import fs from 'fs'
6
+ import viteSetup from './vite.js';
7
+ const port = process.env.PORT || 3000;
8
+ const app = express();
9
+ const server = async () => {
10
+
11
+ // get type from package.json
12
+ const pkg = path.join(process.cwd(), 'package.json');
13
+ let type = 'module';
14
+ if (fs.existsSync(pkg)) {
15
+ const pkgjson = JSON.parse(fs.readFileSync(pkg, 'utf-8'));
16
+ type = pkgjson.type || 'module';
17
+ }
18
+
19
+ const mpack = path.join(process.cwd(), '.mpack');
20
+ const uxp = path.join(mpack, 'uxp.js')
21
+ if (fs.existsSync(uxp)) {
22
+ // load user-express.js based on type
23
+ if (type === 'module') {
24
+ const { default: userExpress } = await import(uxp);
25
+ userExpress(app);
26
+ } else {
27
+ const userExpress = require(uxp).default;
28
+ userExpress(app);
29
+ }
30
+ }
31
+
32
+
33
+ await viteSetup(app)
34
+ app.use((_req, res) => {
35
+ res.status(500).send('Internal Server Error');
36
+ });
37
+
38
+ app.listen(port, () => {
39
+ logger.success(`Server is running on ${chalk.blue(chalk.underline(`http://localhost:${port}`))}`);
40
+ });
41
+ }
42
+
26
43
  server()
@@ -1,77 +1,118 @@
1
- import fs from 'fs-extra'
2
- import path from 'path'
3
- import { logger } from '../../helpers.js'
4
- import makepackConfig from '../../makepack-config.js';
5
- import esbuild from 'esbuild';
6
- import chokidar from 'chokidar';
7
- import { fileURLToPath } from 'url';
8
- import { spawn } from 'child_process';
9
- let __filename, __dirname;
10
-
11
- if (typeof import.meta.url !== 'undefined') {
12
- __filename = fileURLToPath(import.meta.url);
13
- __dirname = path.dirname(__filename);
14
- } else {
15
- __filename = __filename;
16
- __dirname = __dirname;
17
- }
18
-
19
- let server = null;
20
- function startServer() {
21
- if (server) {
22
- server.kill('SIGINT');
23
- server = null;
24
- }
25
- server = spawn('node', [path.resolve(__dirname, 'express.js')], {});
26
- server.stdout.on('data', (data) => {
27
- console.log(data.toString().trim());
28
- });
29
- server.stderr.on('data', (data) => {
30
- console.error(data.toString().trim());
31
- });
32
- }
33
-
34
- const start = async () => {
35
- const config = await makepackConfig()
36
- const exists = fs.existsSync(path.join(process.cwd(), config.start.entry))
37
- if (!exists) {
38
- logger.error(`Entry file ${entry} does not exist. please check your config file`)
39
- process.exit(1)
40
- }
41
-
42
- const expressjs = path.join(process.cwd(), 'express.js')
43
- const expressts = path.join(process.cwd(), 'express.ts')
44
-
45
- if (fs.existsSync(expressjs) || fs.existsSync(expressts)) {
46
- let filename = fs.existsSync(expressjs) ? "express.js" : "express.ts";
47
- let outfile = path.resolve(__dirname, 'user-express.js')
48
-
49
- const ctx = await esbuild.context({
50
- entryPoints: [filename],
51
- outfile: path.resolve(__dirname, 'user-express.js'),
52
- bundle: true,
53
- format: 'esm',
54
- platform: 'node',
55
- packages: 'external',
56
- })
57
-
58
- ctx.watch()
59
-
60
- const watcher = chokidar.watch(outfile, {
61
- persistent: true,
62
- ignoreInitial: true,
63
- });
64
-
65
- watcher.on('change', async () => {
66
- startServer(config)
67
- });
68
-
69
- process.on('SIGINT', async () => {
70
- watcher.close();
71
- process.exit(0);
72
- });
73
- }
74
- startServer(config)
75
- }
76
-
1
+ import path from 'path'
2
+ import fs from 'fs'
3
+ import { spawn } from 'child_process'
4
+ import esbuild from 'esbuild';
5
+ import { fileURLToPath } from 'url';
6
+ import chokidar from 'chokidar';
7
+
8
+ let __filename, __dirname;
9
+
10
+ if (typeof import.meta.url !== 'undefined') {
11
+ __filename = fileURLToPath(import.meta.url);
12
+ __dirname = path.dirname(__filename);
13
+ } else {
14
+ __filename = __filename;
15
+ __dirname = __dirname;
16
+ }
17
+
18
+
19
+ const startServer = () => {
20
+ const mpack = path.join(process.cwd(), '.mpack')
21
+ const server = spawn('node', [path.resolve(mpack, 'index.js')], {
22
+ stdio: 'inherit',
23
+ });
24
+
25
+ server.on('error', (err) => {
26
+ console.error(`Error starting server: ${err.message}`);
27
+ });
28
+ return server;
29
+ }
30
+
31
+ const start = async (args) => {
32
+ let { port } = args
33
+
34
+ if (!port) {
35
+ port = 4000;
36
+ }
37
+ // create a folder call .mpack
38
+ const mpack = path.join(process.cwd(), '.mpack')
39
+ if (fs.existsSync(mpack)) {
40
+ fs.rmSync(mpack, { recursive: true, force: true });
41
+ }
42
+ fs.mkdirSync(mpack)
43
+
44
+
45
+ // build ./express.js to .mpack/index.js with esbuild
46
+ let format = 'esm'
47
+ // get format from package.json
48
+ const pkg = path.join(process.cwd(), 'package.json')
49
+ if (fs.existsSync(pkg)) {
50
+ const pkgjson = JSON.parse(fs.readFileSync(pkg, 'utf-8'))
51
+ if (pkgjson.type === 'commonjs') {
52
+ format = 'cjs'
53
+ }
54
+ }
55
+
56
+ const uxpjs = path.join(process.cwd(), 'express.js')
57
+ const uxpts = path.join(process.cwd(), 'express.ts')
58
+
59
+ if (fs.existsSync(uxpjs) || fs.existsSync(uxpts)) {
60
+ let filename = fs.existsSync(uxpjs) ? "express.js" : "express.ts";
61
+ let outfile = path.join(mpack, 'uxp.js');
62
+
63
+ const ctx = await esbuild.context({
64
+ entryPoints: [filename],
65
+ outfile,
66
+ bundle: true,
67
+ format: 'esm',
68
+ platform: 'node',
69
+ packages: 'external',
70
+ })
71
+ ctx.watch()
72
+ }
73
+
74
+ await esbuild.build({
75
+ entryPoints: [path.resolve(__dirname, 'express.js')],
76
+ outfile: path.join(mpack, 'index.js'),
77
+ bundle: true,
78
+ format,
79
+ platform: 'node',
80
+ packages: 'external',
81
+ define: {
82
+ 'process.env.PORT': JSON.stringify(port),
83
+ },
84
+ })
85
+
86
+ let server = startServer();
87
+
88
+ const uxp = path.join(mpack, 'uxp.js')
89
+
90
+ if (fs.existsSync(uxp)) {
91
+ const watcher = chokidar.watch(uxp, {
92
+ persistent: true,
93
+ ignoreInitial: true,
94
+ });
95
+
96
+ watcher.on('change', async () => {
97
+ // restart the server and remove log Server exited with code
98
+ server.kill();
99
+ server = startServer();
100
+ });
101
+ }
102
+
103
+ process.on('SIGINT', () => {
104
+ console.log('Received SIGINT, killing server...');
105
+ server.kill('SIGINT');
106
+ process.exit(0);
107
+ });
108
+
109
+ process.on('SIGTERM', () => {
110
+ console.log('Received SIGTERM, killing server...');
111
+ server.kill('SIGTERM');
112
+ process.exit(0);
113
+ });
114
+
115
+ }
116
+
117
+
77
118
  export default start