create-gen-app 0.1.7 → 0.2.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.
- package/README.md +35 -31
- package/cache.d.ts +13 -0
- package/cache.js +76 -0
- package/clone.d.ts +6 -0
- package/clone.js +10 -6
- package/esm/cache.js +38 -0
- package/esm/clone.js +14 -11
- package/esm/index.js +37 -24
- package/esm/replace.js +21 -7
- package/esm/template-cache.js +223 -0
- package/index.d.ts +8 -6
- package/index.js +27 -14
- package/package.json +5 -11
- package/replace.js +21 -7
- package/template-cache.d.ts +59 -0
- package/template-cache.js +260 -0
- package/types.d.ts +31 -0
- package/cli.d.ts +0 -6
- package/cli.js +0 -239
- package/esm/cli.js +0 -200
package/esm/cli.js
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import * as fs from "fs";
|
|
3
|
-
import * as path from "path";
|
|
4
|
-
import { Inquirerer } from "inquirerer";
|
|
5
|
-
import minimist from "minimist";
|
|
6
|
-
import { cloneRepo } from "./clone";
|
|
7
|
-
import { createGen } from "./index";
|
|
8
|
-
import packageJson from "../package.json";
|
|
9
|
-
const DEFAULT_REPO = "https://github.com/launchql/pgpm-boilerplates.git";
|
|
10
|
-
const DEFAULT_PATH = ".";
|
|
11
|
-
const DEFAULT_OUTPUT_FALLBACK = "create-gen-app-output";
|
|
12
|
-
const PACKAGE_VERSION = packageJson.version ?? "0.0.0";
|
|
13
|
-
const RESERVED_ARG_KEYS = new Set([
|
|
14
|
-
"_",
|
|
15
|
-
"repo",
|
|
16
|
-
"r",
|
|
17
|
-
"branch",
|
|
18
|
-
"b",
|
|
19
|
-
"path",
|
|
20
|
-
"p",
|
|
21
|
-
"template",
|
|
22
|
-
"t",
|
|
23
|
-
"output",
|
|
24
|
-
"o",
|
|
25
|
-
"force",
|
|
26
|
-
"f",
|
|
27
|
-
"help",
|
|
28
|
-
"h",
|
|
29
|
-
"version",
|
|
30
|
-
"v",
|
|
31
|
-
"no-tty",
|
|
32
|
-
"n",
|
|
33
|
-
]);
|
|
34
|
-
export async function runCli(rawArgv = process.argv.slice(2)) {
|
|
35
|
-
const args = minimist(rawArgv, {
|
|
36
|
-
alias: {
|
|
37
|
-
r: "repo",
|
|
38
|
-
b: "branch",
|
|
39
|
-
p: "path",
|
|
40
|
-
t: "template",
|
|
41
|
-
o: "output",
|
|
42
|
-
f: "force",
|
|
43
|
-
h: "help",
|
|
44
|
-
v: "version",
|
|
45
|
-
n: "no-tty",
|
|
46
|
-
},
|
|
47
|
-
string: ["repo", "branch", "path", "template", "output"],
|
|
48
|
-
boolean: ["force", "help", "version", "no-tty"],
|
|
49
|
-
default: {
|
|
50
|
-
repo: DEFAULT_REPO,
|
|
51
|
-
path: DEFAULT_PATH,
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
if (args.help) {
|
|
55
|
-
printHelp();
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
if (args.version) {
|
|
59
|
-
printVersion();
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (!args.output && args._[0]) {
|
|
63
|
-
args.output = args._[0];
|
|
64
|
-
}
|
|
65
|
-
let tempDir = null;
|
|
66
|
-
try {
|
|
67
|
-
console.log(`Cloning template from ${args.repo}...`);
|
|
68
|
-
if (args.branch) {
|
|
69
|
-
console.log(`Using branch ${args.branch}`);
|
|
70
|
-
}
|
|
71
|
-
tempDir = await cloneRepo(args.repo, { branch: args.branch });
|
|
72
|
-
const selectionRoot = path.join(tempDir, args.path);
|
|
73
|
-
if (!fs.existsSync(selectionRoot) || !fs.statSync(selectionRoot).isDirectory()) {
|
|
74
|
-
throw new Error(`Template path "${args.path}" does not exist in ${args.repo}`);
|
|
75
|
-
}
|
|
76
|
-
const templates = fs
|
|
77
|
-
.readdirSync(selectionRoot, { withFileTypes: true })
|
|
78
|
-
.filter((entry) => entry.isDirectory() && !entry.name.startsWith("."))
|
|
79
|
-
.map((entry) => entry.name)
|
|
80
|
-
.sort();
|
|
81
|
-
if (templates.length === 0) {
|
|
82
|
-
throw new Error("No template folders found in repository");
|
|
83
|
-
}
|
|
84
|
-
let selectedTemplate = args.template;
|
|
85
|
-
if (selectedTemplate) {
|
|
86
|
-
if (!templates.includes(selectedTemplate)) {
|
|
87
|
-
throw new Error(`Template "${selectedTemplate}" not found in ${args.repo}${args.path === "." ? "" : `/${args.path}`}`);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
else if (templates.length === 1) {
|
|
91
|
-
selectedTemplate = templates[0];
|
|
92
|
-
console.log(`Using the only available template: ${selectedTemplate}`);
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
selectedTemplate = await promptForTemplate(templates);
|
|
96
|
-
}
|
|
97
|
-
if (!selectedTemplate) {
|
|
98
|
-
throw new Error("Template selection failed");
|
|
99
|
-
}
|
|
100
|
-
const normalizedBasePath = args.path === "." || args.path === "./"
|
|
101
|
-
? ""
|
|
102
|
-
: args.path.replace(/^[./]+/, "").replace(/\/+$/, "");
|
|
103
|
-
const fromPath = normalizedBasePath
|
|
104
|
-
? path.join(normalizedBasePath, selectedTemplate)
|
|
105
|
-
: selectedTemplate;
|
|
106
|
-
const outputDir = resolveOutputDir(args.output, selectedTemplate);
|
|
107
|
-
ensureOutputDir(outputDir, Boolean(args.force));
|
|
108
|
-
const answerOverrides = extractAnswerOverrides(args);
|
|
109
|
-
const noTty = Boolean(args["no-tty"] ?? args.noTty);
|
|
110
|
-
await createGen({
|
|
111
|
-
templateUrl: args.repo,
|
|
112
|
-
fromBranch: args.branch,
|
|
113
|
-
fromPath,
|
|
114
|
-
outputDir,
|
|
115
|
-
argv: answerOverrides,
|
|
116
|
-
noTty,
|
|
117
|
-
});
|
|
118
|
-
console.log(`\n✨ Done! Project ready at ${outputDir}`);
|
|
119
|
-
return { outputDir, template: selectedTemplate };
|
|
120
|
-
}
|
|
121
|
-
finally {
|
|
122
|
-
if (tempDir && fs.existsSync(tempDir)) {
|
|
123
|
-
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
function printHelp() {
|
|
128
|
-
console.log(`
|
|
129
|
-
create-gen-app CLI
|
|
130
|
-
|
|
131
|
-
Usage:
|
|
132
|
-
create-gen-app [options] [outputDir]
|
|
133
|
-
cga [options] [outputDir]
|
|
134
|
-
|
|
135
|
-
Options:
|
|
136
|
-
-r, --repo <url> Git repository to clone (default: ${DEFAULT_REPO})
|
|
137
|
-
-b, --branch <name> Branch to use when cloning
|
|
138
|
-
-p, --path <dir> Subdirectory that contains templates (default: .)
|
|
139
|
-
-t, --template <name> Template folder to use (will prompt if omitted)
|
|
140
|
-
-o, --output <dir> Output directory (defaults to ./<template>)
|
|
141
|
-
-f, --force Overwrite the output directory if it exists
|
|
142
|
-
-v, --version Show CLI version
|
|
143
|
-
-n, --no-tty Disable TTY mode for prompts
|
|
144
|
-
-h, --help Show this help message
|
|
145
|
-
|
|
146
|
-
You can also pass variable overrides, e.g.:
|
|
147
|
-
create-gen-app --template module --PROJECT_NAME my-app
|
|
148
|
-
`);
|
|
149
|
-
}
|
|
150
|
-
function printVersion() {
|
|
151
|
-
console.log(`create-gen-app v${PACKAGE_VERSION}`);
|
|
152
|
-
}
|
|
153
|
-
async function promptForTemplate(templates) {
|
|
154
|
-
const prompter = new Inquirerer();
|
|
155
|
-
const question = {
|
|
156
|
-
type: "list",
|
|
157
|
-
name: "template",
|
|
158
|
-
message: "Which template would you like to use?",
|
|
159
|
-
options: templates,
|
|
160
|
-
required: true,
|
|
161
|
-
};
|
|
162
|
-
try {
|
|
163
|
-
const answers = (await prompter.prompt({}, [question]));
|
|
164
|
-
return answers.template;
|
|
165
|
-
}
|
|
166
|
-
finally {
|
|
167
|
-
if (typeof prompter.close === "function") {
|
|
168
|
-
prompter.close();
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
function resolveOutputDir(outputArg, template) {
|
|
173
|
-
const base = outputArg ?? (template ? path.join(process.cwd(), template) : DEFAULT_OUTPUT_FALLBACK);
|
|
174
|
-
return path.resolve(base);
|
|
175
|
-
}
|
|
176
|
-
function ensureOutputDir(outputDir, force) {
|
|
177
|
-
if (!fs.existsSync(outputDir)) {
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
if (!force) {
|
|
181
|
-
throw new Error(`Output directory "${outputDir}" already exists. Use --force to overwrite or choose another path.`);
|
|
182
|
-
}
|
|
183
|
-
fs.rmSync(outputDir, { recursive: true, force: true });
|
|
184
|
-
}
|
|
185
|
-
function extractAnswerOverrides(args) {
|
|
186
|
-
const overrides = {};
|
|
187
|
-
for (const [key, value] of Object.entries(args)) {
|
|
188
|
-
if (RESERVED_ARG_KEYS.has(key)) {
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
overrides[key] = value;
|
|
192
|
-
}
|
|
193
|
-
return overrides;
|
|
194
|
-
}
|
|
195
|
-
if (require.main === module) {
|
|
196
|
-
runCli().catch((error) => {
|
|
197
|
-
console.error(error instanceof Error ? error.message : error);
|
|
198
|
-
process.exitCode = 1;
|
|
199
|
-
});
|
|
200
|
-
}
|