create-docus 1.0.4 → 1.1.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 CHANGED
@@ -8,14 +8,45 @@ Add `docus` package globally:
8
8
 
9
9
  ```bash
10
10
  # Using Yarn
11
- yarn install --global docus
11
+ yarn global add docus
12
+
12
13
  # Using NPM
13
14
  npm install --global docus
14
15
  ```
15
16
 
16
17
  ## Commands
17
18
 
18
- TODO
19
+ ### `npm init docus`
20
+
21
+ This package is also responsible for the `npm init docus` command.
22
+
23
+ You can use it without installing `docus` package globally.
24
+
25
+ `--` is required as specified [here](https://docs.npmjs.com/cli/v7/commands/npm-init#forwarding-additional-options).
26
+
27
+ ```bash
28
+ npm init docus -- path/to/project
29
+
30
+ npm init docus -- --type="module" path/to/project
31
+
32
+ npm init docus -- --type="theme" path/to/project
33
+ ```
34
+
35
+ ### `create-docus`
36
+
37
+ If you install the `docus` package globally, it will be shipped with `create-docus` executable.
38
+
39
+ This allows the creation of a [Docus project](https://github.com/docusgen/starter), a [theme](https://github.com/docusgen/theme-starter) or a [module](https://github.com/docusgen/module-starter).
40
+
41
+ You can execute this command anywhere:
42
+
43
+ ```bash
44
+ create-docus path/to/project
45
+
46
+ create-docus --type="module" path/to/project
47
+
48
+ create-docus --type="theme" path/to/project
49
+ ```
19
50
 
20
51
  ## Development
21
52
 
@@ -1,4 +1,4 @@
1
- #\!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
  // #!/usr/bin/env node
3
3
  "use strict";
4
4
 
@@ -0,0 +1,246 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.args = void 0;
7
+ exports.createDir = createDir;
8
+ exports.createProject = createProject;
9
+ exports.cwd = void 0;
10
+ exports.getProjectType = getProjectType;
11
+ exports.getTargetDir = getTargetDir;
12
+ exports.getTemplate = getTemplate;
13
+ exports.getValidPackageName = getValidPackageName;
14
+ exports.pkgManager = exports.log = void 0;
15
+
16
+ var _package = require("../../package.json");
17
+
18
+ var _fs = _interopRequireDefault(require("fs"));
19
+
20
+ var _path = _interopRequireDefault(require("path"));
21
+
22
+ var _kolorist = require("kolorist");
23
+
24
+ var _minimist = _interopRequireDefault(require("minimist"));
25
+
26
+ var _prompts = _interopRequireDefault(require("prompts"));
27
+
28
+ var _degit = _interopRequireDefault(require("degit"));
29
+
30
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
+
32
+ const cwd = process.cwd();
33
+ exports.cwd = cwd;
34
+ const args = (0, _minimist.default)(process.argv.slice(2));
35
+ exports.args = args;
36
+ const pkgManager = /pnpm/.test(process.env.npm_execpath || "") || /pnpm/.test(process.env.npm_config_user_agent || "") ? "pnpm" : /yarn/.test(process.env.npm_execpath || "") ? "yarn" : "npm";
37
+ exports.pkgManager = pkgManager;
38
+ const log = {
39
+ space: () => console.log(),
40
+ bold: str => (0, _kolorist.bold)(str),
41
+ primary: str => (0, _kolorist.green)(str),
42
+ secondary: str => (0, _kolorist.dim)(str),
43
+ warning: str => (0, _kolorist.yellow)(str),
44
+ error: str => (0, _kolorist.red)(str),
45
+ broadcast: str => console.log(` ${str}`),
46
+ motd: () => {
47
+ log.space();
48
+ log.broadcast(`${(0, _kolorist.bold)("Docus") + log.secondary(" CLI")} ${log.primary(`v${_package.version}`)}`);
49
+ log.space();
50
+ }
51
+ };
52
+ exports.log = log;
53
+ const REPOSITORIES = {
54
+ website: "git@github.com:docusgen/starter.git",
55
+ theme: "git@github.com:docusgen/theme-starter.git",
56
+ module: "git@github.com:docusgen/module-starter.git"
57
+ };
58
+
59
+ function copyDir(srcDir, destDir) {
60
+ _fs.default.mkdirSync(destDir, {
61
+ recursive: true
62
+ });
63
+
64
+ for (const file of _fs.default.readdirSync(srcDir)) {
65
+ const srcFile = _path.default.resolve(srcDir, file);
66
+
67
+ const destFile = _path.default.resolve(destDir, file);
68
+
69
+ copy(srcFile, destFile);
70
+ }
71
+ }
72
+
73
+ function emptyDir(dir) {
74
+ if (!_fs.default.existsSync(dir)) return;
75
+
76
+ for (const file of _fs.default.readdirSync(dir)) {
77
+ const abs = _path.default.resolve(dir, file);
78
+
79
+ if (_fs.default.lstatSync(abs).isDirectory()) {
80
+ emptyDir(abs);
81
+
82
+ _fs.default.rmdirSync(abs);
83
+ } else {
84
+ _fs.default.unlinkSync(abs);
85
+ }
86
+ }
87
+ }
88
+
89
+ function copy(src, dest) {
90
+ const stat = _fs.default.statSync(src);
91
+
92
+ if (stat.isDirectory()) copyDir(src, dest);else _fs.default.copyFileSync(src, dest);
93
+ }
94
+
95
+ function getProjectType() {
96
+ switch (args["type"]) {
97
+ case "theme":
98
+ return "theme";
99
+
100
+ case "module":
101
+ return "module";
102
+
103
+ default:
104
+ return "website";
105
+ }
106
+ }
107
+
108
+ async function getTemplate(projectType) {
109
+ const {
110
+ broadcast,
111
+ primary,
112
+ secondary
113
+ } = log;
114
+
115
+ const templateDir = _path.default.join(__dirname, "./template");
116
+
117
+ broadcast([secondary("Cloning the latest"), primary(projectType === "website" ? "Docus" : projectType), secondary("template...")].join(" "));
118
+ emptyDir(templateDir);
119
+ const projectRepository = REPOSITORIES[projectType];
120
+ const repo = (0, _degit.default)(projectRepository, {
121
+ force: true,
122
+ verbose: true,
123
+ mode: "git"
124
+ });
125
+ await repo.clone(templateDir);
126
+ return templateDir;
127
+ }
128
+
129
+ async function createDir(targetDir) {
130
+ const projectPath = _path.default.join(cwd, targetDir);
131
+
132
+ const {
133
+ broadcast,
134
+ warning,
135
+ error,
136
+ space,
137
+ secondary
138
+ } = log;
139
+
140
+ if (!_fs.default.existsSync(projectPath)) {
141
+ _fs.default.mkdirSync(projectPath, {
142
+ recursive: true
143
+ });
144
+ } else {
145
+ const existing = _fs.default.readdirSync(projectPath);
146
+
147
+ const dir = projectPath.split("/");
148
+ const dirName = dir[dir.length - 1];
149
+
150
+ if (existing.length) {
151
+ space();
152
+ broadcast(warning(`Target directory "${dirName}" is not empty.`));
153
+ space();
154
+ const {
155
+ yes
156
+ } = await (0, _prompts.default)({
157
+ type: "confirm",
158
+ name: "yes",
159
+ initial: "Y",
160
+ message: "Remove existing files and continue?"
161
+ });
162
+ if (yes) emptyDir(projectPath);else {
163
+ space();
164
+ broadcast(warning(`Target directory is not empty and you do not want to overwrite it.`));
165
+ space();
166
+ broadcast(error(`Cancelling project creation.`));
167
+ process.exit(1);
168
+ }
169
+ }
170
+ }
171
+
172
+ space();
173
+ broadcast(secondary("Scaffolding project in ") + targetDir + secondary("..."));
174
+ return projectPath;
175
+ }
176
+
177
+ async function getValidPackageName(projectName) {
178
+ projectName = _path.default.basename(projectName);
179
+ const packageNameRegExp = /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
180
+
181
+ if (packageNameRegExp.test(projectName)) {
182
+ return projectName;
183
+ } else {
184
+ const suggestedPackageName = projectName.trim().toLowerCase().replace(/\s+/g, "-").replace(/^[._]/, "").replace(/[^a-z0-9-~]+/g, "-");
185
+ const {
186
+ inputPackageName
187
+ } = await (0, _prompts.default)({
188
+ type: "text",
189
+ name: "inputPackageName",
190
+ message: "Package name:",
191
+ initial: suggestedPackageName,
192
+ validate: input => packageNameRegExp.test(input) ? true : "Invalid package.json name"
193
+ });
194
+ return inputPackageName;
195
+ }
196
+ }
197
+
198
+ async function getTargetDir(projectType) {
199
+ const {
200
+ space,
201
+ broadcast,
202
+ error,
203
+ warning
204
+ } = log;
205
+ let targetDir = args._[0];
206
+
207
+ if (!targetDir) {
208
+ const {
209
+ projectName
210
+ } = await (0, _prompts.default)({
211
+ type: "text",
212
+ name: "projectName",
213
+ message: "Project name:",
214
+ initial: `my-${projectType}`
215
+ });
216
+
217
+ if (!projectName) {
218
+ space();
219
+ broadcast(warning(`You must specify a project name.`));
220
+ space();
221
+ broadcast(error(`Cancelling project creation.`));
222
+ process.exit(1);
223
+ }
224
+
225
+ targetDir = projectName.trim();
226
+ }
227
+
228
+ return targetDir;
229
+ }
230
+
231
+ function createProject(root, templateDir, packageName) {
232
+ const write = (file, content = void 0) => {
233
+ const targetPath = _path.default.join(root, file);
234
+
235
+ if (content) _fs.default.writeFileSync(targetPath, content);else copy(_path.default.join(templateDir, file), targetPath);
236
+ };
237
+
238
+ const files = _fs.default.readdirSync(templateDir);
239
+
240
+ for (const file of files.filter(f => f !== "package.json")) write(file);
241
+
242
+ const pkg = require(_path.default.join(templateDir, "package.json"));
243
+
244
+ pkg.name = packageName;
245
+ write("package.json", JSON.stringify(pkg, null, 2));
246
+ }
@@ -5,76 +5,34 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createDocus = createDocus;
7
7
 
8
- var _fs = _interopRequireDefault(require("fs"));
9
-
10
- var _path = _interopRequireDefault(require("path"));
11
-
12
- var _minimist = _interopRequireDefault(require("minimist"));
8
+ var _path = require("path");
13
9
 
14
10
  var _prompts = _interopRequireDefault(require("prompts"));
15
11
 
16
- var _execa = _interopRequireDefault(require("execa"));
12
+ var _execa = require("execa");
17
13
 
18
- var _helpers = require("../helpers");
14
+ var _helpers = require("./helpers");
19
15
 
20
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
17
 
22
- const argv = (0, _minimist.default)(process.argv.slice(2));
23
18
  const {
24
19
  motd,
25
20
  primary,
26
21
  secondary,
22
+ warning,
27
23
  bold,
28
24
  space,
29
25
  broadcast
30
- } = (0, _helpers.log)();
31
- const cwd = process.cwd();
26
+ } = _helpers.log;
32
27
 
33
28
  async function createDocus() {
34
29
  motd();
35
- let targetDir = argv._[0];
36
-
37
- if (!targetDir) {
38
- const {
39
- projectName
40
- } = await (0, _prompts.default)({
41
- type: "text",
42
- name: "projectName",
43
- message: "Project name:",
44
- initial: "my-website"
45
- });
46
- targetDir = projectName.trim();
47
- }
48
-
30
+ const projectType = (0, _helpers.getProjectType)();
31
+ const targetDir = await (0, _helpers.getTargetDir)(projectType);
49
32
  const packageName = await (0, _helpers.getValidPackageName)(targetDir);
50
-
51
- const root = _path.default.join(cwd, targetDir);
52
-
53
- await (0, _helpers.overwriteDir)(root);
54
- space();
55
- broadcast(secondary("Scaffolding project in ") + targetDir + secondary("..."));
56
- const {
57
- templateDir
58
- } = await (0, _helpers.getTemplate)();
59
-
60
- const write = (file, content = void 0) => {
61
- const targetPath = _path.default.join(root, file);
62
-
63
- if (content) _fs.default.writeFileSync(targetPath, content);else (0, _helpers.copy)(_path.default.join(templateDir, file), targetPath);
64
- };
65
-
66
- const files = _fs.default.readdirSync(templateDir);
67
-
68
- for (const file of files.filter(f => f !== "package.json")) write(file);
69
-
70
- const pkg = require(_path.default.join(templateDir, "package.json"));
71
-
72
- pkg.name = packageName;
73
- write("package.json", JSON.stringify(pkg, null, 2));
74
- const pkgManager = /pnpm/.test(process.env.npm_execpath || "") || /pnpm/.test(process.env.npm_config_user_agent || "") ? "pnpm" : /yarn/.test(process.env.npm_execpath || "") ? "yarn" : "npm";
75
-
76
- const related = _path.default.relative(cwd, root);
77
-
33
+ const projectPath = await (0, _helpers.createDir)(targetDir);
34
+ const templateDir = await (0, _helpers.getTemplate)(projectType);
35
+ (0, _helpers.createProject)(projectPath, templateDir, packageName);
78
36
  broadcast(primary("Done."));
79
37
  space();
80
38
  const {
@@ -85,7 +43,7 @@ async function createDocus() {
85
43
  initial: "Y",
86
44
  message: "Install and start it now?"
87
45
  });
88
- if (yes) await startProject(root);else startItLater(pkgManager, root, related);
46
+ if (yes) await startProject(projectPath);else startItLater(projectPath);
89
47
  }
90
48
 
91
49
  async function startProject(projectPath) {
@@ -101,22 +59,29 @@ async function startProject(projectPath) {
101
59
  title: i
102
60
  }))
