@crustjs/create 0.0.2 → 0.0.3
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 +4 -2
- package/dist/index.js +29 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -33,7 +33,8 @@ interface ScaffoldOptions {
|
|
|
33
33
|
/**
|
|
34
34
|
* Template directory source.
|
|
35
35
|
*
|
|
36
|
-
* - `string
|
|
36
|
+
* - `string` absolute path: used as-is
|
|
37
|
+
* - `string` relative path: resolved from the nearest package root of `process.argv[1]`
|
|
37
38
|
* - `URL`: must be a `file:` URL (for module-relative templates)
|
|
38
39
|
*/
|
|
39
40
|
readonly template: string | URL;
|
|
@@ -92,7 +93,8 @@ type PostScaffoldStep = {
|
|
|
92
93
|
* and dotfile renaming.
|
|
93
94
|
*
|
|
94
95
|
* Template resolution:
|
|
95
|
-
* - `string`
|
|
96
|
+
* - `string` absolute paths are used as-is
|
|
97
|
+
* - `string` relative paths resolve from the nearest package root of `process.argv[1]`
|
|
96
98
|
* - `URL` templates must be `file:` URLs (for module-relative templates)
|
|
97
99
|
*
|
|
98
100
|
* Call `scaffold()` multiple times to layer/compose templates — for example,
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
statSync,
|
|
18
18
|
writeFileSync
|
|
19
19
|
} from "fs";
|
|
20
|
-
import { dirname, join, relative, resolve } from "path";
|
|
20
|
+
import { dirname, isAbsolute, join, relative, resolve } from "path";
|
|
21
21
|
import { fileURLToPath } from "url";
|
|
22
22
|
|
|
23
23
|
// src/isBinary.ts
|
|
@@ -66,6 +66,22 @@ function isNonEmptyDir(dirPath) {
|
|
|
66
66
|
const entries = readdirSync(dirPath);
|
|
67
67
|
return entries.length > 0;
|
|
68
68
|
}
|
|
69
|
+
function findNearestPackageRoot(startPath) {
|
|
70
|
+
let current = resolve(startPath);
|
|
71
|
+
if (existsSync(current) && !statSync(current).isDirectory()) {
|
|
72
|
+
current = dirname(current);
|
|
73
|
+
}
|
|
74
|
+
while (true) {
|
|
75
|
+
if (existsSync(join(current, "package.json"))) {
|
|
76
|
+
return current;
|
|
77
|
+
}
|
|
78
|
+
const parent = dirname(current);
|
|
79
|
+
if (parent === current) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
current = parent;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
69
85
|
function resolveTemplateDir(template) {
|
|
70
86
|
if (template instanceof URL) {
|
|
71
87
|
if (template.protocol !== "file:") {
|
|
@@ -73,7 +89,18 @@ function resolveTemplateDir(template) {
|
|
|
73
89
|
}
|
|
74
90
|
return fileURLToPath(template);
|
|
75
91
|
}
|
|
76
|
-
|
|
92
|
+
if (isAbsolute(template)) {
|
|
93
|
+
return resolve(template);
|
|
94
|
+
}
|
|
95
|
+
const entrypoint = process.argv[1];
|
|
96
|
+
if (!entrypoint) {
|
|
97
|
+
throw new Error(`Could not resolve relative template path "${template}" because process.argv[1] is not set. Pass an absolute path or a file: URL template.`);
|
|
98
|
+
}
|
|
99
|
+
const packageRoot = findNearestPackageRoot(resolve(entrypoint));
|
|
100
|
+
if (!packageRoot) {
|
|
101
|
+
throw new Error(`Could not resolve relative template path "${template}" from entrypoint "${entrypoint}" because no package.json was found in its parent directories. Pass an absolute path or a file: URL template.`);
|
|
102
|
+
}
|
|
103
|
+
return resolve(packageRoot, template);
|
|
77
104
|
}
|
|
78
105
|
function formatTemplateInput(template) {
|
|
79
106
|
return typeof template === "string" ? template : template.href;
|