hoosat-create 1.4.4 → 1.4.6

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.
Files changed (3) hide show
  1. package/README.md +35 -3
  2. package/index.js +166 -102
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -8,6 +8,31 @@ Hoosat create is a `npx` script that creates React 18.2 full-stack server side r
8
8
  npx hoosat-create@latest
9
9
  ```
10
10
 
11
+ ### Commandline arguments
12
+ - `submodule`
13
+ - `--destination`
14
+ - `--author`
15
+ - `--license`
16
+ - `--keywords`
17
+ - `--keywoordsList`
18
+ - `--gitInit`
19
+ - `--hoosatui`
20
+
21
+ For example:
22
+
23
+ ```
24
+ npx hoosat-create@latest --destination "new_project" --author "Toni Lukkaroinen" --gitInit Y --hoosatui Y
25
+ ```
26
+
27
+ Now it will ask the rest like `license` in the process. Which you can default just by pressing enter.
28
+
29
+ ### Submodule creation
30
+
31
+ The following command creates an empty git submodule for you to start working on code that will be shared with another project later.
32
+ ```
33
+ npx hoosat-create@latest submodule --destination "src/client/submodule"
34
+ ```
35
+
11
36
  ## Technologies Used
12
37
 
13
38
  - React-helmet-async 1.3
@@ -71,9 +96,8 @@ router.Middleware(assets("./public"));
71
96
 
72
97
  // Creating a ping route
73
98
  // For request and response we dont use wrappers for node http types and methods.
74
- router.Get("/ping", (req: IncomingMessage, res: ServerResponse) => {
75
- res.writeHead(20, { 'Content-Type': "text/plain" });
76
- res.end("pong!");
99
+ router.Get("/ping", (req: HoosatRequest, res: HoosatResponse) => {
100
+ res.status(200).json("pong!");
77
101
  });
78
102
 
79
103
  // Now that the middleware and routes have been set the server must be created that handles the routes.
@@ -97,6 +121,14 @@ Though for now the only goal seems to be to drop `react-helmet-async`.
97
121
 
98
122
  ## Release changes
99
123
 
124
+ ### 1.4.5 -> 1.4.6
125
+ - Use submodule for server, make cloning of template recursive.
126
+
127
+ ### 1.4.4 -> 1.4.5
128
+ - One small document change in server code example, to make your life easier.
129
+ - Add support for command line arguments.
130
+ - Add support for npx hoosat-create submodule
131
+
100
132
  ### 1.4.3 -> 1.4.4
101
133
  - Why the heck I removed that #!/usr/bin/env node.. Now it's back.
102
134
 
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import fs from 'fs';
2
3
  import { spawn } from 'child_process';
3
4
  import { createInterface } from 'readline';
4
5
  import { readFileSync, writeFileSync } from 'fs';
@@ -11,116 +12,179 @@ const rl = createInterface({
11
12
  });
12
13
 
13
14
  async function run() {
14
- const destination = await askQuestion('Enter destination project name: ');
15
- const description = await askQuestion('Enter project description: ');
16
- const author = await askQuestion('Enter project author: ');
17
- const license = await askQuestion('Enter project license: ');
18
- const keywords = await askQuestion('Enter project keywords (comma-separated): ');
19
- const keywordList = keywords.split(',').map(k => k.trim()).filter(k => k !== '');
20
- const gitInit = await askYesNoQuestion('Do you want to initialize a git repository? [Y/n] ');
21
- let installHoosatUI = undefined;
22
- if (gitInit) {
23
- installHoosatUI = await askYesNoQuestion('Do you want to install HoosatUI? [Y/n] ');
24
- }
25
- try {
26
- // Clone git repository.
27
- const clone = spawn('git', ['clone', repository, destination]);
28
- await new Promise((resolve, reject) => {
29
- clone.on('close', code => {
30
- if (code === 0) {
31
- resolve();
32
- } else {
33
- reject(new Error('Failed to clone project.'));
34
- }
35
- });
36
- });
37
- console.log('Project cloned successfully');
38
- // Change directory to the project directory
15
+ const command = process.argv[2];
16
+
17
+ if (command === 'submodule') {
18
+ console.log('Creating a submodule for project.');
19
+ const rootDirectory = process.cwd();
20
+ //const name = getValueFromArgument('name') || (await askQuestion('Enter name for new submodule: '));
21
+ const destination = getValueFromArgument('destination') || (await askQuestion('Enter relative destination for new submodule: '));
22
+ try {
23
+ await runCommand(`git rev-parse --is-inside-work-tree`, "Current working directory is not git repository.");
24
+ } catch (error) {
25
+ console.error(error.message);
26
+ throw error;
27
+ }
28
+ // mkdir submodule-destination
29
+ try {
30
+ await fs.promises.mkdir(destination, { recursive: true });
31
+ console.log("Created submodule folder.");
32
+ } catch (error) {
33
+ console.error(error.message);
34
+ }
35
+ // cd submodule-destination
36
+ // git init
37
+ // cd back to root of project
38
+ // git commit -m 'Added empty submodule'
39
39
  process.chdir(destination);
40
- // Modify the package.json
41
- const packageJsonPath = join(process.cwd(), 'package.json');
42
- const packageJsonContent = readFileSync(packageJsonPath, 'utf8');
43
- const packageJson = JSON.parse(packageJsonContent);
44
- packageJson.name = destination;
45
- packageJson.description = description;
46
- packageJson.author = author;
47
- packageJson.license = license;
48
- packageJson.keywords = keywordList;
49
- writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
50
- console.log('Project package.json has been modified.');
51
- // Do NPM install for dependencies.
52
- const install = spawn('npm', ['install'], { stdio: 'inherit' });
53
- await new Promise((resolve, reject) => {
54
- install.on('close', code => {
55
- if (code === 0) {
56
- resolve();
57
- } else {
58
- reject(new Error('Failed to install project dependencies.'));
59
- }
60
- });
61
- });
62
- console.log('npm install completed');
40
+ try {
41
+ await runCommand(`git init`, 'Failed to initialize git for submodule.');
42
+ console.log("Initialized git repository.");
43
+ } catch (error) {
44
+ console.error(error, error.message);
45
+ }
46
+ try {
47
+ await runCommand(`touch ./README.md`, 'Failed to create README.md file');
48
+ console.log("CreatedREADME.md file for commit.");
49
+ } catch (error) {
50
+ console.error(error.message);
51
+ }
52
+ try {
53
+ await runCommand(`git add ./README.md`, 'Failed to stage README.md.');
54
+ console.log("Staged README.md");
55
+ } catch (error) {
56
+ console.error(error.message);
57
+ }
58
+ try {
59
+ await runCommand(`git commit -m 'Add readme.'`, 'Failed to commit.');
60
+ console.log("Committed readme.");
61
+ } catch (error) {
62
+ console.error(error.message);
63
+ }
64
+ // Change back to the root of the project
65
+ process.chdir(rootDirectory);
66
+ // git submodule add ./submodule-destination submodule-name
67
+ try {
68
+ await runCommand(`git submodule add ./${destination}`, 'Failed to add the new local repository as git submodule');
69
+ console.log("Added local repository as git submodule to parent git repository.");
70
+ } catch (error) {
71
+ console.error(error, error.message);
72
+ }
73
+ // git commit -m 'Added empty submodule'
74
+ // try {
75
+ // await runCommand(`git commit -m 'Added ${name} submodule into ${destination}'`, 'Failed to commit changes, do it manually');
76
+ // console.log("Committed changes in parent repository.");
77
+ // } catch (error) {
78
+ // console.error(error, error.message);
79
+ // }
80
+ console.log("Now commit your changes to git.");
81
+ rl.close();
82
+ } else {
83
+ console.log('Creating a new project from Hoosat React SSR Template.');
84
+ const destination = getValueFromArgument('destination') || (await askQuestion('Enter destination project name: '));
85
+ const description = getValueFromArgument('description') || (await askQuestion('Enter project description: '));
86
+ const author = getValueFromArgument('author') || (await askQuestion('Enter project author: '));
87
+ const license = getValueFromArgument('license') || (await askQuestion('Enter project license: '));
88
+ const keywords = getValueFromArgument('keywords') || (await askQuestion('Enter project keywords (comma-separated): '));
89
+ const keywordList = keywords.split(',').map(k => k.trim()).filter(k => k !== '');
90
+ const gitInit = getValueFromArgument('gitInit') || (await askYesNoQuestion('Do you want to initialize a git repository? [Y/n] '));
91
+ let installHoosatUI = undefined;
63
92
  if (gitInit) {
64
- // Initialize git repository
65
- const gitInit = spawn('git', ['init']);
66
- await new Promise((resolve, reject) => {
67
- gitInit.on('close', code => {
68
- if (code === 0) {
69
- resolve();
70
- } else {
71
- reject(new Error('Failed to initialize git repository.'));
93
+ installHoosatUI = getValueFromArgument('hoosatui') || await askYesNoQuestion('Do you want to install HoosatUI? [Y/n] ');
94
+ }
95
+ try {
96
+ // Clone git repository.
97
+ try {
98
+ await runCommand(`git clone --recursive ${repository} ${destination}`, 'Failed to clone project.', { stdio: 'inherit' });
99
+ console.log('Project cloned successfully.');
100
+ } catch (error) {
101
+ console.error(error.message);
102
+ }
103
+ // Change directory to the project directory
104
+ process.chdir(destination);
105
+ // Modify the package.json
106
+ const packageJsonPath = join(process.cwd(), 'package.json');
107
+ const packageJsonContent = readFileSync(packageJsonPath, 'utf8');
108
+ const packageJson = JSON.parse(packageJsonContent);
109
+ packageJson.name = destination;
110
+ packageJson.description = description;
111
+ packageJson.author = author;
112
+ packageJson.license = license;
113
+ packageJson.keywords = keywordList;
114
+ writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
115
+ console.log('Project package.json has been modified.');
116
+ // Do NPM install for dependencies.
117
+ try {
118
+ await runCommand('npm install', 'Failed to install project template dependencies.', { stdio: 'inherit' });
119
+ console.log('Project template dependancies installed with npm.');
120
+ } catch (error) {
121
+ console.error(error.message);
122
+ }
123
+ if (gitInit) {
124
+ // Initialize git repository
125
+ try {
126
+ await runCommand('git init', 'Failed to initialize git repository.', { stdio: 'inherit' });
127
+ console.log('Git has been initialized.');
128
+ } catch (error) {
129
+ console.error(error.message);
130
+ }
131
+ console.log('Git repository initialized.');
132
+ if (installHoosatUI) {
133
+ // Change directory to the client directory
134
+ process.chdir('src/client');
135
+ // Clone HoosatUI repository
136
+ try {
137
+ await runCommand('git submodule add https://github.com/hoosat-oy/HoosatUI', 'Failed to add submodule HoosatUI.', { stdio: 'inherit' });
138
+ console.log('HoosatUI cloned successfully as submodule in src/client/HoosatUI.');
139
+ } catch (error) {
140
+ console.error(error.message);
141
+ }
142
+ // Install necessary dev-dependencies
143
+ try {
144
+ await runCommand('npm install --save-dev @testing-library/jest-dom @testing-library/react', 'Failed to install HoosatUI devDependancies.', { stdio: 'inherit' });
145
+ console.log('npm HoosatUI devDependencies installed successfully.');
146
+ } catch (error) {
147
+ console.error(error.message);
72
148
  }
73
- });
74
- });
75
- console.log('Git repository initialized.');
76
- if (installHoosatUI) {
77
- // Change directory to the client directory
78
- process.chdir('src/client');
79
- // Clone HoosatUI repository
80
- const hoosatUIClone = spawn('git', ['clone', 'https://github.com/hoosat-oy/HoosatUI']);
81
- await new Promise((resolve, reject) => {
82
- hoosatUIClone.on('close', code => {
83
- if (code === 0) {
84
- resolve();
85
- } else {
86
- reject(new Error('Failed to clone HoosatUI.'));
87
- }
88
- });
89
- });
90
- console.log('HoosatUI cloned successfully.');
91
- // Install necessary dev-dependencies
92
- const installHoosatUIDevDeps = spawn('npm', ['install', '--save-dev', '@testing-library/jest-dom', '@testing-library/react'], { stdio: 'inherit' });
93
- await new Promise((resolve, reject) => {
94
- installHoosatUIDevDeps.on('close', code => {
95
- if (code === 0) {
96
- resolve();
97
- } else {
98
- reject(new Error('Failed to install HoosatUI devDependencies.'));
99
- }
100
- });
101
- });
102
- console.log('npm HoosatUI devDependencies installed successfully.');
103
- // Install necessary dependencies.
104
- const installHoosatUIDeps = spawn('npm', ['install', 'react-markdown', 'rehype-highlight', 'remark-gfm'], { stdio: 'inherit' });
105
- await new Promise((resolve, reject) => {
106
- installHoosatUIDeps.on('close', code => {
107
- if (code === 0) {
108
- resolve();
109
- } else {
110
- reject(new Error('Failed to install HoosatUI dependencies.'));
111
- }
112
- });
113
- });
114
- console.log('npm HoosatUI dependencies installed successfully.');
149
+ // Install necessary dependencies.
150
+ try {
151
+ await runCommand('npm install react-markdown rehype-highlight remark-gfm', 'Failed to install HoosatUI dependencies.', { stdio: 'inherit' });
152
+ console.log('npm HoosatUI dependencies installed successfully.');
153
+ } catch (error) {
154
+ console.error(error.message);
155
+ }
156
+ }
115
157
  }
158
+ } catch (err) {
159
+ console.error(`Failed to clone project: ${err}`);
160
+ } finally {
161
+ rl.close();
116
162
  }
117
- } catch (err) {
118
- console.error(`Failed to clone project: ${err}`);
119
- } finally {
120
- rl.close();
121
163
  }
122
164
  }
123
165
 
166
+ async function runCommand(commandString, errorMessage, spawnOptions) {
167
+ return new Promise((resolve, reject) => {
168
+ const commandProcess = spawn(commandString, [], { shell: true, ...spawnOptions });
169
+ commandProcess.on('close', code => {
170
+ if (code === 0) {
171
+ resolve();
172
+ } else {
173
+ reject(new Error(commandString + "\r\n" + errorMessage));
174
+ }
175
+ });
176
+ });
177
+ }
178
+
179
+
180
+ function getValueFromArgument(argName) {
181
+ const argIndex = process.argv.findIndex(arg => arg === `--${argName}`);
182
+ if (argIndex !== -1 && argIndex + 1 < process.argv.length) {
183
+ return process.argv[argIndex + 1];
184
+ }
185
+ return undefined;
186
+ }
187
+
124
188
  function askQuestion(question) {
125
189
  return new Promise((resolve, reject) => {
126
190
  rl.question(question, answer => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hoosat-create",
3
3
  "type": "module",
4
- "version": "1.4.4",
4
+ "version": "1.4.6",
5
5
  "description": "Hoosat - React & Typescript SSR project template.",
6
6
  "homepage": "https://example.com",
7
7
  "author": "Toni Lukkaroinen @ Hoosat Oy",