Skip to content

Commit faea38e

Browse files
committed
add warnings field to Info
drop `line` field from pre-existing `warnings` fields
1 parent 484afbe commit faea38e

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
lines changed

src/bin/hsm.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ Warning: ${formatOption(noMinifyOption.name)} is being deprecated and will be re
213213

214214
const timeTook = performance.now() - timeStart
215215

216-
for (const { message, line } of warnings)
217-
log(`Warning "${chalk.bold(message)}" on line ${chalk.bold(String(line))}`)
216+
for (const { message } of warnings)
217+
log(`Warning: ${chalk.bold(message)}`)
218218

219219
await writeFilePersistent(outputPath, script).catch((error: NodeJS.ErrnoException) => {
220220
if (!commands[2] || error.code != `EISDIR`)
@@ -601,7 +601,7 @@ ${colourN(`--help`)}
601601
}
602602
}
603603

604-
function logInfo({ path, users, characterCount, error }: Info, hackmudPath: string) {
604+
function logInfo({ path, users, characterCount, error, warnings }: Info, hackmudPath: string) {
605605
path = getRelativePath(`.`, path)
606606

607607
if (error) {
@@ -610,6 +610,9 @@ function logInfo({ path, users, characterCount, error }: Info, hackmudPath: stri
610610
return
611611
}
612612

613+
for (const warning of warnings)
614+
console.warn(colourF(`Warning: ${warning.message}`))
615+
613616
log(`Pushed ${chalk.bold(path)} to ${users.map(user => chalk.bold(userColours.get(user))).join(`, `)} | ${
614617
chalk.bold(String(characterCount))
615618
} chars | ${chalk.bold(

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export { watch } from "./watch"
99
// TODO `clean()` function that delete all scripts in hackmud directory #70
1010
// TODO optional argument (defaults to false) for `clean()` that makes it keep scripts with a source file #70
1111

12-
export type Info = { path: string, users: string[], characterCount: number, error: Error | undefined }
12+
export type Info =
13+
{ path: string, users: string[], characterCount: number, error: Error | undefined, warnings: { message: string }[] }

src/processScript/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function processScript(code: string, {
7373
filePath,
7474
mangleNames = false,
7575
forceQuineCheats
76-
}: ProcessOptions): Promise<{ script: string, warnings: { message: string, line: number }[] }> {
76+
}: ProcessOptions): Promise<{ script: string, warnings: { message: string }[] }> {
7777
assert(/^\w{11}$/.exec(uniqueId), HERE)
7878

7979
const sourceCode = code
@@ -298,7 +298,7 @@ export async function processScript(code: string, {
298298

299299
code = (await bundle.generate({})).output[0].code
300300

301-
const { file, seclevel } =
301+
const { file, seclevel, warnings } =
302302
transform(parse(code, { sourceType: `module` }), sourceCode, { uniqueId, scriptUser, scriptName })
303303

304304
if (statedSeclevel != undefined && seclevel < statedSeclevel) {
@@ -410,5 +410,5 @@ export async function processScript(code: string, {
410410
)
411411
}
412412

413-
return { script: code, warnings: [] }
413+
return { script: code, warnings }
414414
}

src/processScript/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ export function transform(
782782
}
783783
})
784784

785-
return { file, seclevel }
785+
return { file, seclevel, warnings }
786786

787787
function createGetFunctionPrototypeNode() {
788788
const name = globalFunctionsUnder7Characters.find(name => !program.scope.hasOwnBinding(name))

src/push.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ export async function push(
162162

163163
const uniqueId = Math.floor(Math.random() * (2 ** 52)).toString(36).padStart(11, `0`)
164164

165-
const { script: minifiedCode } = await processScript(
165+
const { script: minifiedCode, warnings } = await processScript(
166166
await readFile(path, { encoding: `utf8` }),
167167
{ minify, scriptUser: true, scriptName, uniqueId, filePath: path, mangleNames, forceQuineCheats }
168168
)
169169

170-
const info: Info = { path, users, characterCount: countHackmudCharacters(minifiedCode), error: undefined }
170+
const info: Info = { path, users, characterCount: countHackmudCharacters(minifiedCode), error: undefined, warnings }
171171

172172
await Promise.all(users.map(user => writeFilePersistent(
173173
resolvePath(hackmudPath, user, `scripts/${scriptName}.js`),

src/watch.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,24 @@ export async function watch(sourceDirectory: string, hackmudDirectory: string, {
130130
const usersToPushTo = [ ...usersToPushToSet ].filter(user => !scriptNamesToUsersToSkip.has(user))
131131

132132
if (!usersToPushTo.length) {
133-
onPush?.({ path, users: [], characterCount: 0, error: new Error(`no users to push to`) })
133+
onPush?.({ path, users: [], characterCount: 0, error: new Error(`no users to push to`), warnings: [] })
134134

135135
return
136136
}
137137

138138
const uniqueId = Math.floor(Math.random() * (2 ** 52)).toString(36).padStart(11, `0`)
139139
const filePath = resolvePath(sourceDirectory, path)
140140
let minifiedCode: string
141+
let warnings
141142

142143
try {
143-
({ script: minifiedCode } = await processScript(
144+
({ script: minifiedCode, warnings } = await processScript(
144145
await readFile(filePath, { encoding: `utf8` }),
145146
{ minify, scriptUser: true, scriptName, uniqueId, filePath, mangleNames, forceQuineCheats }
146147
))
147148
} catch (error) {
148149
assert(error instanceof Error, HERE)
149-
onPush?.({ path, users: [], characterCount: 0, error })
150+
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
150151

151152
return
152153
}
@@ -157,9 +158,13 @@ export async function watch(sourceDirectory: string, hackmudDirectory: string, {
157158
.replace(new RegExp(`\\$${uniqueId}\\$FULL_SCRIPT_NAME\\$`, `g`), `${user}.${scriptName}`)
158159
)))
159160

160-
onPush?.(
161-
{ path, users: usersToPushTo, characterCount: countHackmudCharacters(minifiedCode), error: undefined }
162-
)
161+
onPush?.({
162+
path,
163+
users: usersToPushTo,
164+
characterCount: countHackmudCharacters(minifiedCode),
165+
error: undefined,
166+
warnings
167+
})
163168

164169
return
165170
}
@@ -174,21 +179,22 @@ export async function watch(sourceDirectory: string, hackmudDirectory: string, {
174179
const filePath = resolvePath(sourceDirectory, path)
175180
const sourceCode = await readFile(filePath, { encoding: `utf8` })
176181
let script
182+
let warnings
177183

178184
try {
179-
({ script } = await processScript(
185+
({ script, warnings } = await processScript(
180186
sourceCode,
181187
{ minify, scriptUser: user, scriptName, filePath, mangleNames, forceQuineCheats }
182188
))
183189
} catch (error) {
184190
assert(error instanceof Error, HERE)
185-
onPush?.({ path, users: [], characterCount: 0, error })
191+
onPush?.({ path, users: [], characterCount: 0, error, warnings: [] })
186192

187193
return
188194
}
189195

190196
await writeFilePersistent(resolvePath(hackmudDirectory, user, `scripts`, `${scriptName}.js`), script)
191-
onPush?.({ path, users: [ user ], characterCount: countHackmudCharacters(script), error: undefined })
197+
onPush?.({ path, users: [ user ], characterCount: countHackmudCharacters(script), error: undefined, warnings })
192198
})
193199

194200
if (onReady)

0 commit comments

Comments
 (0)