-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.ts
316 lines (281 loc) Β· 8.91 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { RepositoryCache } from "./src/cache.ts";
import { VERSION } from "./src/constants.ts";
import {
colors,
Command,
CompletionsCommand,
DenoLandProvider,
HelpCommand,
UpgradeCommand,
} from "./src/deps/cli.ts";
import { copy, emptyDir, ensureDir } from "./src/deps/fs.ts";
import { isError, isString } from "./src/deps/npm.ts";
import { path } from "./src/deps/path.ts";
import { LevelName } from "./src/deps/std.ts";
import { ScaffoldError } from "./src/errors.ts";
import { loadRepository } from "./src/load_repository.ts";
import { createLogger } from "./src/logger.ts";
import { ScaffoldPermissions } from "./src/template/define_template.ts";
import { loadWorker } from "./src/template/load_worker.ts";
import { readJson, wait, writeJson } from "./src/utils.ts";
import { directoryIsEmpty } from "./src/utils/directory_is_empty.ts";
import { getUserDetails } from "./src/utils/get_user_details.ts";
import { isColorSupported } from "./src/utils/is_color_supported.ts";
type ScaffoldActionOptions = Parameters<
Parameters<typeof main["action"]>[0]
>[0];
const main = new Command()
.globalOption(
"--cache-dir [cacheDir:string]",
"Set a custom cache directory.",
)
.name("scaffold")
.version(VERSION)
.arguments("<repo:string> [folder:string]")
.description(
`ποΈ Scaffold a new project from any GitHub, GitLab or BitBucket git repository.`,
)
.option(
"--cache-only [cacheOnly:string]",
"Only use the cache (no network requests).",
)
.option("--no-cache", "Disable the cache.")
.option("--reset-cache", "Reset the cache before the download.")
.option(
"-d, --debug",
`Enable debug logging.`,
)
.option("-f, --force", "Overwrite files even if they already exist.")
.option("-s, --silent", "Disable all logging.")
.option(
"--no-template",
`Disable loading the ${colors.gray.italic("scaffold.config.ts")} file.`,
)
.option(
"-y, --no-interactive",
`Disable the interactive prompt. Might break permission requests.`,
)
.option(
"--use-temporary-source",
`Copy local files to a temporary directory.`,
)
.example(
"GitHub",
`${
colors.yellow("scaffold ifiokjr/templates/deno my-project")
}\nThis will use the deno directory within the repository https://github.com/ifiokjr/templates`,
)
.example(
"GitLab",
`${
colors.yellow("scaffold gitlab:ifiokjr/scaffold-test my-project")
}\nThis will pull directly from https://gitlab.com/ifiokjr/scaffold-test`,
)
.example(
"Local",
`${
colors.yellow("scaffold ./path/to/local/directory my-project")
}\nThe path must start with './' to be recognized as a local directory.`,
)
.action(mainAction);
const invalidAlias = new Set([
"help",
"version",
"completions",
"upgrade",
"alias",
"cache",
]);
main
.command("alias")
.arguments("<alias:string> <repo:string>")
.description(
"Create an alias for a template repository",
).action(async (options, alias, repo) => {
const spinner = wait({
text: `Creating alias: ${alias} for repo: ${repo}`,
});
const log = createLogger({ name: "scaffold", levelName: "WARNING" });
const directory = isString(options.cacheDir)
? path.resolve(options.cacheDir)
: undefined;
const cache = new RepositoryCache({ directory, log });
await cache.store.load();
if (cache.store.getAlias(alias)) {
spinner.fail(`Alias already exists.`);
Deno.exit(1);
}
if (invalidAlias.has(alias)) {
spinner.fail(
`The provided alias name is invalid. These names are reserved: ${
[...invalidAlias].join(", ")
}`,
);
Deno.exit(1);
}
await cache.store.setAlias(alias, repo).save();
spinner.succeed(`Alias created.`);
});
const upgrade = new UpgradeCommand({
main: "scaffold.ts",
provider: new DenoLandProvider(),
args: ["--unstable", "-A", "-n", "scaffold"],
});
// Additional commands
main
.command("upgrade", upgrade)
.command("help", new HelpCommand().global())
.command("completions", new CompletionsCommand());
// .command("alias", alias);
await main.parse(Deno.args);
/**
* The action to be performed by the scaffold command.
*/
async function mainAction(
options: ScaffoldActionOptions,
templateFolder: string,
folder = "",
) {
// keep track of the exit code
let exit = 0;
const { debug = false, silent = false, force = false } = options;
const levelName: LevelName = silent
? "CRITICAL"
: debug
? "DEBUG"
: "WARNING";
const log = createLogger({ name: "scaffold", levelName });
const destination = path.resolve(folder);
const spinner = wait({
enabled: !silent || levelName !== "CRITICAL",
text: `Scaffolding the project ${colors.gray.italic(templateFolder)}`,
});
const shouldCache = options.cache !== false;
const cacheDirectory = isString(options.cacheDir)
? path.resolve(options.cacheDir)
: undefined;
const cache = new RepositoryCache({ directory: cacheDirectory, log });
const temporary: string[] = [];
try {
if (shouldCache) {
log.info("Loading store and cache...", cache.getDownloadPath());
await cache.load();
log.info("Store and cache successfully loaded");
}
if (options.resetCache === true) {
await cache.reset();
}
let source: string;
let permissions: Partial<ScaffoldPermissions> = {};
let key: string | undefined;
const alias = cache.store.getAlias(templateFolder);
if (alias) {
templateFolder = alias;
}
if (options.cacheOnly === true) {
const recent = cache.store.getRecent(templateFolder);
if (!recent) {
throw new ScaffoldError(
`--cache-only was used but there is no recent cache entry for ${templateFolder}`,
);
}
key = recent;
source = cache.getDownloadPath(key);
} else if (
["./", "../", "/"].some((p) => templateFolder.startsWith(p)) ||
path.isAbsolute(templateFolder)
) {
spinner.text("Loading local folder...");
source = path.resolve(templateFolder);
if (options.useTemporarySource) {
source = await Deno.makeTempDir();
temporary.push(source);
await copy(path.resolve(templateFolder), source, { overwrite: true });
}
} else {
spinner.text("Loading repository...");
const result = await loadRepository(templateFolder, { cache });
source = result.directory;
key = result.key;
if (!shouldCache) {
temporary.push(source);
} else {
// save this as the most recent cache entry
await cache.store.setRecent(templateFolder, key).save();
}
spinner.succeed(
`Repository loaded from: ${colors.gray.italic(result.repo.url)}`,
);
spinner.text("Checking for saved permissions...");
permissions = await readJson(cache.permissions(result.key));
spinner.succeed("Cached permissions loaded");
}
if (!force && !(await directoryIsEmpty(destination))) {
throw new ScaffoldError(
`The destination folder is not empty: \`${
colors.gray.italic(destination)
}\`. Use \`${colors.blue("--force")}\` to overwrite files.`,
);
}
if (force) {
spinner.text("Removing existing files...");
await ensureDir(destination);
await emptyDir(destination);
}
spinner.text("Preparing scaffold...");
const variables = {
...await getUserDetails(),
name: path.basename(folder),
};
for (const [name, granted] of Object.entries(permissions)) {
if (granted.length === 0) {
continue;
}
log.info(
`${name} permissions automatically granted for "${granted.join(", ")}"`,
);
}
const processor = await loadWorker({
source,
destination,
interactive: options.interactive !== false,
name: "scaffold",
variables,
permissions,
});
spinner.succeed("Scaffold fully loaded.");
// check if this should be called.
await processor.getVariables();
permissions = await processor.getPermissions() ?? permissions;
await processor.processTemplate();
await processor.install();
if (shouldCache && key) {
await writeJson(cache.permissions(key), permissions);
}
spinner.succeed("Scaffolding completed!");
} catch (error) {
let message = "";
if (isError(error)) {
message = error.message;
} else if (isString(error)) {
message = error;
} else {
message = Deno.inspect(error, { colors: isColorSupported() });
}
log.info("The full error stack", error);
spinner.fail(`Something went wrong: ${message}`);
exit = 1;
} finally {
let removed = false;
// Remove all directories that were created.
for (const temp of temporary) {
removed = true;
log.debug("Removing temporary directory:", temp);
await Deno.remove(temp, { recursive: true });
}
if (removed) {
log.debug("Successfully removed temporary directories.");
}
Deno.exit(exit);
}
}