@plexus-ms/cli-template 0.0.0-stage → 0.129.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/LICENSE.copier +25 -0
- package/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +6 -0
- package/dist/bin.js.map +1 -0
- package/dist/cli.d.ts +12 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +412 -0
- package/dist/cli.js.map +1 -0
- package/dist/directory.d.ts +16 -0
- package/dist/directory.d.ts.map +1 -0
- package/dist/directory.js +240 -0
- package/dist/directory.js.map +1 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +15 -0
- package/dist/errors.js.map +1 -0
- package/dist/files.d.ts +17 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +209 -0
- package/dist/files.js.map +1 -0
- package/dist/git-merge-file.d.ts +4 -0
- package/dist/git-merge-file.d.ts.map +1 -0
- package/dist/git-merge-file.js +95 -0
- package/dist/git-merge-file.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/jinja.d.ts +7 -0
- package/dist/jinja.d.ts.map +1 -0
- package/dist/jinja.js +36 -0
- package/dist/jinja.js.map +1 -0
- package/dist/manifest.d.ts +13 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/manifest.js +209 -0
- package/dist/manifest.js.map +1 -0
- package/dist/merge.d.ts +4 -0
- package/dist/merge.d.ts.map +1 -0
- package/dist/merge.js +185 -0
- package/dist/merge.js.map +1 -0
- package/dist/questions.d.ts +14 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +290 -0
- package/dist/questions.js.map +1 -0
- package/dist/render.d.ts +4 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +245 -0
- package/dist/render.js.map +1 -0
- package/dist/types.d.ts +130 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +80 -0
- package/dist/types.js.map +1 -0
- package/package.json +45 -4
- package/README.md +0 -3
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// Directories and trees: the one place that reads a directory into a tree and
|
|
2
|
+
// the one that writes a tree to a directory. The core works on trees in memory
|
|
3
|
+
// (types.ts); this is the filesystem side of it, for the command in cli.ts.
|
|
4
|
+
//
|
|
5
|
+
// Reading. A directory is walked with `lstat`, and a symlink is never followed:
|
|
6
|
+
// it is an entry whose bytes are its target and whose mode says it is a
|
|
7
|
+
// symlink, which the core treats as it must (a template holding one is an
|
|
8
|
+
// error, `merge` never writes through one). A directory that holds nothing is
|
|
9
|
+
// a DIRECTORY entry; one that holds entries is implied by their paths. Any
|
|
10
|
+
// other kind of file, a socket or a pipe, is an input error.
|
|
11
|
+
//
|
|
12
|
+
// `readTree` reads a whole directory, which is what a template and a render
|
|
13
|
+
// are. The directory `merge` merges into is read by `readCurrent` instead, and
|
|
14
|
+
// only as far as `merge` looks (merge.ts): for each path of the two renders,
|
|
15
|
+
// - each directory above it that is a symlink or a file, as an entry, and
|
|
16
|
+
// the walk stops there, since nothing below it is reachable
|
|
17
|
+
// - the path itself where it is a file or a symlink
|
|
18
|
+
// - where it is a directory, one entry below it, the first leaf in name
|
|
19
|
+
// order, so that "current holds entries below the path" is answered, or the
|
|
20
|
+
// empty directory itself as a DIRECTORY entry
|
|
21
|
+
// so that a repository's `.git` and `node_modules` are never read, and neither
|
|
22
|
+
// is any file no render names. This answers as a whole read would where the
|
|
23
|
+
// filesystem tells names apart exactly. On a case-insensitive or normalising
|
|
24
|
+
// one, the default on macOS, `lstat('A')` finds `a`: the partial read then
|
|
25
|
+
// reports what the writes will meet, which is the safer answer, and differs
|
|
26
|
+
// from a whole read.
|
|
27
|
+
//
|
|
28
|
+
// Writing. `applyChanges` makes a directory hold what the core decided, for the
|
|
29
|
+
// paths it is given, an entry to write or nothing to delete, and touches
|
|
30
|
+
// nothing else. It never writes or deletes through a symlink: a path whose
|
|
31
|
+
// directory above it is a symlink or a file, or that is itself a symlink, is
|
|
32
|
+
// skipped. No test reaches these guards, since the core already answers `kept`
|
|
33
|
+
// where a directory above a path is a symlink or the path is one (rules 1 and 4
|
|
34
|
+
// of merge.ts) and never hands this such a path; they are a second line of
|
|
35
|
+
// defence for a caller that hands it something else. A file is written only where its bytes differ from what is there. A
|
|
36
|
+
// file that exists keeps its permissions but for its executable bits, which
|
|
37
|
+
// follow the entry's, and never widen beyond that: where the entry is
|
|
38
|
+
// executable, a mode gains the owner's execute bit and the group's and others'
|
|
39
|
+
// only where they may read (0o600 becomes 0o700, 0o644 becomes 0o755); where it
|
|
40
|
+
// is not, all three are cleared. A new file or directory takes the entry's
|
|
41
|
+
// mode under the process's umask. A failure stops the run with the path that
|
|
42
|
+
// failed and the note that the paths before it, in the order given, were
|
|
43
|
+
// already applied.
|
|
44
|
+
//
|
|
45
|
+
// A deletion removes a file, and then the directories it leaves empty, upward
|
|
46
|
+
// from it and no further than the directory itself: copier removes directories
|
|
47
|
+
// that end empty, and `merge` never deletes one. A directory that was empty
|
|
48
|
+
// before, or the render still holds as an empty directory (`keep`), stays.
|
|
49
|
+
import { chmod, lstat, mkdir, readdir, readFile, readlink, rmdir, unlink, writeFile } from 'node:fs/promises';
|
|
50
|
+
import { join } from 'node:path';
|
|
51
|
+
import { InputError, messageOf } from "./errors.js";
|
|
52
|
+
import { DIRECTORY, isExecutable, MODE_TYPE, REGULAR, SYMLINK } from "./types.js";
|
|
53
|
+
/** An entry for a directory that holds nothing. */
|
|
54
|
+
const EMPTY_DIRECTORY = { bytes: new Uint8Array(), mode: DIRECTORY | 0o755 };
|
|
55
|
+
const SYMLINK_ARGUMENT = 'is a symlink, which is not followed: name the directory itself';
|
|
56
|
+
/** The permission bits a file's mode holds. */
|
|
57
|
+
const PERMISSIONS = 0o7777;
|
|
58
|
+
/** `lstat` of `path`, nothing where there is nothing there or something above it is no directory. */
|
|
59
|
+
async function stat(path) {
|
|
60
|
+
try {
|
|
61
|
+
return await lstat(path);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
const code = error.code;
|
|
65
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
66
|
+
return undefined;
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** The entry at `path`, a directory's as `undefined`. */
|
|
71
|
+
async function leaf(path, mode) {
|
|
72
|
+
const type = mode & MODE_TYPE;
|
|
73
|
+
if (type === SYMLINK)
|
|
74
|
+
return { bytes: Buffer.from(await readlink(path)), mode };
|
|
75
|
+
if (type === REGULAR)
|
|
76
|
+
return { bytes: new Uint8Array(await readFile(path)), mode };
|
|
77
|
+
if (type === DIRECTORY)
|
|
78
|
+
return undefined;
|
|
79
|
+
throw new InputError('neither a file, a symlink nor a directory', path);
|
|
80
|
+
}
|
|
81
|
+
/** Whether `path` is a directory, an input error where it is not. */
|
|
82
|
+
export async function requireDirectory(path) {
|
|
83
|
+
const st = await stat(path);
|
|
84
|
+
if (st === undefined)
|
|
85
|
+
throw new InputError('there is no such directory', path);
|
|
86
|
+
if (st.isSymbolicLink())
|
|
87
|
+
throw new InputError(SYMLINK_ARGUMENT, path);
|
|
88
|
+
if (!st.isDirectory())
|
|
89
|
+
throw new InputError('not a directory', path);
|
|
90
|
+
}
|
|
91
|
+
/** The tree of the directory `root`. */
|
|
92
|
+
export async function readTree(root) {
|
|
93
|
+
await requireDirectory(root);
|
|
94
|
+
const tree = new Map();
|
|
95
|
+
await walk(root, '', tree);
|
|
96
|
+
return tree;
|
|
97
|
+
}
|
|
98
|
+
async function walk(root, prefix, tree) {
|
|
99
|
+
const names = (await readdir(join(root, prefix))).sort();
|
|
100
|
+
if (names.length === 0 && prefix !== '') {
|
|
101
|
+
tree.set(prefix, EMPTY_DIRECTORY);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
for (const name of names) {
|
|
105
|
+
const path = prefix === '' ? name : `${prefix}/${name}`;
|
|
106
|
+
const st = await lstat(join(root, path));
|
|
107
|
+
const entry = await leaf(join(root, path), st.mode);
|
|
108
|
+
if (entry === undefined)
|
|
109
|
+
await walk(root, path, tree);
|
|
110
|
+
else
|
|
111
|
+
tree.set(path, entry);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** The part of the directory `root` that `merge` looks at for `paths`, as the header says. */
|
|
115
|
+
export async function readCurrent(root, paths) {
|
|
116
|
+
await requireDirectory(root);
|
|
117
|
+
const tree = new Map();
|
|
118
|
+
for (const path of paths) {
|
|
119
|
+
const parts = path.split('/');
|
|
120
|
+
for (let i = 1; i <= parts.length; i++) {
|
|
121
|
+
const at = parts.slice(0, i).join('/');
|
|
122
|
+
const st = await stat(join(root, at));
|
|
123
|
+
if (st === undefined)
|
|
124
|
+
break;
|
|
125
|
+
const entry = await leaf(join(root, at), st.mode);
|
|
126
|
+
if (entry !== undefined) {
|
|
127
|
+
tree.set(at, entry);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
if (i === parts.length)
|
|
131
|
+
await firstLeaf(root, at, tree);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return tree;
|
|
135
|
+
}
|
|
136
|
+
/** The first leaf below the directory `at`, in name order; the directory itself where it holds nothing. */
|
|
137
|
+
async function firstLeaf(root, at, tree) {
|
|
138
|
+
const [name] = (await readdir(join(root, at))).sort();
|
|
139
|
+
if (name === undefined) {
|
|
140
|
+
tree.set(at, EMPTY_DIRECTORY);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const path = `${at}/${name}`;
|
|
144
|
+
const st = await lstat(join(root, path));
|
|
145
|
+
const entry = await leaf(join(root, path), st.mode);
|
|
146
|
+
if (entry === undefined)
|
|
147
|
+
await firstLeaf(root, path, tree);
|
|
148
|
+
else
|
|
149
|
+
tree.set(path, entry);
|
|
150
|
+
}
|
|
151
|
+
/** Whether every directory above `path` is a directory or missing, creating the missing ones where `create`. */
|
|
152
|
+
async function parentsOpen(root, path, create) {
|
|
153
|
+
const parts = path.split('/').slice(0, -1);
|
|
154
|
+
for (let i = 1; i <= parts.length; i++) {
|
|
155
|
+
const dir = join(root, ...parts.slice(0, i));
|
|
156
|
+
const st = await stat(dir);
|
|
157
|
+
if (st === undefined) {
|
|
158
|
+
if (!create)
|
|
159
|
+
return true;
|
|
160
|
+
await mkdir(dir);
|
|
161
|
+
}
|
|
162
|
+
else if (!st.isDirectory())
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
/** The mode a file of `mode` takes when its executable bits follow `executable`, nothing else moving. */
|
|
168
|
+
function withExecutable(mode, executable) {
|
|
169
|
+
const bits = mode & PERMISSIONS;
|
|
170
|
+
if (!executable)
|
|
171
|
+
return bits & ~0o111;
|
|
172
|
+
return bits | 0o100 | (bits & 0o040 ? 0o010 : 0) | (bits & 0o004 ? 0o001 : 0);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Makes `root` hold, at each path of `changes`, its entry, or nothing where it
|
|
176
|
+
* is `undefined`; the header says how. `keep` holds the paths of directories
|
|
177
|
+
* not to remove after a deletion.
|
|
178
|
+
*/
|
|
179
|
+
export async function applyChanges(root, changes, keep = new Set()) {
|
|
180
|
+
await mkdir(root, { recursive: true });
|
|
181
|
+
for (const [path, entry] of changes) {
|
|
182
|
+
try {
|
|
183
|
+
if (entry === undefined)
|
|
184
|
+
await remove(root, path, keep);
|
|
185
|
+
else
|
|
186
|
+
await write(root, path, entry);
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
throw new Error(`${path}: ${messageOf(error)}; the paths before it in path order were already applied`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async function write(root, path, entry) {
|
|
194
|
+
if (!(await parentsOpen(root, path, true)))
|
|
195
|
+
return;
|
|
196
|
+
const file = join(root, path);
|
|
197
|
+
const st = await stat(file);
|
|
198
|
+
const directory = (entry.mode & MODE_TYPE) === DIRECTORY;
|
|
199
|
+
if (st === undefined) {
|
|
200
|
+
if (directory)
|
|
201
|
+
await mkdir(file);
|
|
202
|
+
else
|
|
203
|
+
await writeFile(file, entry.bytes, { mode: entry.mode & PERMISSIONS });
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (!st.isFile() || directory)
|
|
207
|
+
return;
|
|
208
|
+
if (!Buffer.from(entry.bytes).equals(await readFile(file)))
|
|
209
|
+
await writeFile(file, entry.bytes);
|
|
210
|
+
if (isExecutable(st.mode) !== isExecutable(entry.mode))
|
|
211
|
+
await chmod(file, withExecutable(st.mode, isExecutable(entry.mode)));
|
|
212
|
+
}
|
|
213
|
+
async function remove(root, path, keep) {
|
|
214
|
+
if (!(await parentsOpen(root, path, false)))
|
|
215
|
+
return;
|
|
216
|
+
const st = await stat(join(root, path));
|
|
217
|
+
if (st === undefined || !st.isFile())
|
|
218
|
+
return;
|
|
219
|
+
await unlink(join(root, path));
|
|
220
|
+
const parts = path.split('/').slice(0, -1);
|
|
221
|
+
for (let i = parts.length; i > 0; i--) {
|
|
222
|
+
const dir = parts.slice(0, i).join('/');
|
|
223
|
+
if (keep.has(dir) || (await readdir(join(root, dir))).length > 0)
|
|
224
|
+
return;
|
|
225
|
+
await rmdir(join(root, dir));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/** Whether `path` can take a render: missing, or a directory that holds nothing; an input error otherwise. */
|
|
229
|
+
export async function requireEmpty(path) {
|
|
230
|
+
const st = await stat(path);
|
|
231
|
+
if (st === undefined)
|
|
232
|
+
return;
|
|
233
|
+
if (st.isSymbolicLink())
|
|
234
|
+
throw new InputError(SYMLINK_ARGUMENT, path);
|
|
235
|
+
if (!st.isDirectory())
|
|
236
|
+
throw new InputError('exists and is not a directory', path);
|
|
237
|
+
if ((await readdir(path)).length > 0)
|
|
238
|
+
throw new InputError('is not empty', path);
|
|
239
|
+
}
|
|
240
|
+
//#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AAI,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC;QAAE,MAAM,IAAI,UAAU,CAAC,cAAc,EAAE,IAAI,CAAC,CAAC;AACnF,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The message of a thrown value, whatever was thrown. */
|
|
2
|
+
export declare function messageOf(error: unknown): string;
|
|
3
|
+
/** An error in what the command was given, such as a directory that is missing; it names the path where there is one. */
|
|
4
|
+
export declare class InputError extends Error {
|
|
5
|
+
readonly path?: string;
|
|
6
|
+
constructor(message: string, path?: string);
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA,0DAA0D;AAC1D,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEhD;AAED,yHAAyH;AACzH,qBAAa,UAAW,SAAQ,KAAK;IACnC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;gBAEX,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM;CAI3C"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// What a caught exception says, for a RenderError's message.
|
|
2
|
+
/** The message of a thrown value, whatever was thrown. */
|
|
3
|
+
export function messageOf(error) {
|
|
4
|
+
return error instanceof Error ? error.message : String(error);
|
|
5
|
+
}
|
|
6
|
+
/** An error in what the command was given, such as a directory that is missing; it names the path where there is one. */
|
|
7
|
+
export class InputError extends Error {
|
|
8
|
+
path;
|
|
9
|
+
constructor(message, path) {
|
|
10
|
+
super(message);
|
|
11
|
+
if (path !== undefined)
|
|
12
|
+
this.path = path;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAE7D,0DAA0D;AAC1D,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAChE,CAAC;AAED,yHAAyH;AACzH,MAAM,OAAO,UAAW,SAAQ,KAAK;IAC1B,IAAI,CAAU;IAEvB,YAAY,OAAe,EAAE,IAAa;QACxC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,IAAI,KAAK,SAAS;YAAE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IAC3C,CAAC;CACF"}
|
package/dist/files.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Context } from './jinja.ts';
|
|
2
|
+
import { type Template } from './manifest.ts';
|
|
3
|
+
import { type Entry, type RenderError } from './types.ts';
|
|
4
|
+
/** A rendered entry, with the template and the path below `files/` it came from. */
|
|
5
|
+
export interface Rendered {
|
|
6
|
+
readonly entry: Entry;
|
|
7
|
+
readonly source: string;
|
|
8
|
+
readonly owner: Template;
|
|
9
|
+
}
|
|
10
|
+
/** A template, and the context its files render in. */
|
|
11
|
+
export interface Rendering {
|
|
12
|
+
readonly template: Template;
|
|
13
|
+
readonly context: Context;
|
|
14
|
+
}
|
|
15
|
+
/** The entries the templates of `renderings` render together, by rendered path; their problems go to `errors`. */
|
|
16
|
+
export declare function renderFiles(renderings: readonly Rendering[], errors: RenderError[]): Map<string, Rendered>;
|
|
17
|
+
//# sourceMappingURL=files.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../src/files.ts"],"names":[],"mappings":"AAuCA,OAAO,EAAE,KAAK,OAAO,EAAgB,MAAM,YAAY,CAAC;AACxD,OAAO,EAAS,KAAK,QAAQ,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAa,KAAK,KAAK,EAA8C,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAEjH,oFAAoF;AACpF,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC;CAC1B;AAED,uDAAuD;AACvD,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B;AAgDD,kHAAkH;AAClH,wBAAgB,WAAW,CAAC,UAAU,EAAE,SAAS,SAAS,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAiD1G"}
|
package/dist/files.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// A template's files rendered: which file lands where, and with what bytes.
|
|
2
|
+
// Ported from copier v9.19.1 (MIT, see LICENSE.copier): `_render_path` and
|
|
3
|
+
// `_render_parts` for the paths, and the render-or-copy part of `_render_file`
|
|
4
|
+
// of copier/_main.py for the content and the mode.
|
|
5
|
+
//
|
|
6
|
+
// Every part of a path is Jinja and is rendered by itself, so that a part can
|
|
7
|
+
// be a condition: a part that renders empty skips the file, or the whole
|
|
8
|
+
// subtree for a directory. A directory part is rendered once per source
|
|
9
|
+
// directory, not once per file below it, which is why an error in it is
|
|
10
|
+
// reported once. A rendered part may hold slashes and lands nested; `.` and
|
|
11
|
+
// `..` are resolved, and a path that leaves the output, is absolute, holds a
|
|
12
|
+
// NUL character, or is a file at the output's root is an error.
|
|
13
|
+
//
|
|
14
|
+
// A file whose name ends in `.jinja` is rendered with the context and lands
|
|
15
|
+
// without the suffix; any other is copied as bytes. A `.jinja` file
|
|
16
|
+
// that is not valid text is copied as bytes, under the name without the
|
|
17
|
+
// suffix; its content is otherwise carried as it is, a byte order mark
|
|
18
|
+
// included. The mode is reduced to 0o644 or, where its owner may execute it,
|
|
19
|
+
// 0o755, as types.ts states. A file whose Jinja fails is an error naming its
|
|
20
|
+
// path in the template.
|
|
21
|
+
//
|
|
22
|
+
// The templates of a render land in one tree, and every path is written once,
|
|
23
|
+
// by one template. Two sources that render to one path are an error naming
|
|
24
|
+
// both, a file beside its `.jinja` sibling included, as is a file with another
|
|
25
|
+
// path rendered below it; where the two belong to two templates, the error
|
|
26
|
+
// names both templates as well, in `template` the one whose name sorts first
|
|
27
|
+
// for one path, and the file's for a path below a file. Copier
|
|
28
|
+
// differs here: the `.jinja` file wins and the other is silently not written.
|
|
29
|
+
// Directories at one path are one directory, of one template or of several.
|
|
30
|
+
// A directory that renders to a path and holds nothing in the result, because
|
|
31
|
+
// the templates hold nothing there or because every file below it was
|
|
32
|
+
// skipped, lands as an empty directory, as copier creates it; only the
|
|
33
|
+
// innermost of nested ones is kept, and one below which another template
|
|
34
|
+
// writes is implied by that template's paths. An empty directory several
|
|
35
|
+
// templates hold is owned by the one whose name sorts first, so that the owner
|
|
36
|
+
// does not depend on the order the templates are given in.
|
|
37
|
+
// A symlink anywhere in a template is rejected where the template is read
|
|
38
|
+
// (manifest.ts).
|
|
39
|
+
import { messageOf } from "./errors.js";
|
|
40
|
+
import { renderString } from "./jinja.js";
|
|
41
|
+
import { FILES } from "./manifest.js";
|
|
42
|
+
import { DIRECTORY, fileMode, isExecutable, MODE_TYPE, REGULAR } from "./types.js";
|
|
43
|
+
const SUFFIX = '.jinja';
|
|
44
|
+
const utf8 = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
45
|
+
const encoder = new TextEncoder();
|
|
46
|
+
/** What an empty directory is written as. */
|
|
47
|
+
const EMPTY_DIRECTORY = { bytes: new Uint8Array(), mode: DIRECTORY | 0o755 };
|
|
48
|
+
/** A file's entry as `render` writes it: its bytes, and a mode of 0o644 or 0o755. */
|
|
49
|
+
const written = (bytes, mode) => ({ bytes, mode: fileMode(isExecutable(mode)) });
|
|
50
|
+
/** The text of `bytes`, or undefined where they are not valid UTF-8. */
|
|
51
|
+
function textOf(bytes) {
|
|
52
|
+
try {
|
|
53
|
+
return utf8.decode(bytes);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** The last part of a source path. */
|
|
60
|
+
const nameOf = (source) => source.slice(source.lastIndexOf('/') + 1);
|
|
61
|
+
/** The directory of a source path: everything before its last `/`, or `''`. */
|
|
62
|
+
const parentOf = (source) => source.slice(0, Math.max(0, source.lastIndexOf('/')));
|
|
63
|
+
/** `part` below `parent`, both of the output and with `..` and `.` resolved; undefined where it leaves the output. */
|
|
64
|
+
function join(parent, part) {
|
|
65
|
+
// An absolute part, or one with a NUL character, is no path inside the output.
|
|
66
|
+
if (part.startsWith('/') || part.includes('\0'))
|
|
67
|
+
return undefined;
|
|
68
|
+
const parts = parent === '' ? [] : parent.split('/');
|
|
69
|
+
for (const segment of part.split('/')) {
|
|
70
|
+
if (segment === '' || segment === '.')
|
|
71
|
+
continue;
|
|
72
|
+
if (segment !== '..')
|
|
73
|
+
parts.push(segment);
|
|
74
|
+
else if (parts.pop() === undefined)
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return parts.join('/');
|
|
78
|
+
}
|
|
79
|
+
/** The entries the templates of `renderings` render together, by rendered path; their problems go to `errors`. */
|
|
80
|
+
export function renderFiles(renderings, errors) {
|
|
81
|
+
const rendered = new Map();
|
|
82
|
+
// Two entries that cannot both be written: an error naming both sources, and
|
|
83
|
+
// their templates where they differ, the first one's in `template`.
|
|
84
|
+
const clash = (first, second, message) => {
|
|
85
|
+
const template = first.owner.manifest.name;
|
|
86
|
+
if (first.owner === second.owner) {
|
|
87
|
+
errors.push({ template, path: `${FILES}${second.source}`, message: message(first.source, second.source) });
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const other = second.owner.manifest.name;
|
|
91
|
+
const text = message(`${template}'s ${first.source}`, `${other}'s ${second.source}`);
|
|
92
|
+
errors.push({ template, other, path: `${FILES}${first.source}`, message: text });
|
|
93
|
+
};
|
|
94
|
+
// Two templates at one path are named first by name; two sources of one template in the order found.
|
|
95
|
+
const put = (path, value) => {
|
|
96
|
+
const first = rendered.get(path);
|
|
97
|
+
if (first === undefined)
|
|
98
|
+
rendered.set(path, value);
|
|
99
|
+
else {
|
|
100
|
+
const [a, b] = value.owner.manifest.name < first.owner.manifest.name ? [value, first] : [first, value];
|
|
101
|
+
clash(a, b, (one, two) => `${one} and ${two} both render to ${path}`);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
// The directories that may land empty, by rendered path: directories at one
|
|
105
|
+
// path are one, named by the first source of the template whose name sorts first.
|
|
106
|
+
const directories = new Map();
|
|
107
|
+
for (const { template, context } of renderings) {
|
|
108
|
+
for (const [path, source] of renderTemplate(template, context, put, errors)) {
|
|
109
|
+
const first = directories.get(path);
|
|
110
|
+
if (first === undefined || template.manifest.name < first.owner.manifest.name)
|
|
111
|
+
directories.set(path, { entry: EMPTY_DIRECTORY, source, owner: template });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// A directory is kept where nothing is written below it.
|
|
115
|
+
const taken = [...rendered.keys(), ...directories.keys()];
|
|
116
|
+
for (const [path, directory] of directories) {
|
|
117
|
+
if (!taken.some((other) => other.startsWith(`${path}/`)))
|
|
118
|
+
put(path, directory);
|
|
119
|
+
}
|
|
120
|
+
for (const [path, below] of rendered) {
|
|
121
|
+
for (let up = parentOf(path); up !== ''; up = parentOf(up)) {
|
|
122
|
+
const above = rendered.get(up);
|
|
123
|
+
if (above !== undefined && (above.entry.mode & MODE_TYPE) === REGULAR) {
|
|
124
|
+
clash(above, below, (a, b) => `${a} renders to ${up}, a file, and ${b} renders to ${path} below it`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return rendered;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Puts the files `template` renders in `context`, and returns the paths its
|
|
132
|
+
* source directories render to, each with its first source; its problems go
|
|
133
|
+
* to `errors`.
|
|
134
|
+
*/
|
|
135
|
+
function renderTemplate(template, context, put, errors) {
|
|
136
|
+
const fail = (source, message) => {
|
|
137
|
+
errors.push({ template: template.manifest.name, path: `${FILES}${source}`, message });
|
|
138
|
+
return 'failed';
|
|
139
|
+
};
|
|
140
|
+
// Where the source name `name`, which is the last part of `source`, lands below the rendered directory `parent`.
|
|
141
|
+
const locate = (parent, name, source) => {
|
|
142
|
+
if (typeof parent === 'string')
|
|
143
|
+
return 'skipped';
|
|
144
|
+
let part;
|
|
145
|
+
try {
|
|
146
|
+
part = renderString(name, context);
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
return fail(source, `error rendering template path ${source}: ${messageOf(error)}`);
|
|
150
|
+
}
|
|
151
|
+
if (part === '')
|
|
152
|
+
return 'skipped';
|
|
153
|
+
const path = join(parent.path, part);
|
|
154
|
+
if (path === undefined) {
|
|
155
|
+
const shown = parent.path === '' || part.startsWith('/') ? part : `${parent.path}/${part}`;
|
|
156
|
+
return fail(source, `${JSON.stringify(shown)} is forbidden: it is not inside the output`);
|
|
157
|
+
}
|
|
158
|
+
return { path };
|
|
159
|
+
};
|
|
160
|
+
// Each source directory is located once, with the root as `''`.
|
|
161
|
+
const directories = new Map([['', { path: '' }]]);
|
|
162
|
+
const directoryOf = (source) => {
|
|
163
|
+
const known = directories.get(source);
|
|
164
|
+
if (known !== undefined)
|
|
165
|
+
return known;
|
|
166
|
+
const located = locate(directoryOf(parentOf(source)), nameOf(source), source);
|
|
167
|
+
directories.set(source, located);
|
|
168
|
+
return located;
|
|
169
|
+
};
|
|
170
|
+
for (const [source, entry] of template.files) {
|
|
171
|
+
if ((entry.mode & MODE_TYPE) === DIRECTORY) {
|
|
172
|
+
directoryOf(source);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const isTemplate = source.endsWith(SUFFIX);
|
|
176
|
+
if (nameOf(source) === SUFFIX) {
|
|
177
|
+
fail(source, `${source} has no name besides the ${SUFFIX} suffix`);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const name = isTemplate ? nameOf(source).slice(0, -SUFFIX.length) : nameOf(source);
|
|
181
|
+
const located = locate(directoryOf(parentOf(source)), name, source);
|
|
182
|
+
if (typeof located === 'string')
|
|
183
|
+
continue;
|
|
184
|
+
const path = located.path;
|
|
185
|
+
if (path === '') {
|
|
186
|
+
fail(source, `${source} renders to the output's root, which is no file`);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const text = isTemplate ? textOf(entry.bytes) : undefined;
|
|
190
|
+
if (text === undefined) {
|
|
191
|
+
put(path, { entry: written(entry.bytes, entry.mode), source, owner: template });
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
put(path, { entry: written(encoder.encode(renderString(text, context)), entry.mode), source, owner: template });
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
fail(source, messageOf(error));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// Directories at one path are one, and the first source names it.
|
|
202
|
+
const located = new Map();
|
|
203
|
+
for (const [source, at] of directories) {
|
|
204
|
+
if (typeof at !== 'string' && at.path !== '' && !located.has(at.path))
|
|
205
|
+
located.set(at.path, source);
|
|
206
|
+
}
|
|
207
|
+
return located;
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=files.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.js","sourceRoot":"","sources":["../src/files.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,2EAA2E;AAC3E,+EAA+E;AAC/E,mDAAmD;AACnD,EAAE;AACF,8EAA8E;AAC9E,yEAAyE;AACzE,wEAAwE;AACxE,wEAAwE;AACx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1
|
+
// gitMergeFile: the real merge of one file, the `MergeFile` the CLI hands to
|
|
2
|
+
// `merge`. It is the one part of the package outside the core that touches the
|
|
3
|
+
// filesystem and runs a command, and the core never imports it: it writes the
|
|
4
|
+
// three sides to a temporary directory, runs `git merge-file` there, and
|
|
5
|
+
// removes the directory again.
|
|
6
|
+
//
|
|
7
|
+
// git merge-file -p -L current -L "old render" -L "new render" -- current old new
|
|
8
|
+
//
|
|
9
|
+
// `-p` prints the merged file instead of overwriting the current side. The
|
|
10
|
+
// labels name the three sides, and a conflict stays in the file between them:
|
|
11
|
+
//
|
|
12
|
+
// <<<<<<< current
|
|
13
|
+
// the user's lines
|
|
14
|
+
// =======
|
|
15
|
+
// the new render's lines
|
|
16
|
+
// >>>>>>> new render
|
|
17
|
+
//
|
|
18
|
+
// The merge is reported conflicted when the result holds the opening or the
|
|
19
|
+
// closing label line exactly, `\r\n` line endings included: never by a bare
|
|
20
|
+
// `<<<<<<<`, which a file may hold for reasons of its own. A current side that
|
|
21
|
+
// already holds one of the two exact label lines, a conflict left by an earlier
|
|
22
|
+
// merge, is not merged again: git would nest a second pair of markers in the
|
|
23
|
+
// first. It is returned as it is, conflicted, and git is not run; the labels
|
|
24
|
+
// are chosen to make a file that holds such a line for other reasons unlikely. git's exit code is the
|
|
25
|
+
// number of conflicts, which serves only to tell a failed run (above 127) from
|
|
26
|
+
// a merge.
|
|
27
|
+
//
|
|
28
|
+
// git runs with no configuration but its built-in defaults: none of the
|
|
29
|
+
// caller's, so that `merge.conflictStyle` and the like cannot change the
|
|
30
|
+
// result; none from the environment of a git process that may have started
|
|
31
|
+
// this one, a hook's for one; and none of a repository the temporary directory
|
|
32
|
+
// may lie in, which git would otherwise find by walking up from it. Bytes are carried as they are: git merges lines
|
|
33
|
+
// whatever their ending and leaves the bytes of each line alone. A binary side
|
|
34
|
+
// never reaches it, since the core decides what is binary (merge.ts); git
|
|
35
|
+
// would fail on one.
|
|
36
|
+
//
|
|
37
|
+
// Ported from copier v9.19.1, copier/_main.py, the `merge-file` call with three
|
|
38
|
+
// labels and the conflict detection of `_apply_update`; MIT-licensed, see
|
|
39
|
+
// LICENSE.copier.
|
|
40
|
+
import { spawn } from 'node:child_process';
|
|
41
|
+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
42
|
+
import { devNull, tmpdir } from 'node:os';
|
|
43
|
+
import { dirname, join } from 'node:path';
|
|
44
|
+
/** The labels of the three sides, in the order git merge-file takes them. */
|
|
45
|
+
const LABELS = { current: 'current', old: 'old render', new: 'new render' };
|
|
46
|
+
/** The lines that open and close a conflict, without their line ending. */
|
|
47
|
+
const MARKERS = new Set([`<<<<<<< ${LABELS.current}`, `>>>>>>> ${LABELS.new}`]);
|
|
48
|
+
/** The three-way merge of one file by `git merge-file`. */
|
|
49
|
+
export async function gitMergeFile(sides) {
|
|
50
|
+
if (conflicted(sides.current))
|
|
51
|
+
return { bytes: sides.current, conflicted: true };
|
|
52
|
+
const dir = await mkdtemp(join(tmpdir(), 'merge-file-'));
|
|
53
|
+
try {
|
|
54
|
+
await Promise.all([
|
|
55
|
+
writeFile(join(dir, 'current'), sides.current),
|
|
56
|
+
writeFile(join(dir, 'old'), sides.old),
|
|
57
|
+
writeFile(join(dir, 'new'), sides.new),
|
|
58
|
+
]);
|
|
59
|
+
const labels = [LABELS.current, LABELS.old, LABELS.new].flatMap((label) => ['-L', label]);
|
|
60
|
+
const bytes = await git(['merge-file', '-p', ...labels, '--', 'current', 'old', 'new'], dir, sides.path);
|
|
61
|
+
return { bytes, conflicted: conflicted(bytes) };
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
await rm(dir, { recursive: true, force: true });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** Whether `bytes` hold a line that opens or closes a conflict between our labels. */
|
|
68
|
+
function conflicted(bytes) {
|
|
69
|
+
// latin1 maps each byte to one character, so that any bytes split into lines.
|
|
70
|
+
return Buffer.from(bytes)
|
|
71
|
+
.toString('latin1')
|
|
72
|
+
.split('\n')
|
|
73
|
+
.some((line) => MARKERS.has(line.endsWith('\r') ? line.slice(0, -1) : line));
|
|
74
|
+
}
|
|
75
|
+
/** What git prints for `args` in `cwd`, with no configuration but its defaults; a failure names `path`. */
|
|
76
|
+
function git(args, cwd, path) {
|
|
77
|
+
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_')));
|
|
78
|
+
// No global or system configuration, and no repository above `cwd` whose own would apply.
|
|
79
|
+
Object.assign(env, { GIT_CONFIG_GLOBAL: devNull, GIT_CONFIG_NOSYSTEM: '1', GIT_CEILING_DIRECTORIES: dirname(cwd) });
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
const child = spawn('git', args, { cwd, env, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
82
|
+
const stdout = [];
|
|
83
|
+
const stderr = [];
|
|
84
|
+
child.stdout.on('data', (chunk) => stdout.push(chunk));
|
|
85
|
+
child.stderr.on('data', (chunk) => stderr.push(chunk));
|
|
86
|
+
child.on('error', reject);
|
|
87
|
+
child.on('close', (code) => {
|
|
88
|
+
if (code !== null && code <= 127)
|
|
89
|
+
resolve(new Uint8Array(Buffer.concat(stdout)));
|
|
90
|
+
else
|
|
91
|
+
reject(new Error(`git merge-file failed on ${path}: ${Buffer.concat(stderr).toString().trim()}`));
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=git-merge-file.js.map
|