103
61
  });
104
- if (!agent) return;
105
- await (0, _execa.default)(agent, ["install"], {
62
+
63
+ if (!agent) {
64
+ space();
65
+ broadcast(warning(`You cancelled the agent selection!`));
66
+ return startItLater(projectPath);
67
+ }
68
+
69
+ await (0, _execa.execa)(agent, ["install"], {
106
70
  stdio: "inherit",
107
71
  cwd: projectPath
108
72
  });
109
- await (0, _execa.default)(agent, ["run", "dev"], {
73
+ await (0, _execa.execa)(agent, ["run", "dev"], {
110
74
  stdio: "inherit",
111
75
  cwd: projectPath
112
76
  });
113
77
  }
114
78
 
115
- function startItLater(pkgManager, root, dir) {
79
+ function startItLater(projectPath) {
116
80
  space();
117
81
  broadcast(secondary("Start it later with:"));
118
82
  space();
119
- if (root !== cwd) broadcast(primary(`cd ${bold(dir)}`));
120
- broadcast(primary(`${pkgManager === "yarn" ? "yarn" : `${pkgManager} install`}`));
121
- broadcast(primary(`${pkgManager === "yarn" ? "yarn dev" : `${pkgManager} run dev`}`));
83
+ const dir = (0, _path.relative)(_helpers.cwd, projectPath);
84
+ if (projectPath !== _helpers.cwd) broadcast(primary(`cd ${bold(dir)}`));
85
+ broadcast(primary(`${_helpers.pkgManager === "yarn" ? "yarn" : `${_helpers.pkgManager} install`}`));
86
+ broadcast(primary(`${_helpers.pkgManager === "yarn" ? "yarn dev" : `${_helpers.pkgManager} run dev`}`));
122
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-docus",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "description": "The Docus CLI.",
5
5
  "repository": "https://github.com/docusgen/cli",
6
6
  "bugs": {
@@ -38,24 +38,26 @@
38
38
  ],
39
39
  "dependencies": {
40
40
  "degit": "^2.8.4",
41
+ "execa": "^6.0.0",
41
42
  "kolorist": "^1.5.0",
42
43
  "minimist": "^1.2.5",
43
- "prompts": "^2.4.1"
44
+ "prompts": "^2.4.2"
44
45
  },
45
46
  "devDependencies": {
46
47
  "@types/degit": "^2.8.3",
47
- "@types/jest": "^27.0.1",
48
+ "@types/jest": "^27.0.3",
48
49
  "@types/minimist": "^1.2.2",
49
50
  "@types/prompts": "^2.0.14",
50
51
  "chokidar": "^3.5.2",
51
- "jest": "^27.0.6",
52
- "lint-staged": "^11.1.2",
52
+ "jest": "^27.3.1",
53
+ "jiti": "^1.12.9",
54
+ "lint-staged": "^12.1.2",
53
55
  "pascalcase": "^1.0.0",
54
- "prettier": "^2.3.2",
55
- "ts-jest": "^27.0.5",
56
- "typescript": "^4.3.5",
57
- "unbuild": "^0.4.2",
58
- "jiti": "^1.11.0"
56
+ "prettier": "^2.4.1",
57
+ "ts-jest": "^27.0.7",
58
+ "typescript": "^4.5.2",
59
+ "unbuild": "^0.5.13",
60
+ "upath": "^2.0.1"
59
61
  },
60
62
  "gitHooks": {
61
63
  "pre-commit": "lint-staged"
package/dist/helpers.js DELETED
@@ -1,154 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.copy = copy;
7
- exports.log = log;
8
- exports.getTemplate = getTemplate;
9
- exports.overwriteDir = overwriteDir;
10
- exports.getValidPackageName = getValidPackageName;
11
-
12
- var _package = require("../package.json");
13
-
14
- var _fs = _interopRequireDefault(require("fs"));
15
-
16
- var _path = _interopRequireDefault(require("path"));
17
-
18
- var _kolorist = require("kolorist");
19
-
20
- var _prompts = _interopRequireDefault(require("prompts"));
21
-
22
- var _degit = _interopRequireDefault(require("degit"));
23
-
24
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
-
26
- function copyDir(srcDir, destDir) {
27
- _fs.default.mkdirSync(destDir, {
28
- recursive: true
29
- });
30
-
31
- for (const file of _fs.default.readdirSync(srcDir)) {
32
- const srcFile = _path.default.resolve(srcDir, file);
33
-
34
- const destFile = _path.default.resolve(destDir, file);
35
-
36
- copy(srcFile, destFile);
37
- }
38
- }
39
-
40
- function emptyDir(dir) {
41
- if (!_fs.default.existsSync(dir)) return;
42
-
43
- for (const file of _fs.default.readdirSync(dir)) {
44
- const abs = _path.default.resolve(dir, file);
45
-
46
- if (_fs.default.lstatSync(abs).isDirectory()) {
47
- emptyDir(abs);
48
-
49
- _fs.default.rmdirSync(abs);
50
- } else {
51
- _fs.default.unlinkSync(abs);
52
- }
53
- }
54
- }
55
-
56
- function copy(src, dest) {
57
- const stat = _fs.default.statSync(src);
58
-
59
- if (stat.isDirectory()) copyDir(src, dest);else _fs.default.copyFileSync(src, dest);
60
- }
61
-
62
- function log() {
63
- const commands = {
64
- space: () => console.log(),
65
- bold: str => (0, _kolorist.bold)(str),
66
- primary: str => (0, _kolorist.green)(str),
67
- secondary: str => (0, _kolorist.dim)(str),
68
- warning: str => (0, _kolorist.yellow)(str),
69
- broadcast: str => console.log(` ${str}`),
70
- motd: () => {
71
- commands.space();
72
- commands.broadcast(`${(0, _kolorist.bold)("Docus") + commands.secondary(" CLI")} ${commands.primary(`v${_package.version}`)}`);
73
- commands.space();
74
- }
75
- };
76
- return commands;
77
- }
78
-
79
- async function getTemplate() {
80
- const {
81
- broadcast,
82
- primary,
83
- secondary
84
- } = log();
85
-
86
- const templateDir = _path.default.join(__dirname, "./template");
87
-
88
- broadcast([secondary("Cloning the latest"), primary("Docus"), secondary("template...")].join(" "));
89
- emptyDir(templateDir);
90
- const repo = (0, _degit.default)("git@github.com:docusgen/starter.git", {
91
- force: true,
92
- verbose: true,
93
- mode: "git"
94
- });
95
- await repo.clone(templateDir);
96
- return {
97
- templateDir
98
- };
99
- }
100
-
101
- async function overwriteDir(path2) {
102
- const {
103
- broadcast,
104
- warning,
105
- space
106
- } = log();
107
-
108
- if (!_fs.default.existsSync(path2)) {
109
- _fs.default.mkdirSync(path2, {
110
- recursive: true
111
- });
112
- } else {
113
- const existing = _fs.default.readdirSync(path2);
114
-
115
- const dir = path2.split("/");
116
- const dirName = dir[dir.length - 1];
117
-
118
- if (existing.length) {
119
- space();
120
- broadcast(warning(`Target directory "${dirName}" is not empty.`));
121
- space();
122
- const {
123
- yes
124
- } = await (0, _prompts.default)({
125
- type: "confirm",
126
- name: "yes",
127
- initial: "Y",
128
- message: "Remove existing files and continue?"
129
- });
130
- if (yes) emptyDir(path2);else return;
131
- }
132
- }
133
- }
134
-
135
- async function getValidPackageName(projectName) {
136
- projectName = _path.default.basename(projectName);
137
- const packageNameRegExp = /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
138
-
139
- if (packageNameRegExp.test(projectName)) {
140
- return projectName;
141
- } else {
142
- const suggestedPackageName = projectName.trim().toLowerCase().replace(/\s+/g, "-").replace(/^[._]/, "").replace(/[^a-z0-9-~]+/g, "-");
143
- const {
144
- inputPackageName
145
- } = await (0, _prompts.default)({
146
- type: "text",
147
- name: "inputPackageName",
148
- message: "Package name:",
149
- initial: suggestedPackageName,
150
- validate: input => packageNameRegExp.test(input) ? true : "Invalid package.json name"
151
- });
152
- return inputPackageName;
153
- }
154
- }