@crustjs/create 0.0.1 → 0.0.2
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/dist/index.d.ts +15 -16
- package/dist/index.js +20 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -22,24 +22,23 @@ declare function interpolate(content: string, context: Record<string, string>):
|
|
|
22
22
|
* @example
|
|
23
23
|
* ```ts
|
|
24
24
|
* const options: ScaffoldOptions = {
|
|
25
|
-
* template: "../templates/base",
|
|
25
|
+
* template: new URL("../templates/base", import.meta.url),
|
|
26
26
|
* dest: "./my-project",
|
|
27
|
-
* importMeta: import.meta.url,
|
|
28
27
|
* context: { name: "my-app", description: "A cool CLI" },
|
|
29
28
|
* conflict: "abort",
|
|
30
29
|
* };
|
|
31
30
|
* ```
|
|
32
31
|
*/
|
|
33
32
|
interface ScaffoldOptions {
|
|
34
|
-
/** Relative path to the template directory (resolved against `importMeta`). */
|
|
35
|
-
readonly template: string;
|
|
36
|
-
/** Absolute or relative path to the destination directory. */
|
|
37
|
-
readonly dest: string;
|
|
38
33
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
34
|
+
* Template directory source.
|
|
35
|
+
*
|
|
36
|
+
* - `string`: resolved relative to `process.cwd()`
|
|
37
|
+
* - `URL`: must be a `file:` URL (for module-relative templates)
|
|
41
38
|
*/
|
|
42
|
-
readonly
|
|
39
|
+
readonly template: string | URL;
|
|
40
|
+
/** Absolute or relative path to the destination directory. */
|
|
41
|
+
readonly dest: string;
|
|
43
42
|
/**
|
|
44
43
|
* Variables to interpolate into template file contents.
|
|
45
44
|
* Keys map to `{{key}}` placeholders in template files.
|
|
@@ -92,9 +91,9 @@ type PostScaffoldStep = {
|
|
|
92
91
|
* Copy a template directory to a destination, applying variable interpolation
|
|
93
92
|
* and dotfile renaming.
|
|
94
93
|
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
94
|
+
* Template resolution:
|
|
95
|
+
* - `string` templates resolve relative to `process.cwd()`
|
|
96
|
+
* - `URL` templates must be `file:` URLs (for module-relative templates)
|
|
98
97
|
*
|
|
99
98
|
* Call `scaffold()` multiple times to layer/compose templates — for example,
|
|
100
99
|
* a base template followed by a TypeScript-specific overlay.
|
|
@@ -108,9 +107,8 @@ type PostScaffoldStep = {
|
|
|
108
107
|
* import { scaffold } from "@crustjs/create";
|
|
109
108
|
*
|
|
110
109
|
* const result = await scaffold({
|
|
111
|
-
* template: "../templates/base",
|
|
110
|
+
* template: new URL("../templates/base", import.meta.url),
|
|
112
111
|
* dest: "./my-project",
|
|
113
|
-
* importMeta: import.meta.url,
|
|
114
112
|
* context: { name: "my-app", description: "A cool CLI" },
|
|
115
113
|
* });
|
|
116
114
|
*
|
|
@@ -141,6 +139,7 @@ declare function scaffold(options: ScaffoldOptions): Promise<ScaffoldResult>;
|
|
|
141
139
|
* ```
|
|
142
140
|
*/
|
|
143
141
|
declare function runSteps(steps: PostScaffoldStep[], cwd: string): Promise<void>;
|
|
142
|
+
type PackageManager = "npm" | "pnpm" | "bun" | "yarn";
|
|
144
143
|
/**
|
|
145
144
|
* Detect the package manager for a project directory.
|
|
146
145
|
*
|
|
@@ -158,7 +157,7 @@ declare function runSteps(steps: PostScaffoldStep[], cwd: string): Promise<void>
|
|
|
158
157
|
* // => "bun" (if bun.lock exists)
|
|
159
158
|
* ```
|
|
160
159
|
*/
|
|
161
|
-
declare function detectPackageManager(cwd?: string):
|
|
160
|
+
declare function detectPackageManager(cwd?: string): PackageManager;
|
|
162
161
|
/**
|
|
163
162
|
* Check whether `git` is installed and available on the system PATH.
|
|
164
163
|
*
|
|
@@ -192,4 +191,4 @@ declare function getGitUser(): {
|
|
|
192
191
|
name: string | null;
|
|
193
192
|
email: string | null;
|
|
194
193
|
};
|
|
195
|
-
export { scaffold, runSteps, isGitInstalled, interpolate, getGitUser, detectPackageManager, ScaffoldResult, ScaffoldOptions, PostScaffoldStep };
|
|
194
|
+
export { scaffold, runSteps, isGitInstalled, interpolate, getGitUser, detectPackageManager, ScaffoldResult, ScaffoldOptions, PostScaffoldStep, PackageManager };
|
package/dist/index.js
CHANGED
|
@@ -66,10 +66,28 @@ function isNonEmptyDir(dirPath) {
|
|
|
66
66
|
const entries = readdirSync(dirPath);
|
|
67
67
|
return entries.length > 0;
|
|
68
68
|
}
|
|
69
|
+
function resolveTemplateDir(template) {
|
|
70
|
+
if (template instanceof URL) {
|
|
71
|
+
if (template.protocol !== "file:") {
|
|
72
|
+
throw new Error(`Template URL must use file: protocol, got "${template.protocol}".`);
|
|
73
|
+
}
|
|
74
|
+
return fileURLToPath(template);
|
|
75
|
+
}
|
|
76
|
+
return resolve(process.cwd(), template);
|
|
77
|
+
}
|
|
78
|
+
function formatTemplateInput(template) {
|
|
79
|
+
return typeof template === "string" ? template : template.href;
|
|
80
|
+
}
|
|
69
81
|
async function scaffold(options) {
|
|
70
|
-
const { template, dest,
|
|
71
|
-
const templateDir =
|
|
82
|
+
const { template, dest, context, conflict = "abort" } = options;
|
|
83
|
+
const templateDir = resolveTemplateDir(template);
|
|
72
84
|
const destDir = resolve(dest);
|
|
85
|
+
if (!existsSync(templateDir)) {
|
|
86
|
+
throw new Error(`Template directory "${templateDir}" does not exist (from template: "${formatTemplateInput(template)}").`);
|
|
87
|
+
}
|
|
88
|
+
if (!statSync(templateDir).isDirectory()) {
|
|
89
|
+
throw new Error(`Template path "${templateDir}" is not a directory (from template: "${formatTemplateInput(template)}").`);
|
|
90
|
+
}
|
|
73
91
|
if (conflict === "abort" && isNonEmptyDir(destDir)) {
|
|
74
92
|
throw new Error(`Destination directory "${destDir}" already exists and is non-empty. Use conflict: "overwrite" to proceed.`);
|
|
75
93
|
}
|