create-hyperbook 0.2.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/LICENSE.md +21 -0
- package/README.md +64 -0
- package/dist/create-hyperbook.d.mts +1 -0
- package/dist/create-hyperbook.d.ts +2 -0
- package/dist/create-hyperbook.mjs +2 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +181 -0
- package/dist/index.js.map +7 -0
- package/dist/templates/default/archives/project-1/index.md +0 -0
- package/dist/templates/default/archives/project-2/bla.c +0 -0
- package/dist/templates/default/archives/project-2/main.c +0 -0
- package/dist/templates/default/book/index.md +11 -0
- package/dist/templates/default/book/logo.png +0 -0
- package/dist/templates/default/gitignore +3 -0
- package/dist/templates/default/glossary/hyperbook.md +11 -0
- package/package.json +33 -0
- package/templates/default/archives/project-1/index.md +0 -0
- package/templates/default/archives/project-2/bla.c +0 -0
- package/templates/default/archives/project-2/main.c +0 -0
- package/templates/default/book/index.md +11 -0
- package/templates/default/book/logo.png +0 -0
- package/templates/default/gitignore +3 -0
- package/templates/default/glossary/hyperbook.md +11 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Mike Barkmin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# create-hyperbook <a href="https://npmjs.com/package/create-hyperbook"><img src="https://img.shields.io/npm/v/create-hyperbook" alt="npm package"></a>
|
|
2
|
+
|
|
3
|
+
## Scaffolding Your First Hyperbook Project
|
|
4
|
+
|
|
5
|
+
> **Compatibility Note:**
|
|
6
|
+
> Hyperbook requires [Node.js](https://nodejs.org/en/) version 20+. Please upgrade if your package manager warns about it.
|
|
7
|
+
|
|
8
|
+
With NPM:
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm create hyperbook@latest
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
With Yarn:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
yarn create hyperbook
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
With PNPM:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm create hyperbook
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
With Bun:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
bun create hyperbook
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Then follow the prompts!
|
|
33
|
+
|
|
34
|
+
You can also directly specify the project name via the command line. For example, to scaffold a Hyperbook project named "my-documentation", run:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
# npm 7+, extra double-dash is needed:
|
|
38
|
+
npm create hyperbook@latest my-documentation
|
|
39
|
+
|
|
40
|
+
# yarn
|
|
41
|
+
yarn create hyperbook my-documentation
|
|
42
|
+
|
|
43
|
+
# pnpm
|
|
44
|
+
pnpm create hyperbook my-documentation
|
|
45
|
+
|
|
46
|
+
# Bun
|
|
47
|
+
bun create hyperbook my-documentation
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Currently supported template:
|
|
51
|
+
|
|
52
|
+
- `default` - A basic Hyperbook project with sample content
|
|
53
|
+
|
|
54
|
+
You can use `.` for the project name to scaffold in the current directory.
|
|
55
|
+
|
|
56
|
+
## What is Hyperbook?
|
|
57
|
+
|
|
58
|
+
Hyperbook is a tool for building interactive workbooks and documentation. It provides features like:
|
|
59
|
+
|
|
60
|
+
- Custom markdown directives for rich content
|
|
61
|
+
- Embedded code execution environments
|
|
62
|
+
- Multi-language support
|
|
63
|
+
- Interactive elements (Excalidraw, videos, alerts, and more)
|
|
64
|
+
- Built-in navigation and search
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import e from"chalk";import t from"prompts";import n from"os";import r from"path";import i from"fs";import{execSync as a}from"child_process";import{fileURLToPath as o}from"url";async function s(e){try{return await i.promises.access(e,(i.constants||i).W_OK),!0}catch{return!1}}async function c(e,t={recursive:!0}){await i.promises.mkdir(e,t)}function l(e){let t=[`.DS_Store`,`.git`,`.gitattributes`,`.gitignore`,`.gitlab-ci.yml`,`.hg`,`.hgcheck`,`.hgignore`,`.idea`,`.npmignore`,`.travis.yml`,`LICENSE`,`Thumbs.db`,`npm-debug.log`,`yarn-debug.log`,`yarn-error.log`];return i.readdirSync(e).filter(e=>!t.includes(e)).filter(e=>!/\.iml$/.test(e)).length===0}function u(e){try{a(`git --version`,{stdio:`ignore`});try{return a(`git rev-parse --is-inside-work-tree`,{stdio:`ignore`}),!1}catch{}try{return a(`hg --cwd . root`,{stdio:`ignore`}),!1}catch{}return a(`git init`,{stdio:`ignore`,cwd:e}),a(`git checkout -b main`,{stdio:`ignore`,cwd:e}),a(`git add -A`,{stdio:`ignore`,cwd:e}),a(`git commit -m "Initial commit from Hyperbook"`,{stdio:`ignore`,cwd:e}),!0}catch{return!1}}async function d(e){let{bookPath:t,name:a,description:o=``,author:d=``,authorUrl:p=``,license:m=`cc-by`,language:h=`en`,template:g=`default`}=e;if(!t||t.trim()===``)return{success:!1,root:``,bookName:``,error:`Book path is required`};let _=r.resolve(t.trim());if(!await s(r.dirname(_)))return{success:!1,root:_,bookName:``,error:`The book path is not writable, please check folder permissions.`};let v=a||r.basename(_);try{await c(_)}catch(e){return{success:!1,root:_,bookName:v,error:`Failed to create directory: ${e}`}}if(!l(_))return{success:!1,root:_,bookName:v,error:`Directory is not empty`};let y={name:v,version:`0.0.0`,logo:`/logo.png`,description:o,license:m,author:{name:d,url:p},language:h};try{i.writeFileSync(r.join(_,`hyperbook.json`),JSON.stringify(y,null,2)+n.EOL)}catch(e){return{success:!1,root:_,bookName:v,error:`Failed to write hyperbook.json: ${e}`}}try{let e=g?r.join(__dirname,`./templates/${g}`):r.join(__dirname,`./templates/default`);i.existsSync(e)&&await f(e,_)}catch(e){return{success:!1,root:_,bookName:v,error:`Failed to copy template files: ${e}`}}return u(_),{success:!0,root:_,bookName:v}}async function f(e,t){let n=await i.promises.readdir(e,{withFileTypes:!0});for(let a of n){let n=r.join(e,a.name),o=r.join(t,a.name);a.name===`gitignore`?o=r.join(t,`.gitignore`):a.name===`README-template.md`&&(o=r.join(t,`README.md`)),a.isDirectory()?(await i.promises.mkdir(o,{recursive:!0}),await f(n,o)):await i.promises.copyFile(n,o)}}const p=o(import.meta.url),m=r.dirname(p);async function h(){console.log(),console.log(e.bold(`Welcome to Hyperbook!`)),console.log();let n=process.argv[2];if(typeof n==`string`&&(n=n.trim()),!n){let e=await t({type:`text`,name:`path`,message:`What is your book named?`,initial:`my-book`});typeof e.path==`string`&&(n=e.path.trim())}n||(console.log(),console.log(`Please specify the book directory:`),console.log(` ${e.cyan(`npm create hyperbook`)} ${e.green(`<book-directory>`)}`),console.log(),console.log(`For example:`),console.log(` ${e.cyan(`npm create hyperbook`)} ${e.green(`my-new-book`)}`),console.log(),process.exit(1));let{description:a}=await t({type:`text`,name:`description`,message:`What is your book about?`,initial:``}),{author:o}=await t({type:`text`,name:`author`,message:`Who is the author of the book?`,initial:``}),{authorUrl:s}=await t({type:`text`,name:`authorUrl`,message:`What is the link to the authors homepage?`,initial:``}),{license:c}=await t({type:`select`,name:`license`,message:`Pick a license for your book!`,choices:[{title:`Creative Commons Zero (CC0)`,value:`cc0`},{title:`Creative Commons Attribution (CC BY)`,value:`cc-by`},{title:`Creative Commons Attribution-ShareAlike (CC BY-SA)`,value:`cc-by-sa`},{title:`Creative Commons Attribution-NoDerivs (CC BY-ND)`,value:`cc-by-nd`},{title:`Creative Commons Attribution-NonCommercial (CC BY-NC)`,value:`cc-by-nc`},{title:`Creative Commons Attribution-NonCommercial-ShareAlike (CC BY-NC-SA)`,value:`cc-by-nc-sa`},{title:`Creative Commons Attribution-NonCommercial-NoDervis (CC BY-NC-ND)`,value:`cc-by-nc-nd`},{title:`Custom`,value:`custom`}]});c===`custom`&&(c=(await t({type:`text`,name:`license`,message:`Which custom license you want to use?`})).license);let{language:l}=await t({type:`text`,name:`language`,message:`In which language is your book written? Provide a short code, e.g.: en or de`,initial:`en`}),u=r.join(m,`templates`),f=[`default`];try{i.existsSync(u)&&(f=i.readdirSync(u,{withFileTypes:!0}).filter(e=>e.isDirectory()).map(e=>e.name))}catch{f=[`default`]}let p=`default`;if(f.length>1){let{selectedTemplate:e}=await t({type:`select`,name:`selectedTemplate`,message:`Which template would you like to use?`,choices:f.map(e=>({title:e.charAt(0).toUpperCase()+e.slice(1),value:e})),initial:0});p=e||`default`}console.log(),console.log(`Creating a new hyperbook...`),console.log();let h=await d({bookPath:n,description:a,author:o,authorUrl:s,license:c,language:l,template:p});h.success||(console.error(e.red(`Error creating hyperbook:`),h.error),process.exit(1)),console.log(`${e.green(`Success!`)} Created ${h.bookName} at ${h.root}`),console.log(`Inside that directory, you can run several commands:`),console.log(),console.log(e.cyan(` hyperbook dev`)),console.log(` Starts the development server.`),console.log(),console.log(e.cyan(` hyperbook build`)),console.log(` Builds the book for production.`),console.log(),console.log(`We suggest that you begin by typing:`),console.log();let g=process.cwd(),_=h.bookName,v;v=g&&h.root.startsWith(g)?_:h.root,console.log(e.cyan(` cd`),v),console.log(` ${e.cyan(`hyperbook dev`)}`),console.log()}h().catch(t=>{console.error(e.red(`An error occurred:`),t),process.exit(1)});export{};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface CreateHyperbookOptions {
|
|
2
|
+
bookPath: string;
|
|
3
|
+
name?: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
author?: string;
|
|
6
|
+
authorUrl?: string;
|
|
7
|
+
license?: string;
|
|
8
|
+
language?: string;
|
|
9
|
+
template?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface CreateHyperbookResult {
|
|
12
|
+
success: boolean;
|
|
13
|
+
root: string;
|
|
14
|
+
bookName: string;
|
|
15
|
+
error?: string;
|
|
16
|
+
}
|
|
17
|
+
export declare function isWriteable(directory: string): Promise<boolean>;
|
|
18
|
+
export declare function makeDir(root: string, options?: {
|
|
19
|
+
recursive: boolean;
|
|
20
|
+
}): Promise<void>;
|
|
21
|
+
export declare function isFolderEmpty(root: string): boolean;
|
|
22
|
+
export declare function tryGitInit(root: string): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Create a new Hyperbook project
|
|
25
|
+
* @param options - Configuration options for the new project
|
|
26
|
+
* @returns Result indicating success or failure
|
|
27
|
+
*/
|
|
28
|
+
export declare function createHyperbook(options: CreateHyperbookOptions): Promise<CreateHyperbookResult>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import os from "os";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import { execSync } from "child_process";
|
|
6
|
+
async function isWriteable(directory) {
|
|
7
|
+
try {
|
|
8
|
+
await fs.promises.access(directory, (fs.constants || fs).W_OK);
|
|
9
|
+
return true;
|
|
10
|
+
} catch (err) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
async function makeDir(root, options = { recursive: true }) {
|
|
15
|
+
await fs.promises.mkdir(root, options);
|
|
16
|
+
}
|
|
17
|
+
function isFolderEmpty(root) {
|
|
18
|
+
const validFiles = [
|
|
19
|
+
".DS_Store",
|
|
20
|
+
".git",
|
|
21
|
+
".gitattributes",
|
|
22
|
+
".gitignore",
|
|
23
|
+
".gitlab-ci.yml",
|
|
24
|
+
".hg",
|
|
25
|
+
".hgcheck",
|
|
26
|
+
".hgignore",
|
|
27
|
+
".idea",
|
|
28
|
+
".npmignore",
|
|
29
|
+
".travis.yml",
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"Thumbs.db",
|
|
32
|
+
"npm-debug.log",
|
|
33
|
+
"yarn-debug.log",
|
|
34
|
+
"yarn-error.log"
|
|
35
|
+
];
|
|
36
|
+
const conflicts = fs.readdirSync(root).filter((file) => !validFiles.includes(file)).filter((file) => !/\.iml$/.test(file));
|
|
37
|
+
return conflicts.length === 0;
|
|
38
|
+
}
|
|
39
|
+
function tryGitInit(root) {
|
|
40
|
+
try {
|
|
41
|
+
execSync("git --version", { stdio: "ignore" });
|
|
42
|
+
try {
|
|
43
|
+
execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" });
|
|
44
|
+
return false;
|
|
45
|
+
} catch (_) {
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
execSync("hg --cwd . root", { stdio: "ignore" });
|
|
49
|
+
return false;
|
|
50
|
+
} catch (_) {
|
|
51
|
+
}
|
|
52
|
+
execSync("git init", { stdio: "ignore", cwd: root });
|
|
53
|
+
execSync("git checkout -b main", { stdio: "ignore", cwd: root });
|
|
54
|
+
execSync("git add -A", { stdio: "ignore", cwd: root });
|
|
55
|
+
execSync('git commit -m "Initial commit from Hyperbook"', {
|
|
56
|
+
stdio: "ignore",
|
|
57
|
+
cwd: root
|
|
58
|
+
});
|
|
59
|
+
return true;
|
|
60
|
+
} catch (e) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function createHyperbook(options) {
|
|
65
|
+
const {
|
|
66
|
+
bookPath,
|
|
67
|
+
name,
|
|
68
|
+
description = "",
|
|
69
|
+
author = "",
|
|
70
|
+
authorUrl = "",
|
|
71
|
+
license = "cc-by",
|
|
72
|
+
language = "en",
|
|
73
|
+
template = "default"
|
|
74
|
+
} = options;
|
|
75
|
+
if (!bookPath || bookPath.trim() === "") {
|
|
76
|
+
return {
|
|
77
|
+
success: false,
|
|
78
|
+
root: "",
|
|
79
|
+
bookName: "",
|
|
80
|
+
error: "Book path is required"
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const root = path.resolve(bookPath.trim());
|
|
84
|
+
if (!await isWriteable(path.dirname(root))) {
|
|
85
|
+
return {
|
|
86
|
+
success: false,
|
|
87
|
+
root,
|
|
88
|
+
bookName: "",
|
|
89
|
+
error: "The book path is not writable, please check folder permissions."
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const bookName = name || path.basename(root);
|
|
93
|
+
try {
|
|
94
|
+
await makeDir(root);
|
|
95
|
+
} catch (err) {
|
|
96
|
+
return {
|
|
97
|
+
success: false,
|
|
98
|
+
root,
|
|
99
|
+
bookName,
|
|
100
|
+
error: `Failed to create directory: ${err}`
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
if (!isFolderEmpty(root)) {
|
|
104
|
+
return {
|
|
105
|
+
success: false,
|
|
106
|
+
root,
|
|
107
|
+
bookName,
|
|
108
|
+
error: "Directory is not empty"
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
const hyperbookJson = {
|
|
112
|
+
name: bookName,
|
|
113
|
+
version: "0.0.0",
|
|
114
|
+
logo: "/logo.png",
|
|
115
|
+
description,
|
|
116
|
+
license,
|
|
117
|
+
author: {
|
|
118
|
+
name: author,
|
|
119
|
+
url: authorUrl
|
|
120
|
+
},
|
|
121
|
+
language
|
|
122
|
+
};
|
|
123
|
+
try {
|
|
124
|
+
fs.writeFileSync(
|
|
125
|
+
path.join(root, "hyperbook.json"),
|
|
126
|
+
JSON.stringify(hyperbookJson, null, 2) + os.EOL
|
|
127
|
+
);
|
|
128
|
+
} catch (err) {
|
|
129
|
+
return {
|
|
130
|
+
success: false,
|
|
131
|
+
root,
|
|
132
|
+
bookName,
|
|
133
|
+
error: `Failed to write hyperbook.json: ${err}`
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const templateDir = template ? path.join(__dirname, `./templates/${template}`) : path.join(__dirname, "./templates/default");
|
|
138
|
+
if (fs.existsSync(templateDir)) {
|
|
139
|
+
await copyTemplateFiles(templateDir, root);
|
|
140
|
+
}
|
|
141
|
+
} catch (err) {
|
|
142
|
+
return {
|
|
143
|
+
success: false,
|
|
144
|
+
root,
|
|
145
|
+
bookName,
|
|
146
|
+
error: `Failed to copy template files: ${err}`
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
tryGitInit(root);
|
|
150
|
+
return {
|
|
151
|
+
success: true,
|
|
152
|
+
root,
|
|
153
|
+
bookName
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
async function copyTemplateFiles(from, to) {
|
|
157
|
+
const entries = await fs.promises.readdir(from, { withFileTypes: true });
|
|
158
|
+
for (const entry of entries) {
|
|
159
|
+
const srcPath = path.join(from, entry.name);
|
|
160
|
+
let destPath = path.join(to, entry.name);
|
|
161
|
+
if (entry.name === "gitignore") {
|
|
162
|
+
destPath = path.join(to, ".gitignore");
|
|
163
|
+
} else if (entry.name === "README-template.md") {
|
|
164
|
+
destPath = path.join(to, "README.md");
|
|
165
|
+
}
|
|
166
|
+
if (entry.isDirectory()) {
|
|
167
|
+
await fs.promises.mkdir(destPath, { recursive: true });
|
|
168
|
+
await copyTemplateFiles(srcPath, destPath);
|
|
169
|
+
} else {
|
|
170
|
+
await fs.promises.copyFile(srcPath, destPath);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
export {
|
|
175
|
+
createHyperbook,
|
|
176
|
+
isFolderEmpty,
|
|
177
|
+
isWriteable,
|
|
178
|
+
makeDir,
|
|
179
|
+
tryGitInit
|
|
180
|
+
};
|
|
181
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["import os from \"os\";\nimport path from \"path\";\nimport fs from \"fs\";\nimport { execSync } from \"child_process\";\n\nexport interface CreateHyperbookOptions {\n bookPath: string;\n name?: string;\n description?: string;\n author?: string;\n authorUrl?: string;\n license?: string;\n language?: string;\n template?: string;\n}\n\nexport interface CreateHyperbookResult {\n success: boolean;\n root: string;\n bookName: string;\n error?: string;\n}\n\nexport async function isWriteable(directory: string): Promise<boolean> {\n try {\n await fs.promises.access(directory, (fs.constants || fs).W_OK);\n return true;\n } catch (err) {\n return false;\n }\n}\n\nexport async function makeDir(\n root: string,\n options = { recursive: true },\n): Promise<void> {\n await fs.promises.mkdir(root, options);\n}\n\nexport function isFolderEmpty(root: string): boolean {\n const validFiles = [\n \".DS_Store\",\n \".git\",\n \".gitattributes\",\n \".gitignore\",\n \".gitlab-ci.yml\",\n \".hg\",\n \".hgcheck\",\n \".hgignore\",\n \".idea\",\n \".npmignore\",\n \".travis.yml\",\n \"LICENSE\",\n \"Thumbs.db\",\n \"npm-debug.log\",\n \"yarn-debug.log\",\n \"yarn-error.log\",\n ];\n\n const conflicts = fs\n .readdirSync(root)\n .filter((file) => !validFiles.includes(file))\n .filter((file) => !/\\.iml$/.test(file));\n\n return conflicts.length === 0;\n}\n\nexport function tryGitInit(root: string): boolean {\n try {\n execSync(\"git --version\", { stdio: \"ignore\" });\n\n // Check if already in a git repository\n try {\n execSync(\"git rev-parse --is-inside-work-tree\", { stdio: \"ignore\" });\n return false;\n } catch (_) {\n // Not in a git repo, continue\n }\n\n // Check if in mercurial repository\n try {\n execSync(\"hg --cwd . root\", { stdio: \"ignore\" });\n return false;\n } catch (_) {\n // Not in hg repo, continue\n }\n\n execSync(\"git init\", { stdio: \"ignore\", cwd: root });\n execSync(\"git checkout -b main\", { stdio: \"ignore\", cwd: root });\n execSync(\"git add -A\", { stdio: \"ignore\", cwd: root });\n execSync('git commit -m \"Initial commit from Hyperbook\"', {\n stdio: \"ignore\",\n cwd: root,\n });\n return true;\n } catch (e) {\n return false;\n }\n}\n\n/**\n * Create a new Hyperbook project\n * @param options - Configuration options for the new project\n * @returns Result indicating success or failure\n */\nexport async function createHyperbook(\n options: CreateHyperbookOptions,\n): Promise<CreateHyperbookResult> {\n const {\n bookPath,\n name,\n description = \"\",\n author = \"\",\n authorUrl = \"\",\n license = \"cc-by\",\n language = \"en\",\n template = \"default\",\n } = options;\n\n if (!bookPath || bookPath.trim() === \"\") {\n return {\n success: false,\n root: \"\",\n bookName: \"\",\n error: \"Book path is required\",\n };\n }\n\n const root = path.resolve(bookPath.trim());\n\n // Check if directory is writeable\n if (!(await isWriteable(path.dirname(root)))) {\n return {\n success: false,\n root,\n bookName: \"\",\n error: \"The book path is not writable, please check folder permissions.\",\n };\n }\n\n const bookName = name || path.basename(root);\n\n // Create directory\n try {\n await makeDir(root);\n } catch (err) {\n return {\n success: false,\n root,\n bookName,\n error: `Failed to create directory: ${err}`,\n };\n }\n\n // Check if folder is empty\n if (!isFolderEmpty(root)) {\n return {\n success: false,\n root,\n bookName,\n error: \"Directory is not empty\",\n };\n }\n\n // Create hyperbook.json\n const hyperbookJson = {\n name: bookName,\n version: \"0.0.0\",\n logo: \"/logo.png\",\n description: description,\n license,\n author: {\n name: author,\n url: authorUrl,\n },\n language: language,\n };\n\n try {\n fs.writeFileSync(\n path.join(root, \"hyperbook.json\"),\n JSON.stringify(hyperbookJson, null, 2) + os.EOL,\n );\n } catch (err) {\n return {\n success: false,\n root,\n bookName,\n error: `Failed to write hyperbook.json: ${err}`,\n };\n }\n\n // Copy template files\n try {\n const templateDir = template\n ? path.join(__dirname, `./templates/${template}`)\n : path.join(__dirname, \"./templates/default\");\n\n if (fs.existsSync(templateDir)) {\n await copyTemplateFiles(templateDir, root);\n }\n } catch (err) {\n return {\n success: false,\n root,\n bookName,\n error: `Failed to copy template files: ${err}`,\n };\n }\n\n // Initialize git repository\n tryGitInit(root);\n\n return {\n success: true,\n root,\n bookName,\n };\n}\n\nasync function copyTemplateFiles(from: string, to: string): Promise<void> {\n const entries = await fs.promises.readdir(from, { withFileTypes: true });\n\n for (const entry of entries) {\n const srcPath = path.join(from, entry.name);\n let destPath = path.join(to, entry.name);\n\n // Handle special file renames\n if (entry.name === \"gitignore\") {\n destPath = path.join(to, \".gitignore\");\n } else if (entry.name === \"README-template.md\") {\n destPath = path.join(to, \"README.md\");\n }\n\n if (entry.isDirectory()) {\n await fs.promises.mkdir(destPath, { recursive: true });\n await copyTemplateFiles(srcPath, destPath);\n } else {\n await fs.promises.copyFile(srcPath, destPath);\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,OAAO,QAAQ;AACf,SAAS,gBAAgB;AAoBzB,eAAsB,YAAY,WAAqC;AACrE,MAAI;AACF,UAAM,GAAG,SAAS,OAAO,YAAY,GAAG,aAAa,IAAI,IAAI;AAC7D,WAAO;AAAA,EACT,SAAS,KAAK;AACZ,WAAO;AAAA,EACT;AACF;AAEA,eAAsB,QACpB,MACA,UAAU,EAAE,WAAW,KAAK,GACb;AACf,QAAM,GAAG,SAAS,MAAM,MAAM,OAAO;AACvC;AAEO,SAAS,cAAc,MAAuB;AACnD,QA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EACF,SAAS,KAAK;AACZ,WAAO;AAAA,MACL,SAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA,OAAO,kCAAkC,GAAG;AAAA,IAC9C;AAAA,EACF;AAGA,aAAW,IAAI;AAEf,SAAO;AAAA,IACL,SAAS;AAAA,IACT;AAAA,IACA;AAAA,EACF;AACF;AAEA,eAAe,kBAAkB,MAAc,IAA2B;AACxE,QAAM,UAAU,MAAM,GAAG,SAAS,QAAQ,MAAM,EAAE,eAAe,KAAK,CAAC;AAEvE,aAAW,SAAS,SAAS;AAC3B,UAAM,UAAU,KAAK,KAAK,MAAM,MAAM,IAAI;AAC1C,QAAI,WAAW,KAAK,KAAK,IAAI,MAAM,IAAI;AAGvC,QAAI,MAAM,SAAS,aAAa;AAC9B,iBAAW,KAAK,KAAK,IAAI,YAAY;AAAA,IACvC,WAAW,MAAM,SAAS,sBAAsB;AAC9C,iBAAW,KAAK,KAAK,IAAI,WAAW;AAAA,IACtC;AAEA,QAAI,MAAM,YAAY,GAAG;AACvB,YAAM,GAAG,SAAS,MAAM,UAAU,EAAE,WAAW,KAAK,CAAC;AACrD,YAAM,kBAAkB,SAAS,QAAQ;AAAA,IAC3C,OAAO;AACL,YAAM,GAAG,SAAS,SAAS,SAAS,QAAQ;AAAA,IAC9C;AAAA,EACF;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-hyperbook",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Shared logic for creating new Hyperbook projects",
|
|
5
|
+
"platform": "node",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"create-hyperbook": "./dist/create-hyperbook.mjs"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"templates"
|
|
14
|
+
],
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"chalk": "^4.1.2",
|
|
17
|
+
"prompts": "^2.4.2",
|
|
18
|
+
"@hyperbook/types": "0.18.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/prompts": "^2.4.9",
|
|
22
|
+
"rimraf": "6.0.1",
|
|
23
|
+
"tsdown": "^0.16.6",
|
|
24
|
+
"typescript": "5.7.3",
|
|
25
|
+
"vitest": "3.0.5"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "rimraf dist && pnpm build:create && pnpm build:pkg && pnpm build:types && node postbuild.mjs",
|
|
29
|
+
"build:pkg": "node ../../scripts/build.mjs",
|
|
30
|
+
"build:create": "tsdown",
|
|
31
|
+
"build:types": "tsc --project tsconfig.build.json --declaration --emitDeclarationOnly"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
Binary file
|