-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
162 lines (142 loc) · 5.02 KB
/
index.js
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
#!/usr/bin/env node
import child_process from 'child_process'
import { askQuestion } from './question-asker.js'
import * as fs from 'fs'
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import upload from './drive.cjs';
import { version } from "./version.js";
import onboarding from "./onboarding.js";
import figlet from 'figlet';
// from stackoverflow, but
// TODO: do this without fileURLToPath? I think the "real" node way is to use URLs throughout
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename);
// function log(msg) {
// console.log(`STEACC>> ${msg}`)
// }
// log(process.argv[2]) // debugging
switch (process.argv[2]) {
case "update":
case "up":
update()
break
case "backup":
case "upload": // deprecated
upload()
break
case "hello":
console.log("Hello S.T.E.A.C.C.")
break
case "version":
console.log(version);
break
case "figlet-fonts":
child_process.exec('figlet -l', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
for (let line of stdout.split('\n')) {
console.log(line)
const fancyText = figlet.textSync('Hello, World!', { font: line, width: 80 });
console.log(fancyText)
}
})
break
case "winget":
const ps = child_process.spawn('powershell', ['-File', 'run-winget.ps1'], { stdio: "inherit", cwd: __dirname });
ps.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
break
default:
let name, projectName
while (true) {
name = await askQuestion("Coder name: ")
if (/\s/.test(name)) {
console.log("No spaces allowed")
continue
}
break
}
// see if directory exists
let isOnboarding = false
if (fs.existsSync(`C:\\${name}\\`)) {
console.log(
figlet.textSync(`Welcome back,`, {
width: process.stdout.columns
})
)
console.log(
figlet.textSync(`${name}`, {
font: 'Small Keyboard',
width: process.stdout.columns
})
)
}
else {
isOnboarding = true
const contentDir = path.join(__dirname, "content")
await onboarding.run(name, contentDir)
}
while (true) {
if (isOnboarding) {
console.log("Now you must choose a name for your first project.")
console.log("Remember the name you choose; you will use it to get back to your code.")
}
else {
const subdirectories = fs.readdirSync(`C:\\${name}`).filter(
file => fs.statSync(`C:\\${name}\\${file}`).isDirectory());
console.log("\nExisting projects:\n")
// NOTE: NOT using backticks or other string in the line below
// so that it's logged as in the more raw way that an array is logged
// (e.g., [ "foo", "bar" ])
// so learners get used to seeing that.
console.log(subdirectories)
}
projectName = await askQuestion("Project name: ")
if (/\s/.test(projectName)) {
console.log("No spaces allowed")
continue
}
if (projectName === "") {
continue
}
break
}
if (projectName) {
// see if directory exists
if (fs.existsSync(`C:\\${name}\\${projectName}\\`)) {
console.log(`Loading project: ${name}`)
}
else {
console.log("Creating project...")
console.log(`mkdir C:\\${name}\\${projectName}\\`)
fs.mkdirSync(`C:\\${name}\\${projectName}\\`)
// TODO: create a more complete package.json?
fs.writeFileSync(`C:\\${name}\\${projectName}\\package.json`, JSON.stringify({
"type": "module"
}))
fs.copyFileSync(__dirname + '\\question-asker.js',
`C:\\${name}\\${projectName}\\question-asker.js`)
fs.copyFileSync(__dirname + '\\favicon.ico',
`C:\\${name}\\${projectName}\\favicon.ico`)
}
}
console.log("Changing working directory...")
let cdCommand = `cd C:\\${name}\\`
if (projectName) {
cdCommand += `${projectName}\\`
}
fs.writeFileSync(process.env.TEMP + '\\st-out.ps1', `"${cdCommand}"\n${cdCommand}\n`)
}
function update() {
child_process.exec('npm update -g @marcstober/steacc', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
})
}