-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.ts
108 lines (87 loc) · 2.53 KB
/
init.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
import * as path from 'path';
import * as argopts from 'cli';
const HelpMessage = `Initialize a new Wren project.
USAGE:
deno run -A -r https://wren.deno.dev <DIRECTORY>
OPTIONS:
--vscode setup this Wren project for VSCode
`;
const template = (name: string) => (element: string) =>
`https://raw.githubusercontent.com/zaiste/wren/main/examples/${name}/${element}`;
const templates = {
// FIXME automate with GitHub API
bare: ['README.md', 'deno.json', 'import_map.json', 'main.ts', 'justfile'],
};
const download = async (url: string, path: string) => {
const response = await fetch(url);
const text = await response.text();
Deno.writeTextFile(path, text);
};
const error = (message: string) => {
console.error(`%cerror%c: ${message}`, 'color: red; font-weight: bold', '');
Deno.exit(1);
};
const flags = argopts.parse(Deno.args, {
boolean: ['vscode'],
default: { 'vscode': null },
});
if (flags._.length !== 1) {
error(HelpMessage);
}
console.log(
`\n%c 🪶 Wren %c`,
'background-color: #d8cbc4; color: black; font-weight: bold',
'',
);
console.log('A small, but powerful HTTP library built for convenience and simplicity');
const projectName = Deno.args[0];
const projectPath = path.resolve(projectName);
try {
const dir = [...Deno.readDirSync(projectPath)];
const isEmpty = dir.length === 0 ||
dir.length === 1 && dir[0].name === '.git';
if (!isEmpty) {
error('Directory is not empty.');
}
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
await Deno.mkdir(projectName, { recursive: true });
const input = 'bare';
for (const el of templates[input]) {
await download(template(input)(el), path.join(projectName, el));
}
if (flags.vscode) {
await Deno.mkdir(path.join(projectPath, '.vscode'), { recursive: true });
const VSCodeSettings = {
'deno.enable': true,
'deno.lint': true,
'editor.defaultFormatter': 'denoland.vscode-deno',
};
await Deno.writeTextFile(
path.join(projectPath, '.vscode', 'settings.json'),
JSON.stringify(VSCodeSettings, null, 2) + '\n',
);
const VSCodeExtensions = {
recommendations: ['denoland.vscode-deno'],
};
await Deno.writeTextFile(
path.join(projectPath, '.vscode', 'extensions.json'),
JSON.stringify(VSCodeExtensions, null, 2) + '\n',
);
}
console.log('\n%cProject initialized.\n', 'color: green; font-weight: bold');
console.log(
`Enter the project directory using %ccd ${projectName}%c.`,
'color: cyan',
'',
);
console.log(
'Run %cdeno task start%c to start the project. %cCTRL-C%c to stop.',
'color: cyan',
'',
'color: cyan',
'',
);